Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add more FromWorld implementations #3945

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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},
system::{
check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch,
Expand Down Expand Up @@ -168,6 +169,12 @@ impl<Param: SystemParam> SystemState<Param> {
}
}

impl<Param: SystemParam> FromWorld for SystemState<Param> {
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
Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -1224,6 +1225,25 @@ impl<T: Default> 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<T>(pub T);

macro_rules! impl_from_world {
TheRawMeatball marked this conversation as resolved.
Show resolved Hide resolved
($($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,
}
Expand Down