1#[cfg(test)]
4use std::sync::atomic::{AtomicU64, Ordering};
5
6use uuid::Uuid;
7
8pub trait IdGenerator: Send + Sync {
10 fn new_id(&self) -> Uuid;
11}
12
13#[derive(Debug, Default, Clone, Copy)]
15pub struct RandomIds;
16
17impl IdGenerator for RandomIds {
18 fn new_id(&self) -> Uuid {
19 Uuid::new_v4()
20 }
21}
22
23#[cfg(test)]
25#[derive(Debug, Default)]
26pub struct SequentialIds {
27 issued: AtomicU64,
28}
29
30#[cfg(test)]
31impl SequentialIds {
32 pub fn nth(n: u64) -> Uuid {
34 Uuid::from_u64_pair(0x0000_0000_0000_4000, 0x8000_0000_0000_0000 | n)
35 }
36}
37
38#[cfg(test)]
39impl IdGenerator for SequentialIds {
40 fn new_id(&self) -> Uuid {
41 let n = self.issued.fetch_add(1, Ordering::Relaxed) + 1;
42 Self::nth(n)
43 }
44}