Path: blob/main/crates/bevy_render/src/render_resource/resource_macros.rs
6596 views
#[macro_export]1macro_rules! define_atomic_id {2($atomic_id_type:ident) => {3#[derive(Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)]4pub struct $atomic_id_type(core::num::NonZero<u32>);56impl $atomic_id_type {7#[expect(8clippy::new_without_default,9reason = "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."10)]11pub fn new() -> Self {12use core::sync::atomic::{AtomicU32, Ordering};1314static COUNTER: AtomicU32 = AtomicU32::new(1);1516let counter = COUNTER.fetch_add(1, Ordering::Relaxed);17Self(core::num::NonZero::<u32>::new(counter).unwrap_or_else(|| {18panic!(19"The system ran out of unique `{}`s.",20stringify!($atomic_id_type)21);22}))23}24}2526impl From<$atomic_id_type> for core::num::NonZero<u32> {27fn from(value: $atomic_id_type) -> Self {28value.029}30}3132impl From<core::num::NonZero<u32>> for $atomic_id_type {33fn from(value: core::num::NonZero<u32>) -> Self {34Self(value)35}36}37};38}394041