Skip to main content

dope_sys/io/socket/route/
selector.rs

1use std::num;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4#[repr(transparent)]
5pub struct Offset(u8);
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8#[repr(transparent)]
9pub struct Mask(num::NonZero<u8>);
10
11/// Minimum packet payload extent required by one selector branch.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13#[repr(transparent)]
14pub struct Minimum(num::NonZero<u8>);
15
16/// Packet byte interpreted as the selected shape's declared length.
17#[derive(Clone, Copy, Debug, Eq, PartialEq)]
18#[repr(transparent)]
19pub struct Length(u8);
20
21/// Selects one dense route byte from either of two packet shapes.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub struct Selector {
24    branch_at: Offset,
25    branch_mask: Mask,
26    clear_key: Offset,
27    clear_min: Minimum,
28    length_at: Offset,
29    length: Length,
30    set_key: Offset,
31    set_min: Minimum,
32}
33
34impl Offset {
35    const fn new(offset: u8) -> Self {
36        Self(offset)
37    }
38
39    pub const fn get(self) -> u8 {
40        self.0
41    }
42}
43
44impl Mask {
45    const fn new<const MASK: u8>() -> Self {
46        const {
47            assert!(MASK > 0);
48        }
49        Self(num::NonZero::<u8>::MIN.saturating_add(MASK - 1))
50    }
51
52    pub const fn get(self) -> num::NonZero<u8> {
53        self.0
54    }
55}
56
57impl Minimum {
58    const fn new<const BYTES: u8>() -> Self {
59        const {
60            assert!(BYTES > 0);
61        }
62        Self(num::NonZero::<u8>::MIN.saturating_add(BYTES - 1))
63    }
64
65    pub const fn get(self) -> num::NonZero<u8> {
66        self.0
67    }
68}
69
70impl Length {
71    pub(crate) const fn new(bytes: u8) -> Self {
72        Self(bytes)
73    }
74
75    pub const fn get(self) -> u8 {
76        self.0
77    }
78}
79
80impl Selector {
81    pub const fn fixed<
82        const BRANCH_AT: u8,
83        const BRANCH_MASK: u8,
84        const CLEAR_KEY: u8,
85        const CLEAR_MIN: u8,
86        const LENGTH_AT: u8,
87        const LENGTH: u8,
88        const SET_KEY: u8,
89        const SET_MIN: u8,
90    >() -> Self {
91        const {
92            assert!(BRANCH_AT < CLEAR_MIN);
93            assert!(CLEAR_KEY < CLEAR_MIN);
94            assert!(BRANCH_AT < SET_MIN);
95            assert!(LENGTH_AT < SET_MIN);
96            assert!(SET_KEY < SET_MIN);
97            assert!(CLEAR_MIN <= SET_MIN);
98        }
99        Self {
100            branch_at: Offset::new(BRANCH_AT),
101            branch_mask: Mask::new::<BRANCH_MASK>(),
102            clear_key: Offset::new(CLEAR_KEY),
103            clear_min: Minimum::new::<CLEAR_MIN>(),
104            length_at: Offset::new(LENGTH_AT),
105            length: Length::new(LENGTH),
106            set_key: Offset::new(SET_KEY),
107            set_min: Minimum::new::<SET_MIN>(),
108        }
109    }
110
111    pub const fn branch_at(self) -> Offset {
112        self.branch_at
113    }
114
115    pub const fn branch_mask(self) -> Mask {
116        self.branch_mask
117    }
118
119    pub const fn clear_key(self) -> Offset {
120        self.clear_key
121    }
122
123    pub const fn clear_min(self) -> Minimum {
124        self.clear_min
125    }
126
127    pub const fn length_at(self) -> Offset {
128        self.length_at
129    }
130
131    pub const fn length(self) -> Length {
132        self.length
133    }
134
135    pub const fn set_key(self) -> Offset {
136        self.set_key
137    }
138
139    pub const fn set_min(self) -> Minimum {
140        self.set_min
141    }
142}
143
144const _: () = {
145    assert!(std::mem::size_of::<Selector>() == 8);
146    assert!(std::mem::align_of::<Selector>() == 1);
147};