use std::fmt;
/// A generated id
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct UniqueId(u32);
impl UniqueId {
/// Generate a new, globally unique id
pub fn new() -> UniqueId {
use std::sync::atomic::{AtomicUsize, Ordering};
lazy_static! {
static ref NEXT_ID: AtomicUsize = AtomicUsize::new(0);
}
// FIXME: check for integer overflow
UniqueId(NEXT_ID.fetch_add(1, Ordering::SeqCst) as u32)
}
}
impl fmt::Display for UniqueId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}