diff --git a/crates/bevy_ecs/src/world/identifier.rs b/crates/bevy_ecs/src/world/identifier.rs index 2f5f2e9b91..b075205818 100644 --- a/crates/bevy_ecs/src/world/identifier.rs +++ b/crates/bevy_ecs/src/world/identifier.rs @@ -1,3 +1,4 @@ +use crate::system::{ExclusiveSystemParam, SystemMeta}; use crate::{ component::Tick, storage::SparseSetIndex, @@ -65,6 +66,19 @@ unsafe impl SystemParam for WorldId { } } +impl ExclusiveSystemParam for WorldId { + type State = WorldId; + type Item<'s> = WorldId; + + fn init(world: &mut World, _system_meta: &mut SystemMeta) -> Self::State { + world.id() + } + + fn get_param<'s>(state: &'s mut Self::State, _system_meta: &SystemMeta) -> Self::Item<'s> { + *state + } +} + impl SparseSetIndex for WorldId { #[inline] fn sparse_set_index(&self) -> usize { @@ -95,6 +109,30 @@ mod tests { } } + #[test] + fn world_id_system_param() { + fn test_system(world_id: WorldId) -> WorldId { + world_id + } + + let mut world = World::default(); + let system_id = world.register_system(test_system); + let world_id = world.run_system(system_id).unwrap(); + assert_eq!(world.id(), world_id); + } + + #[test] + fn world_id_exclusive_system_param() { + fn test_system(_world: &mut World, world_id: WorldId) -> WorldId { + world_id + } + + let mut world = World::default(); + let system_id = world.register_system(test_system); + let world_id = world.run_system(system_id).unwrap(); + assert_eq!(world.id(), world_id); + } + // We cannot use this test as-is, as it causes other tests to panic due to using the same atomic variable. // #[test] // #[should_panic]