use std::{
num::NonZeroU64,
sync::atomic::{AtomicU64, Ordering},
};
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Identifier {
inner: NonZeroU64,
}
impl Identifier {
pub fn create() -> Self {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
let inner = unsafe { NonZeroU64::new_unchecked(id) };
Self { inner }
}
}
macro_rules! impl_eq_ord_hash_proxy {
($type:ty => $($access:tt)*) => {
impl PartialEq for $type {
fn eq(&self, other: &Self) -> bool {
self $($access)* == other $($access)*
}
}
impl Eq for $type {}
impl PartialOrd for $type {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $type {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self $($access)*.cmp(&other $($access)*)
}
}
impl std::hash::Hash for $type {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self $($access)*.hash(state)
}
}
};
}
#[cfg_attr(not(wgpu_core), allow(unused_macros))]
macro_rules! impl_eq_ord_hash_arc_address {
($type:ty => $($access:tt)*) => {
impl PartialEq for $type {
fn eq(&self, other: &Self) -> bool {
let address_left = std::sync::Arc::as_ptr(&self $($access)*);
let address_right = std::sync::Arc::as_ptr(&other $($access)*);
address_left == address_right
}
}
impl Eq for $type {}
impl PartialOrd for $type {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $type {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let address_left = std::sync::Arc::as_ptr(&self $($access)*);
let address_right = std::sync::Arc::as_ptr(&other $($access)*);
address_left.cmp(&address_right)
}
}
impl std::hash::Hash for $type {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let address = std::sync::Arc::as_ptr(&self $($access)*);
address.hash(state)
}
}
};
}
#[cfg_attr(not(wgpu_core), allow(unused_imports))]
pub(crate) use {impl_eq_ord_hash_arc_address, impl_eq_ord_hash_proxy};