websoc_kit/
connection_id.rs

1use std::fmt;
2
3use uuid::Uuid;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash)]
6pub struct ConnectionId(Uuid);
7
8impl Default for ConnectionId {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14impl ConnectionId {
15    #[must_use]
16    pub fn new() -> Self {
17        ConnectionId(Uuid::new_v4())
18    }
19
20    #[must_use]
21    pub fn as_bytes(&self) -> &[u8; 16] {
22        self.0.as_bytes()
23    }
24}
25
26impl From<Uuid> for ConnectionId {
27    fn from(uuid: Uuid) -> Self {
28        ConnectionId(uuid)
29    }
30}
31
32impl From<ConnectionId> for Uuid {
33    fn from(connection_id: ConnectionId) -> Self {
34        connection_id.0
35    }
36}
37
38impl fmt::Debug for ConnectionId {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        write!(f, "ConnectionId({})", self.0)
41    }
42}
43
44impl fmt::Display for ConnectionId {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{}", self.0.simple())
47    }
48}