From 3f614535b99b6cda366c93c16107303eaebecec7 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Tue, 5 Apr 2022 20:04:34 +0000 Subject: [PATCH] Add more FromWorld implementations (#3945) # 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`. --- crates/bevy_ecs/src/query/state.rs | 10 ++++++++++ crates/bevy_ecs/src/system/function_system.rs | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 3decc6def908a..5956e69564661 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -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, @@ -32,6 +33,15 @@ where pub(crate) filter_state: F::State, } +impl FromWorld for QueryState +where + F::Fetch: FilterFetch, +{ + fn from_world(world: &mut World) -> Self { + world.query_filtered() + } +} + impl QueryState where F::Fetch: FilterFetch, diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index c03d09684f844..f3cade700fce5 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -1,6 +1,7 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId}, component::ComponentId, + prelude::FromWorld, query::{Access, FilteredAccessSet}, schedule::SystemLabel, system::{ @@ -229,6 +230,12 @@ impl SystemState { } } +impl FromWorld for SystemState { + 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