Skip to main content

samod_core/network/
connection_id.rs

1use std::sync::atomic::{AtomicU32, Ordering};
2
3static LAST_CONNECTION_ID: AtomicU32 = AtomicU32::new(0);
4
5/// A unique identifier for network connections in the samod-core system.
6///
7/// `ConnectionId` represents a communication channel that can be used for sending
8/// and receiving messages. Each connection has a unique identifier that is automatically
9/// generated when the connection is created.
10///
11/// ## Usage
12///
13/// Connections are created through the `CreateConnection` command and are used to scope
14/// network operations. All send and receive operations specify which connection
15/// they operate on.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct ConnectionId(u32);
18
19impl ConnectionId {
20    pub(crate) fn new() -> Self {
21        let id = LAST_CONNECTION_ID.fetch_add(1, Ordering::SeqCst);
22        ConnectionId(id)
23    }
24}
25
26impl From<ConnectionId> for u32 {
27    fn from(id: ConnectionId) -> Self {
28        id.0
29    }
30}
31
32impl From<u32> for ConnectionId {
33    fn from(id: u32) -> Self {
34        ConnectionId(id)
35    }
36}