use iceoryx2_bb_concurrency::atomic::AtomicU64;
use iceoryx2_bb_concurrency::atomic::Ordering;
use core::marker::PhantomData;
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct UniqueId {
value: u64,
}
impl Default for UniqueId {
fn default() -> Self {
#[cfg(not(all(test, loom, feature = "std")))]
static COUNTER: AtomicU64 = AtomicU64::new(0);
#[cfg(all(test, loom, feature = "std"))]
static COUNTER: std::sync::LazyLock<IoxAtomicU64> = std::sync::LazyLock::new(|| {
unimplemented!("loom does not provide const-initialization for atomic variables.")
});
UniqueId {
value: COUNTER.fetch_add(1, Ordering::Relaxed),
}
}
}
impl UniqueId {
pub fn new() -> Self {
Self::default()
}
pub fn value(&self) -> u64 {
self.value
}
}
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct TypedUniqueId<T> {
value: u64,
_phantom: PhantomData<T>,
}
impl<T> Default for TypedUniqueId<T> {
fn default() -> Self {
#[cfg(not(all(test, loom, feature = "std")))]
static COUNTER: AtomicU64 = AtomicU64::new(0);
#[cfg(all(test, loom, feature = "std"))]
static COUNTER: std::sync::LazyLock<IoxAtomicU64> = std::sync::LazyLock::new(|| {
unimplemented!("loom does not provide const-initialization for atomic variables.")
});
Self {
value: COUNTER.fetch_add(1, Ordering::Relaxed),
_phantom: PhantomData,
}
}
}
impl<T> TypedUniqueId<T> {
pub fn new() -> Self {
Self::default()
}
pub fn value(&self) -> u64 {
self.value
}
}