1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! A helper to predict the encoded length
use crate::anyvec::AnyVec;
/// A helper to predict the encoded length
#[derive(Debug, Clone, Copy, Default)]
pub struct Length {
/// The accumulated length
len: usize,
}
impl Length {
/// Creates a new, all-zero length
pub const fn new() -> Self {
Self { len: 0 }
}
/// Writes a `u8`
///
/// # Panics
/// This function panics if the total accumulated length is greater than `usize::MAX`.
pub fn u8(mut self, _u8: &u8) -> Self {
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(1).expect("accumulated length is too large"));
self
}
/// Writes a `u16`
///
/// # Panics
/// This function panics if the total accumulated length is greater than `usize::MAX`.
pub fn u16(mut self, _u16: &u16) -> Self {
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(2).expect("accumulated length is too large"));
self
}
/// Writes some raw data as-is
///
/// # Panics
/// This function panics if the total accumulated length is greater than `usize::MAX`.
pub fn raw<T>(mut self, raw: &T) -> Self
where
T: AsRef<[u8]> + IntoIterator<Item = u8>,
{
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(raw.as_ref().len()).expect("accumulated length is too large"));
self
}
/// Writes a length-prefixed byte field
///
/// # Panics
/// This function panics if the length of the byte field is greater than `u16::MAX`. This function also panics if
/// the total accumulated length is greater than `usize::MAX`.
pub fn bytes<T>(mut self, bytes: &T) -> Self
where
T: AsRef<[u8]> + IntoIterator<Item = u8>,
{
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = (self.len.checked_add(2))
.and_then(|len| len.checked_add(bytes.as_ref().len()))
.expect("accumulated length is too large"));
self
}
/// Writes a bitmap as byte
///
/// # Panics
/// This function panics if the total accumulated length is greater than `usize::MAX`.
pub fn bitmap(mut self, _bits: &[bool; 8]) -> Self {
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(1).expect("accumulated length is too large"));
self
}
/// Writes a packet type and associated flags (as bitmap) as header byte
///
/// # Panics
/// This function panics if the packet type is greater than `15` (`2^4 - 1`). This function also panics if the total
/// accumulated length is greater than `usize::MAX`.
pub fn header(mut self, type_: &u8, _flags: &[bool; 4]) -> Self {
// Validate type value
#[allow(clippy::panic, reason = "serious API misuse")]
(assert!(*type_ <= 15, "packet type is too large"));
// Accumulate length
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(1).expect("accumulated length is too large"));
self
}
/// Writes a packet length field
///
/// # Panics
/// This function panics if the packet length is greater than `2^28 - 1`. This function also panics if the total
/// accumulated length is greater than `usize::MAX`.
pub fn packetlen(mut self, len: &usize) -> Self {
// Validate and compute packet length size
#[allow(clippy::panic, reason = "packet length must be encoded in 4 or less heptets")]
#[allow(clippy::unusual_byte_groupings, reason = "length bytes are encoded in heptets")]
let len_size = match len {
0b1_0000000_0000000_0000000_0000000.. => panic!("packet length is too large"),
0b1_0000000_0000000_0000000.. => 4,
0b1_0000000_0000000.. => 3,
0b1_0000000.. => 2,
_ => 1,
};
// Accumulate length
#[allow(clippy::expect_used, reason = "serious API misuse")]
(self.len = self.len.checked_add(len_size).expect("accumulated length is too large"));
self
}
/// Writes an optional `u16`
///
/// # Panics
/// This function panics if the total accumulated length is greater than `usize::MAX`.
pub fn optional_u16(self, u16_: &Option<u16>) -> Self {
match u16_ {
Some(u16_) => self.u16(u16_),
None => self,
}
}
/// Writes an optional length-prefixed byte field
///
/// # Panics
/// This function panics if the length of the byte field is greater than `u16::MAX`. This function also panics if
/// the total accumulated length is greater than `usize::MAX`.
pub fn optional_bytes<T>(self, bytes: &Option<T>) -> Self
where
T: AnyVec<u8>,
{
match bytes {
Some(bytes) => self.bytes(bytes),
None => self,
}
}
/// Writes a sequence of topic+quality-of-service tuples
///
/// # Panics
/// This function panics if the length of a topic is greater than `u16::MAX`. This function also panics if the total
/// accumulated length is greater than `usize::MAX`.
pub fn topics<S, T>(mut self, topics: &S) -> Self
where
S: AsRef<[T]>,
T: AsRef<[u8]> + IntoIterator<Item = u8>,
{
// Sum-up all topics
for topic in topics.as_ref() {
// Topics are just concatenated
self = self.bytes(topic);
}
self
}
/// Writes a sequence of topic+quality-of-service tuples
///
/// # Panics
/// This function panics if the length of a topic is greater than `u16::MAX`. This function also panics if the total
/// accumulated length is greater than `usize::MAX`.
pub fn topics_qos<S, T>(mut self, topics_qos: &S) -> Self
where
S: AsRef<[(T, u8)]>,
T: AsRef<[u8]> + IntoIterator<Item = u8>,
{
// Sum-up all tuples
for (topic, qos) in topics_qos.as_ref() {
// Topic+QoS tubles are just concatenated
self = self.bytes(topic);
self = self.u8(qos);
}
self
}
}
impl From<Length> for usize {
fn from(value: Length) -> Self {
value.len
}
}