bevy_render/render_resource/
resource_macros.rs

1#[macro_export]
2macro_rules! define_atomic_id {
3    ($atomic_id_type:ident) => {
4        #[derive(Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)]
5        pub struct $atomic_id_type(core::num::NonZero<u32>);
6
7        // We use new instead of default to indicate that each ID created will be unique.
8        #[allow(clippy::new_without_default)]
9        impl $atomic_id_type {
10            pub fn new() -> Self {
11                use core::sync::atomic::{AtomicU32, Ordering};
12
13                static COUNTER: AtomicU32 = AtomicU32::new(1);
14
15                let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
16                Self(core::num::NonZero::<u32>::new(counter).unwrap_or_else(|| {
17                    panic!(
18                        "The system ran out of unique `{}`s.",
19                        stringify!($atomic_id_type)
20                    );
21                }))
22            }
23        }
24
25        impl From<$atomic_id_type> for core::num::NonZero<u32> {
26            fn from(value: $atomic_id_type) -> Self {
27                value.0
28            }
29        }
30
31        impl From<core::num::NonZero<u32>> for $atomic_id_type {
32            fn from(value: core::num::NonZero<u32>) -> Self {
33                Self(value)
34            }
35        }
36    };
37}