Skip to content

Commit

Permalink
Add more FromWorld implementations (bevyengine#3945)
Browse files Browse the repository at this point in the history
# Objective

Make `FromWorld` more useful for abstractions with a form similar to
```rs
trait FancyAbstraction {
  type PreInitializedData: FromWorld;
}
```

## Solution

Add a `FromWorld` implementation for `SystemState` as well as a way to group together multiple `FromWorld` implementing types as one.

Note: I plan to follow up this PR with another to add `Local` support to exclusive systems, which should get a fair amount of use from the `FromWorld` implementation on `SystemState`.
  • Loading branch information
TheRawMeatball authored and aevyrie committed Jun 7, 2022
1 parent 0d63c4f commit c95048a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId},
component::ComponentId,
entity::Entity,
prelude::FromWorld,
query::{
Access, Fetch, FetchState, FilterFetch, FilteredAccess, NopFetch, QueryCombinationIter,
QueryIter, WorldQuery,
Expand Down Expand Up @@ -32,6 +33,15 @@ where
pub(crate) filter_state: F::State,
}

impl<Q: WorldQuery, F: WorldQuery> FromWorld for QueryState<Q, F>
where
F::Fetch: FilterFetch,
{
fn from_world(world: &mut World) -> Self {
world.query_filtered()
}
}

impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F>
where
F::Fetch: FilterFetch,
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId},
component::ComponentId,
prelude::FromWorld,
query::{Access, FilteredAccessSet},
schedule::SystemLabel,
system::{
Expand Down Expand Up @@ -229,6 +230,12 @@ impl<Param: SystemParam> SystemState<Param> {
}
}

impl<Param: SystemParam> FromWorld for SystemState<Param> {
fn from_world(world: &mut World) -> Self {
Self::new(world)
}
}

/// Conversion trait to turn something into a [`System`].
///
/// Use this to get a system from a function. Also note that every system implements this trait as
Expand Down

0 comments on commit c95048a

Please sign in to comment.