aboutsummaryrefslogtreecommitdiff
path: root/src/utils/id.rs
blob: c21511e6d2d66fd7567f2be85cc080e2e2d7db29 (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
use std::sync::atomic::{AtomicU64, Ordering};

/// Counter that returns unique IDs.
pub struct IdCounter {
    value: AtomicU64,
}

impl IdCounter {
    pub const fn new() -> Self {
        Self {
            // Start from 1 to reduce the possibility that some other code that uses these IDs will
            // get confused.
            value: AtomicU64::new(1),
        }
    }

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

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