Skip to main content

swink_agent/
agent_id.rs

1//! Standalone module for [`AgentId`] to avoid circular imports between
2//! `agent` and `registry`.
3
4use std::fmt;
5use std::sync::atomic::{AtomicU64, Ordering};
6
7/// Unique identifier assigned to every [`crate::Agent`] on construction.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct AgentId(u64);
10
11impl AgentId {
12    pub(crate) fn next() -> Self {
13        static COUNTER: AtomicU64 = AtomicU64::new(1);
14        Self(COUNTER.fetch_add(1, Ordering::Relaxed))
15    }
16}
17
18impl fmt::Display for AgentId {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "AgentId({})", self.0)
21    }
22}