use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Clone, Debug)]
pub struct Name<T> {
string: String,
index: usize,
_phantom: PhantomData<T>,
}
impl<T> Name<T> {
pub fn new(s: impl Into<String>) -> Self {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
Name {
string: s.into(),
index: COUNTER.fetch_add(1, Ordering::SeqCst),
_phantom: PhantomData,
}
}
pub fn with_index(s: impl Into<String>, index: usize) -> Self {
Name {
string: s.into(),
index,
_phantom: PhantomData,
}
}
pub fn string(&self) -> &str {
&self.string
}
pub fn index(&self) -> usize {
self.index
}
}
impl<T> fmt::Display for Name<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}@{}", self.string, self.index)
}
}
impl<T> PartialEq for Name<T> {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl<T> Eq for Name<T> {}
impl<T> Hash for Name<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state);
}
}