aboutsummaryrefslogtreecommitdiff
path: root/src/utils/id.rs
blob: 3f8a2a7d2f5e48683221f067e2ba96aa1d26680a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::sync::atomic::{AtomicU32, Ordering};

/// Counter that returns unique IDs.
///
/// Under the hood it uses a `u32` that will eventually wrap around. When incrementing it once a
/// second, it will wrap around after about 136 years.
pub struct IdCounter {
    value: AtomicU32,
}

impl IdCounter {
    pub const fn new() -> Self {
        Self {
            value: AtomicU32::new(0),
        }
    }

    pub fn next(&self) -> u32 {
        self.value.fetch_add(1, Ordering::SeqCst)
    }
}

impl Default for IdCounter {
    fn default() -> Self {
        Self::new()
    }
}