diff --git a/crates/bevy_ecs/src/world/identifier.rs b/crates/bevy_ecs/src/world/identifier.rs index c65da1b81a7e1..b565c534d9233 100644 --- a/crates/bevy_ecs/src/world/identifier.rs +++ b/crates/bevy_ecs/src/world/identifier.rs @@ -1,4 +1,7 @@ -use crate::storage::SparseSetIndex; +use crate::{ + storage::SparseSetIndex, + system::{ReadOnlySystemParam, SystemParam}, +}; use std::sync::atomic::{AtomicUsize, Ordering}; #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] @@ -27,6 +30,27 @@ impl WorldId { } } +// SAFETY: Has read-only access to shared World metadata +unsafe impl ReadOnlySystemParam for WorldId {} + +// SAFETY: A World's ID is immutable and fetching it from the World does not borrow anything +unsafe impl SystemParam for WorldId { + type State = (); + + type Item<'world, 'state> = WorldId; + + fn init_state(_: &mut super::World, _: &mut crate::system::SystemMeta) -> Self::State {} + + unsafe fn get_param<'world, 'state>( + _: &'state mut Self::State, + _: &crate::system::SystemMeta, + world: &'world super::World, + _: u32, + ) -> Self::Item<'world, 'state> { + world.id + } +} + impl SparseSetIndex for WorldId { #[inline] fn sparse_set_index(&self) -> usize {