bevy/crates
Carter Anderson a20dc36c8c Add new SystemState and rename old SystemState to SystemMeta (#2283)
This enables `SystemParams` to be used outside of function systems. Anything can create and store `SystemState`, which enables efficient "param state cached" access to `SystemParams`.

It adds a `ReadOnlySystemParamFetch` trait, which enables safe `SystemState::get` calls without unique world access.

I renamed the old `SystemState` to `SystemMeta` to enable us to mirror the `QueryState` naming convention (but I'm happy to discuss alternative names if people have other ideas). I initially pitched this as `ParamState`, but given that it needs to include full system metadata, that doesn't feel like a particularly accurate name.

```rust
#[derive(Eq, PartialEq, Debug)]
struct A(usize);

#[derive(Eq, PartialEq, Debug)]
struct B(usize);

let mut world = World::default();
world.insert_resource(A(42));
world.spawn().insert(B(7));

// we get nice lifetime elision when declaring the type on the left hand side
let mut system_state: SystemState<(Res<A>, Query<&B>)> = SystemState::new(&mut world);
let (a, query) = system_state.get(&world);
assert_eq!(*a, A(42), "returned resource matches initial value");
assert_eq!(
    *query.single().unwrap(),
    B(7),
    "returned component matches initial value"
);

// mutable system params require unique world access
let mut system_state: SystemState<(ResMut<A>, Query<&mut B>)> = SystemState::new(&mut world);
let (a, query) = system_state.get_mut(&mut world);

// static lifetimes are required when declaring inside of structs
struct SomeContainer {
  state: SystemState<(Res<'static, A>, Res<'static, B>)>
}

// this can be shortened using type aliases, which will be useful for complex param tuples
type MyParams<'a> = (Res<'a, A>, Res<'a, B>);
struct SomeContainer {
  state: SystemState<MyParams<'static>>
}

// It is the user's responsibility to call SystemState::apply(world) for parameters that queue up work   
let mut system_state: SystemState<(Commands, Query<&B>)> = SystemState::new(&mut world);
{
  let (mut commands, query) = system_state.get(&world);
  commands.insert_resource(3.14);
}
system_state.apply(&mut world);
```

## Future Work

* Actually use SystemState inside FunctionSystem. This would be trivial, but it requires FunctionSystem to wrap SystemState in Option in its current form (which complicates system metadata lookup). I'd prefer to hold off until we adopt something like the later designs linked in #1364, which enable us to contruct Systems using a World reference (and also remove the need for `.system`).
* Consider a "scoped" approach to automatically call SystemState::apply when systems params are no longer being used (either a container type with a Drop impl, or a function that takes a closure for user logic operating on params).
2021-06-02 19:57:38 +00:00
..
bevy_app Add inline documentation to bevy code (#1404) 2021-05-06 02:26:54 +00:00
bevy_asset [assets] fix Assets being set as 'changed' each frame (#2280) 2021-06-02 02:30:14 +00:00
bevy_audio Update rodio requirement from 0.13 to 0.14 (#2244) 2021-05-29 01:08:58 +00:00
bevy_core use bytemuck crate instead of Byteable trait (#2183) 2021-05-17 22:29:10 +00:00
bevy_derive Use bevy_reflect as path in case of no direct references (#1875) 2021-05-19 19:03:36 +00:00
bevy_diagnostic fix diagnostic length for asset count (#2165) 2021-05-14 19:31:36 +00:00
bevy_dylib Release 0.5.0 (#1835) 2021-04-06 18:48:48 +00:00
bevy_dynamic_plugin Various cleanups (#2046) 2021-05-01 20:07:06 +00:00
bevy_ecs Add new SystemState and rename old SystemState to SystemMeta (#2283) 2021-06-02 19:57:38 +00:00
bevy_gilrs Release 0.5.0 (#1835) 2021-04-06 18:48:48 +00:00
bevy_gltf Update glam (0.15.1) and hexasphere (3.4) (#2199) 2021-05-18 18:56:15 +00:00
bevy_input Hide re-exported docs (#1985) 2021-04-27 18:29:33 +00:00
bevy_internal Hide re-exported docs (#1985) 2021-04-27 18:29:33 +00:00
bevy_log Hide re-exported docs (#1985) 2021-04-27 18:29:33 +00:00
bevy_macro_utils Use bevy_reflect as path in case of no direct references (#1875) 2021-05-19 19:03:36 +00:00
bevy_math Update glam (0.15.1) and hexasphere (3.4) (#2199) 2021-05-18 18:56:15 +00:00
bevy_pbr Fix PBR regression for unlit materials (#2197) 2021-05-17 22:45:07 +00:00
bevy_reflect Use bevy_reflect as path in case of no direct references (#1875) 2021-05-19 19:03:36 +00:00
bevy_render Refactor ResMut/Mut/ReflectMut to remove duplicated code (#2217) 2021-05-30 19:29:31 +00:00
bevy_scene Hide re-exported docs (#1985) 2021-04-27 18:29:33 +00:00
bevy_sprite use bytemuck crate instead of Byteable trait (#2183) 2021-05-17 22:29:10 +00:00
bevy_tasks Various cleanups (#2046) 2021-05-01 20:07:06 +00:00
bevy_text Allows a number of clippy lints and fixes 2 (#1999) 2021-05-14 20:37:32 +00:00
bevy_transform drop overwritten component data on double insert (#2227) 2021-05-30 20:15:40 +00:00
bevy_ui Allows a number of clippy lints and fixes 2 (#1999) 2021-05-14 20:37:32 +00:00
bevy_utils EnumVariantMeta derive (#1972) 2021-04-21 23:46:54 +00:00
bevy_wgpu Allows a number of clippy lints and fixes 2 (#1999) 2021-05-14 20:37:32 +00:00
bevy_window Adds an alias mouse position -> cursor position (#2038) 2021-04-28 21:26:47 +00:00
bevy_winit Bump winit to 0.25 (#2186) 2021-05-18 18:36:36 +00:00