1use std::sync::atomic::{AtomicU64, Ordering};
5
6pub trait IdGenerator {
8 fn uuid(&self) -> String;
10}
11
12#[derive(Debug, Default, Clone, Copy)]
14pub struct RandomIds;
15
16impl IdGenerator for RandomIds {
17 fn uuid(&self) -> String {
18 uuid::Uuid::new_v4().to_string()
19 }
20}
21
22#[derive(Debug, Default)]
25pub struct SequentialIds {
26 next: AtomicU64,
27}
28
29impl SequentialIds {
30 pub fn new() -> Self {
31 Self::default()
32 }
33}
34
35impl IdGenerator for SequentialIds {
36 fn uuid(&self) -> String {
37 let n = self.next.fetch_add(1, Ordering::Relaxed) + 1;
38 uuid::Uuid::from_u128(u128::from(n)).to_string()
39 }
40}