bevy/examples/ecs
Carter Anderson 9d453530fa System Param Lifetime Split (#2605)
# 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.
2021-08-15 20:51:53 +00:00
..
component_change_detection.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
ecs_guide.rs Cleanup FromResources (#2601) 2021-08-13 22:21:34 +00:00
event.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
fixed_timestep.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
hierarchy.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
iter_combinations.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
parallel_query.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
removal_detection.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
startup_system.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
state.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
system_chaining.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
system_param.rs System Param Lifetime Split (#2605) 2021-08-15 20:51:53 +00:00
system_sets.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
timers.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00