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        impl $atomic_id_type {
8            #[expect(
9                clippy::new_without_default,
10                reason = "Implementing the `Default` trait on atomic IDs would imply that two `<AtomicIdType>::default()` equal each other. By only implementing `new()`, we indicate that each atomic ID created will be unique."
11            )]
12            pub fn new() -> Self {
13                use core::sync::atomic::{AtomicU32, Ordering};
14
15                static COUNTER: AtomicU32 = AtomicU32::new(1);
16
17                let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
18                Self(core::num::NonZero::<u32>::new(counter).unwrap_or_else(|| {
19                    panic!(
20                        "The system ran out of unique `{}`s.",
21                        stringify!($atomic_id_type)
22                    );
23                }))
24            }
25        }
26
27        impl From<$atomic_id_type> for core::num::NonZero<u32> {
28            fn from(value: $atomic_id_type) -> Self {
29                value.0
30            }
31        }
32
33        impl From<core::num::NonZero<u32>> for $atomic_id_type {
34            fn from(value: core::num::NonZero<u32>) -> Self {
35                Self(value)
36            }
37        }
38    };
39}