⭐️ A friendly language for building type-safe, scalable systems!
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 40 lines 863 B view raw
1use std::sync::{ 2 Arc, 3 atomic::{AtomicU64, Ordering}, 4}; 5 6/// A generator of unique ids. Only one should be used per compilation run to 7/// ensure ids do not get reused. 8#[derive(Debug, Clone, Default)] 9pub struct UniqueIdGenerator { 10 id: Arc<AtomicU64>, 11} 12 13impl UniqueIdGenerator { 14 pub fn new() -> Self { 15 Self::default() 16 } 17 18 pub fn next(&self) -> u64 { 19 self.id.fetch_add(1, Ordering::Relaxed) 20 } 21} 22 23#[test] 24fn id_geneation() { 25 let ids = UniqueIdGenerator::new(); 26 let ids2 = ids.clone(); 27 28 assert_eq!(ids.next(), 0); 29 assert_eq!(ids.next(), 1); 30 assert_eq!(ids.next(), 2); 31 32 // Cloned ones use the same counter 33 assert_eq!(ids2.next(), 3); 34 assert_eq!(ids2.next(), 4); 35 assert_eq!(ids2.next(), 5); 36 37 // The original is updated 38 assert_eq!(ids.next(), 6); 39 assert_eq!(ids.next(), 7); 40}