use std::{
num::NonZeroUsize,
sync::atomic::{AtomicUsize, Ordering},
};
const ZERO_NODE_ID_VALUE: usize = 0;
static LAST_NODE_ID_VALUE: AtomicUsize = AtomicUsize::new(ZERO_NODE_ID_VALUE);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::Display)]
pub struct NodeId(NonZeroUsize);
impl NodeId {
#[allow(clippy::new_without_default)] pub fn new() -> Self {
loop {
let last_value = LAST_NODE_ID_VALUE.fetch_add(1, Ordering::Relaxed);
let next_value = last_value.wrapping_add(1);
if let Some(next_value) = NonZeroUsize::new(next_value) {
return Self(next_value);
}
}
}
}