mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
9d453530fa
# Objective
Enable using exact World lifetimes during read-only access . This is motivated by the new renderer's need to allow read-only world-only queries to outlive the query itself (but still be constrained by the world lifetime).
For example:
115b170d1f/pipelined/bevy_pbr2/src/render/mod.rs (L774)
## Solution
Split out SystemParam state and world lifetimes and pipe those lifetimes up to read-only Query ops (and add into_inner for Res). According to every safety test I've run so far (except one), this is safe (see the temporary safety test commit). Note that changing the mutable variants to the new lifetimes would allow aliased mutable pointers (try doing that to see how it affects the temporary safety tests).
The new state lifetime on SystemParam does make `#[derive(SystemParam)]` more cumbersome (the current impl requires PhantomData if you don't use both lifetimes). We can make this better by detecting whether or not a lifetime is used in the derive and adjusting accordingly, but that should probably be done in its own pr.
## Why is this a draft?
The new lifetimes break QuerySet safety in one very specific case (see the query_set system in system_safety_test). We need to solve this before we can use the lifetimes given.
This is due to the fact that QuerySet is just a wrapper over Query, which now relies on world lifetimes instead of `&self` lifetimes to prevent aliasing (but in systems, each Query has its own implied lifetime, not a centralized world lifetime). I believe the fix is to rewrite QuerySet to have its own World lifetime (and own the internal reference). This will complicate the impl a bit, but I think it is doable. I'm curious if anyone else has better ideas.
Personally, I think these new lifetimes need to happen. We've gotta have a way to directly tie read-only World queries to the World lifetime. The new renderer is the first place this has come up, but I doubt it will be the last. Worst case scenario we can come up with a second `WorldLifetimeQuery<Q, F = ()>` parameter to enable these read-only scenarios, but I'd rather not add another type to the type zoo.
43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use bevy::{ecs::system::SystemParam, prelude::*};
|
|
|
|
/// This example creates a SystemParam struct that counts the number of players
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(PlayerCount(0))
|
|
.add_startup_system(spawn)
|
|
.add_system(count_players)
|
|
.run();
|
|
}
|
|
|
|
pub struct Player;
|
|
pub struct PlayerCount(usize);
|
|
|
|
/// The SystemParam struct can contain any types that can also be included in a
|
|
/// system function signature.
|
|
///
|
|
/// In this example, it includes a query and a mutable resource.
|
|
#[derive(SystemParam)]
|
|
struct PlayerCounter<'w, 's> {
|
|
players: Query<'w, 's, &'static Player>,
|
|
count: ResMut<'w, PlayerCount>,
|
|
}
|
|
|
|
impl<'w, 's> PlayerCounter<'w, 's> {
|
|
fn count(&mut self) {
|
|
self.count.0 = self.players.iter().len();
|
|
}
|
|
}
|
|
|
|
/// Spawn some players to count
|
|
fn spawn(mut commands: Commands) {
|
|
commands.spawn().insert(Player);
|
|
commands.spawn().insert(Player);
|
|
commands.spawn().insert(Player);
|
|
}
|
|
|
|
/// The SystemParam can be used directly in a system argument.
|
|
fn count_players(mut counter: PlayerCounter) {
|
|
counter.count();
|
|
|
|
println!("{} players in the game", counter.count.0);
|
|
}
|