Skip to main content

o_sfu_router/model/
ids.rs

1use std::fmt::{self, Display, Formatter};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub struct RouterId(pub u64);
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct ProducerId(pub u64);
8
9impl ProducerId {
10    #[must_use]
11    pub fn allocate(next: &mut u64) -> Self {
12        let id = Self(*next);
13        *next = next.saturating_add(1);
14        id
15    }
16}
17
18impl Display for ProducerId {
19    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
20        write!(formatter, "producer-{}", self.0)
21    }
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
25pub struct ConsumerId(pub u64);
26
27impl ConsumerId {
28    #[must_use]
29    pub fn allocate(next: &mut u64) -> Self {
30        let id = Self(*next);
31        *next = next.saturating_add(1);
32        id
33    }
34}
35
36impl Display for ConsumerId {
37    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
38        write!(formatter, "consumer-{}", self.0)
39    }
40}
41
42/// unique identifier for a user's transport connection within the server process
43///
44/// this separates the ephemeral transport lifecycle from the persistent logical
45/// user identity. a single user might create multiple connections over time due to
46/// network drops or handovers. this identifier ensures media operations only apply
47/// to the specific transport they were negotiated against
48#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
49pub struct ConnectionId(u64);
50
51impl ConnectionId {
52    #[must_use]
53    pub fn allocate(next_connection_id: &mut u64) -> Self {
54        let connection_id = Self(*next_connection_id);
55        *next_connection_id = next_connection_id.saturating_add(1);
56        connection_id
57    }
58
59    #[must_use]
60    pub const fn from_raw(raw: u64) -> Self {
61        Self(raw)
62    }
63
64    #[must_use]
65    pub const fn as_u64(self) -> u64 {
66        self.0
67    }
68}
69
70impl Display for ConnectionId {
71    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
72        self.0.fmt(formatter)
73    }
74}
75
76/// runtime-local identifier for one rtc media worker
77///
78/// this is worker identity, not a worker count or vector capacity
79/// convert to raw `usize` only when indexing worker storage or projecting
80/// telemetry and diagnostics fields
81#[repr(transparent)]
82#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
83pub struct MediaWorkerId(usize);
84
85impl MediaWorkerId {
86    #[must_use]
87    pub const fn from_raw(raw: usize) -> Self {
88        Self(raw)
89    }
90
91    #[must_use]
92    pub const fn as_usize(self) -> usize {
93        self.0
94    }
95}