bevy_ecs/world/
identifier.rs1use 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)]
12pub struct WorldId(usize);
19
20static MAX_WORLD_ID: AtomicUsize = AtomicUsize::new(0);
22
23impl WorldId {
24 pub fn new() -> Option<Self> {
30 MAX_WORLD_ID
31 .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
47unsafe impl ReadOnlySystemParam for WorldId {}
49
50unsafe 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 &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 }