From 9e67da3766feeb5b62d166b00335092ff04193f3 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Mon, 14 Feb 2022 19:49:26 +0300 Subject: [PATCH 1/4] Add more FromWorld implementations --- crates/bevy_ecs/src/system/function_system.rs | 7 +++++++ crates/bevy_ecs/src/world/mod.rs | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 36835c1d90a5d..33c19360a14aa 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}, system::{ check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch, @@ -168,6 +169,12 @@ impl SystemState { } } +impl FromWorld for SystemState { + fn from_world(world: &mut World) -> Self { + Self::new(world) + } +} + /// A trait for defining systems with a [`SystemParam`] associated type. /// /// This facilitates the creation of systems that are generic over some trait diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index ad037819e89f7..69d7462596373 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -3,6 +3,7 @@ mod spawn_batch; mod world_cell; pub use crate::change_detection::Mut; +use bevy_ecs_macros::all_tuples; pub use entity_ref::*; pub use spawn_batch::*; pub use world_cell::*; @@ -1224,6 +1225,25 @@ impl FromWorld for T { } } +/// Wrapper type to enable getting multiple [`FromWorld`] types in one type. +/// +/// This wrapper is necessary because a [`FromWorld`] implementation for a tuple of +/// [`FromWorld`] implementing types would conflict with the [`FromWorld`] impl over all +/// [`Default`] type. +pub struct FromWorldWrap(pub T); + +macro_rules! impl_from_world { + ($($t:ident),*) => { + impl<$($t: FromWorld,)*> FromWorld for FromWorldWrap<($($t,)*)> { + fn from_world(_world: &mut World) -> Self { + FromWorldWrap(($($t::from_world(_world),)*)) + } + } + }; +} + +all_tuples!(impl_from_world, 0, 12, T); + struct MainThreadValidator { main_thread: std::thread::ThreadId, } From 2bf05905a37ea03c667cbfc3826c970b39392de5 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Sat, 2 Apr 2022 12:47:12 +0300 Subject: [PATCH 2/4] impl FromWorld for QueryState --- crates/bevy_ecs/src/query/state.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index f675de4d89a1d..796394031113a 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, From d111ae27f47d097dffab8c558484a11aa83244a0 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Tue, 5 Apr 2022 22:35:59 +0300 Subject: [PATCH 3/4] Remove FromWorldWrap --- crates/bevy_ecs/src/world/mod.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index ef6e56cfe5f48..b6daec9b8be5b 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1317,25 +1317,6 @@ impl FromWorld for T { } } -/// Wrapper type to enable getting multiple [`FromWorld`] types in one type. -/// -/// This wrapper is necessary because a [`FromWorld`] implementation for a tuple of -/// [`FromWorld`] implementing types would conflict with the [`FromWorld`] impl over all -/// [`Default`] type. -pub struct FromWorldWrap(pub T); - -macro_rules! impl_from_world { - ($($t:ident),*) => { - impl<$($t: FromWorld,)*> FromWorld for FromWorldWrap<($($t,)*)> { - fn from_world(_world: &mut World) -> Self { - FromWorldWrap(($($t::from_world(_world),)*)) - } - } - }; -} - -all_tuples!(impl_from_world, 0, 12, T); - struct MainThreadValidator { main_thread: std::thread::ThreadId, } From 1349ec8dd4aa7a0290f14eaad48ffc5b5fa5ad95 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Tue, 5 Apr 2022 22:36:36 +0300 Subject: [PATCH 4/4] cleanup --- crates/bevy_ecs/src/world/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index b6daec9b8be5b..5060a8cfbfd7c 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -3,7 +3,6 @@ mod spawn_batch; mod world_cell; pub use crate::change_detection::Mut; -use bevy_ecs_macros::all_tuples; pub use entity_ref::*; pub use spawn_batch::*; pub use world_cell::*;