Skip to main content

leodos_protocols/
ids.rs

1/// Protocol identifier newtypes for type-safe ID handling.
2///
3/// Each type wraps `u32`. Validation (bit-width checks) is
4/// performed at point of use, not at construction.
5
6/// Spacecraft Identifier.
7#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
8#[repr(transparent)]
9pub struct Scid(u32);
10
11impl Scid {
12    /// Creates a new spacecraft ID.
13    pub const fn new(id: u32) -> Self {
14        Self(id)
15    }
16
17    /// Returns the raw value.
18    pub const fn get(self) -> u32 {
19        self.0
20    }
21
22    /// Minimum number of bits needed to represent this value.
23    pub const fn num_bits(self) -> u32 {
24        u32::BITS - self.0.leading_zeros()
25    }
26}
27
28impl core::fmt::Display for Scid {
29    fn fmt(
30        &self,
31        f: &mut core::fmt::Formatter<'_>,
32    ) -> core::fmt::Result {
33        write!(f, "{}", self.0)
34    }
35}
36
37/// Virtual Channel Identifier.
38#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
39#[repr(transparent)]
40pub struct Vcid(u32);
41
42impl Vcid {
43    /// Creates a new virtual channel ID.
44    pub const fn new(id: u32) -> Self {
45        Self(id)
46    }
47
48    /// Returns the raw value.
49    pub const fn get(self) -> u32 {
50        self.0
51    }
52
53    /// Minimum number of bits needed to represent this value.
54    pub const fn num_bits(self) -> u32 {
55        u32::BITS - self.0.leading_zeros()
56    }
57}
58
59impl core::fmt::Display for Vcid {
60    fn fmt(
61        &self,
62        f: &mut core::fmt::Formatter<'_>,
63    ) -> core::fmt::Result {
64        write!(f, "{}", self.0)
65    }
66}