bevy_ecs/world/
identifier.rs

1use crate::{
2    component::Tick,
3    storage::SparseSetIndex,
4    system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam},
5    world::{FromWorld, World},
6};
7use core::sync::atomic::{AtomicUsize, Ordering};
8
9use super::unsafe_world_cell::UnsafeWorldCell;
10
11#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
12// We use usize here because that is the largest `Atomic` we want to require
13/// A unique identifier for a [`World`].
14///
15/// The trait [`FromWorld`] is implemented for this type, which returns the
16/// ID of the world passed to [`FromWorld::from_world`].
17// Note that this *is* used by external crates as well as for internal safety checks
18pub struct WorldId(usize);
19
20/// The next [`WorldId`].
21static MAX_WORLD_ID: AtomicUsize = AtomicUsize::new(0);
22
23impl WorldId {
24    /// Create a new, unique [`WorldId`]. Returns [`None`] if the supply of unique
25    /// [`WorldId`]s has been exhausted
26    ///
27    /// Please note that the [`WorldId`]s created from this method are unique across
28    /// time - if a given [`WorldId`] is [`Drop`]ped its value still cannot be reused
29    pub fn new() -> Option<Self> {
30        MAX_WORLD_ID
31            // We use `Relaxed` here since this atomic only needs to be consistent with itself
32            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |val| {
33                val.checked_add(1)
34            })
35            .map(WorldId)
36            .ok()
37    }
38}
39
40impl FromWorld for WorldId {
41    #[inline]
42    fn from_world(world: &mut World) -> Self {
43        world.id()
44    }
45}
46
47// SAFETY: No world data is accessed.
48unsafe impl ReadOnlySystemParam for WorldId {}
49
50// SAFETY: No world data is accessed.
51unsafe impl SystemParam for WorldId {
52    type State = ();
53
54    type Item<'world, 'state> = WorldId;
55
56    fn init_state(_: &mut World, _: &mut SystemMeta) -> Self::State {}
57
58    #[inline]
59    unsafe fn get_param<'world, 'state>(
60        _: &'state mut Self::State,
61        _: &SystemMeta,
62        world: UnsafeWorldCell<'world>,
63        _: Tick,
64    ) -> Self::Item<'world, 'state> {
65        world.id()
66    }
67}
68
69impl ExclusiveSystemParam for WorldId {
70    type State = WorldId;
71    type Item<'s> = WorldId;
72
73    fn init(world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {
74        world.id()
75    }
76
77    fn get_param<'s>(state: &'s mut Self::State, _system_meta: &SystemMeta) -> Self::Item<'s> {
78        *state
79    }
80}
81
82impl SparseSetIndex for WorldId {
83    #[inline]
84    fn sparse_set_index(&self) -> usize {
85        self.0
86    }
87
88    #[inline]
89    fn get_sparse_set_index(value: usize) -> Self {
90        Self(value)
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn world_ids_unique() {
100        let ids = core::iter::repeat_with(WorldId::new)
101            .take(50)
102            .map(Option::unwrap)
103            .collect::<Vec<_>>();
104        for (i, &id1) in ids.iter().enumerate() {
105            // For the first element, i is 0 - so skip 1
106            for &id2 in ids.iter().skip(i + 1) {
107                assert_ne!(id1, id2, "WorldIds should not repeat");
108            }
109        }
110    }
111
112    #[test]
113    fn world_id_system_param() {
114        fn test_system(world_id: WorldId) -> WorldId {
115            world_id
116        }
117
118        let mut world = World::default();
119        let system_id = world.register_system(test_system);
120        let world_id = world.run_system(system_id).unwrap();
121        assert_eq!(world.id(), world_id);
122    }
123
124    #[test]
125    fn world_id_exclusive_system_param() {
126        fn test_system(_world: &mut World, world_id: WorldId) -> WorldId {
127            world_id
128        }
129
130        let mut world = World::default();
131        let system_id = world.register_system(test_system);
132        let world_id = world.run_system(system_id).unwrap();
133        assert_eq!(world.id(), world_id);
134    }
135
136    // We cannot use this test as-is, as it causes other tests to panic due to using the same atomic variable.
137    // #[test]
138    // #[should_panic]
139    // fn panic_on_overflow() {
140    //     MAX_WORLD_ID.store(usize::MAX - 50, Ordering::Relaxed);
141    //     core::iter::repeat_with(WorldId::new)
142    //         .take(500)
143    //         .for_each(|_| ());
144    // }
145}