Skip to main content

topcoat_core/context/
id.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3/// A unique identifier for a [`Cx`].
4///
5/// Every [`Cx`] is assigned a distinct `CxId` when it is created, making it
6/// cheap to compare and hash. Retrieve a context's id with [`Cx::id`].
7///
8/// [`Cx`]: crate::context::Cx
9/// [`Cx::id`]: crate::context::Cx::id
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct CxId(u64);
12
13impl CxId {
14    /// Returns a fresh `CxId` that is distinct from every previously issued ID.
15    pub(crate) fn new() -> Self {
16        static COUNTER: AtomicU64 = AtomicU64::new(0);
17        Self(COUNTER.fetch_add(1, Ordering::Relaxed))
18    }
19}
20
21impl Default for CxId {
22    fn default() -> Self {
23        Self::new()
24    }
25}