Skip to main content

samod_core/network/
dialer_id.rs

1use std::sync::atomic::{AtomicU32, Ordering};
2
3static LAST_DIALER_ID: AtomicU32 = AtomicU32::new(0);
4
5/// A unique identifier for a dialer in the samod-core system.
6///
7/// A dialer represents a persistent outgoing connection that automatically
8/// reconnects with backoff. Each dialer has at most one active connection
9/// at a time.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct DialerId(u32);
12
13impl DialerId {
14    pub(crate) fn new() -> Self {
15        let id = LAST_DIALER_ID.fetch_add(1, Ordering::SeqCst);
16        DialerId(id)
17    }
18}
19
20impl std::fmt::Display for DialerId {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        write!(f, "{}", self.0)
23    }
24}
25
26impl From<DialerId> for u32 {
27    fn from(id: DialerId) -> Self {
28        id.0
29    }
30}
31
32impl From<u32> for DialerId {
33    fn from(id: u32) -> Self {
34        DialerId(id)
35    }
36}