use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct TensorId(u64);
impl TensorId {
#[inline]
pub fn new() -> Self {
Self(NEXT_ID.fetch_add(1, Ordering::Relaxed))
}
#[inline]
pub fn raw(self) -> u64 {
self.0
}
#[inline]
pub fn as_u64(self) -> u64 {
self.0
}
#[inline]
pub const fn from_raw(id: u64) -> Self {
Self(id)
}
}
impl Default for TensorId {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for TensorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Tensor({})", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unique_ids() {
let id1 = TensorId::new();
let id2 = TensorId::new();
let id3 = TensorId::new();
assert_ne!(id1, id2);
assert_ne!(id2, id3);
assert_ne!(id1, id3);
}
#[test]
fn test_id_incrementing() {
let id1 = TensorId::new();
let id2 = TensorId::new();
assert!(id2.raw() > id1.raw());
}
}