use std::sync::atomic::{AtomicU64, Ordering};
pub trait IdGenerator {
fn uuid(&self) -> String;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct RandomIds;
impl IdGenerator for RandomIds {
fn uuid(&self) -> String {
uuid::Uuid::new_v4().to_string()
}
}
#[derive(Debug, Default)]
pub struct SequentialIds {
next: AtomicU64,
}
impl SequentialIds {
pub fn new() -> Self {
Self::default()
}
}
impl IdGenerator for SequentialIds {
fn uuid(&self) -> String {
let n = self.next.fetch_add(1, Ordering::Relaxed) + 1;
uuid::Uuid::from_u128(u128::from(n)).to_string()
}
}