Skip to content

Commit 5a5ec08

Browse files
committed
add u128 and i128 support
1 parent db7bb23 commit 5a5ec08

File tree

12 files changed

+316
-175
lines changed

12 files changed

+316
-175
lines changed

msgpacker/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "msgpacker"
3-
version = "0.4.2"
3+
version = "0.4.3"
44
authors = ["Victor Lopez <[email protected]>"]
55
categories = ["compression", "encoding", "parser-implementations"]
66
edition = "2021"

msgpacker/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum Error {
1313
InvalidUtf8,
1414
/// The protocol format tag is not valid.
1515
UnexpectedFormatTag,
16+
/// The provided bin length is not valid.
17+
UnexpectedBinLength,
1618
}
1719

1820
impl fmt::Display for Error {

msgpacker/src/extension.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ mod tests {
305305

306306
proptest! {
307307
#[test]
308-
fn extension_bytes(t: i8, b: Vec<u8>) {
308+
fn extension_bytes(mut t: i8, b: Vec<u8>) {
309+
if t == Extension::TIMESTAMP {
310+
t -= 1;
311+
}
309312
let x = Extension::Ext(t, b);
310313
let mut bytes = vec![];
311314
x.pack(&mut bytes);

msgpacker/src/helpers.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ pub fn take_byte(buf: &mut &[u8]) -> Result<u8, Error> {
1616
Ok(l[0])
1717
}
1818

19-
pub fn take_num_iter<I, V, const N: usize>(bytes: I, f: fn([u8; N]) -> V) -> Result<V, Error>
20-
where
21-
I: Iterator<Item = u8>,
22-
{
23-
bytes
24-
.array_chunks()
25-
.next()
26-
.ok_or(Error::BufferTooShort)
27-
.map(f)
28-
}
29-
3019
pub fn take_num<V, const N: usize>(buf: &mut &[u8], f: fn([u8; N]) -> V) -> Result<V, Error> {
3120
if buf.len() < N {
3221
return Err(Error::BufferTooShort);
@@ -47,6 +36,17 @@ pub fn take_buffer<'a>(buf: &mut &'a [u8], len: usize) -> Result<&'a [u8], Error
4736
Ok(l)
4837
}
4938

39+
pub fn take_num_iter<I, V, const N: usize>(bytes: I, f: fn([u8; N]) -> V) -> Result<V, Error>
40+
where
41+
I: Iterator<Item = u8>,
42+
{
43+
bytes
44+
.array_chunks()
45+
.next()
46+
.ok_or(Error::BufferTooShort)
47+
.map(f)
48+
}
49+
5050
#[cfg(feature = "alloc")]
5151
pub fn take_buffer_iter<I>(bytes: I, len: usize) -> Result<alloc::vec::Vec<u8>, Error>
5252
where

msgpacker/src/pack/int.rs

+94-23
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ impl Packable for u32 {
4646
buf.extend(iter::once(Format::UINT8).chain(iter::once(*self as u8)));
4747
2
4848
} else if *self <= u16::MAX as u32 {
49-
buf.extend(
50-
iter::once(Format::UINT16).chain(self.to_be_bytes().iter().skip(2).copied()),
51-
);
49+
buf.extend(iter::once(Format::UINT16).chain((*self as u16).to_be_bytes()));
5250
3
5351
} else {
5452
buf.extend(iter::once(Format::UINT32).chain(self.to_be_bytes()));
@@ -69,14 +67,10 @@ impl Packable for u64 {
6967
buf.extend(iter::once(Format::UINT8).chain(iter::once(*self as u8)));
7068
2
7169
} else if *self <= u16::MAX as u64 {
72-
buf.extend(
73-
iter::once(Format::UINT16).chain(self.to_be_bytes().iter().skip(6).copied()),
74-
);
70+
buf.extend(iter::once(Format::UINT16).chain((*self as u16).to_be_bytes()));
7571
3
7672
} else if *self <= u32::MAX as u64 {
77-
buf.extend(
78-
iter::once(Format::UINT32).chain(self.to_be_bytes().iter().skip(4).copied()),
79-
);
73+
buf.extend(iter::once(Format::UINT32).chain((*self as u32).to_be_bytes()));
8074
5
8175
} else {
8276
buf.extend(iter::once(Format::UINT64).chain(self.to_be_bytes()));
@@ -85,6 +79,37 @@ impl Packable for u64 {
8579
}
8680
}
8781

82+
impl Packable for u128 {
83+
fn pack<T>(&self, buf: &mut T) -> usize
84+
where
85+
T: Extend<u8>,
86+
{
87+
if *self <= 127 {
88+
buf.extend(iter::once(*self as u8 & Format::POSITIVE_FIXINT));
89+
1
90+
} else if *self <= u8::MAX as u128 {
91+
buf.extend(iter::once(Format::UINT8).chain(iter::once(*self as u8)));
92+
2
93+
} else if *self <= u16::MAX as u128 {
94+
buf.extend(iter::once(Format::UINT16).chain((*self as u16).to_be_bytes()));
95+
3
96+
} else if *self <= u32::MAX as u128 {
97+
buf.extend(iter::once(Format::UINT32).chain((*self as u32).to_be_bytes()));
98+
5
99+
} else if *self <= u64::MAX as u128 {
100+
buf.extend(iter::once(Format::UINT64).chain((*self as u64).to_be_bytes()));
101+
9
102+
} else {
103+
buf.extend(
104+
iter::once(Format::BIN8)
105+
.chain(iter::once(16))
106+
.chain(self.to_be_bytes()),
107+
);
108+
18
109+
}
110+
}
111+
}
112+
88113
impl Packable for usize {
89114
fn pack<T>(&self, buf: &mut T) -> usize
90115
where
@@ -97,14 +122,10 @@ impl Packable for usize {
97122
buf.extend(iter::once(Format::UINT8).chain(iter::once(*self as u8)));
98123
2
99124
} else if *self <= u16::MAX as usize {
100-
buf.extend(
101-
iter::once(Format::UINT16).chain(self.to_be_bytes().iter().skip(6).copied()),
102-
);
125+
buf.extend(iter::once(Format::UINT16).chain((*self as u16).to_be_bytes()));
103126
3
104127
} else if *self <= u32::MAX as usize {
105-
buf.extend(
106-
iter::once(Format::UINT32).chain(self.to_be_bytes().iter().skip(4).copied()),
107-
);
128+
buf.extend(iter::once(Format::UINT32).chain((*self as u32).to_be_bytes()));
108129
5
109130
} else {
110131
buf.extend(iter::once(Format::UINT64).chain(self.to_be_bytes()));
@@ -138,7 +159,7 @@ impl Packable for i16 {
138159
{
139160
if *self < i8::MIN as i16 {
140161
buf.extend(iter::once(Format::INT16).chain(self.to_be_bytes()));
141-
2
162+
3
142163
} else if *self <= -33 {
143164
buf.extend(iter::once(Format::INT8).chain(iter::once((*self as i8) as u8)));
144165
2
@@ -150,7 +171,7 @@ impl Packable for i16 {
150171
1
151172
} else {
152173
buf.extend(iter::once(Format::INT16).chain(self.to_be_bytes()));
153-
2
174+
3
154175
}
155176
}
156177
}
@@ -165,7 +186,7 @@ impl Packable for i32 {
165186
5
166187
} else if *self < i8::MIN as i32 {
167188
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
168-
2
189+
3
169190
} else if *self <= -33 {
170191
buf.extend(iter::once(Format::INT8).chain(iter::once((*self as i8) as u8)));
171192
2
@@ -177,7 +198,7 @@ impl Packable for i32 {
177198
1
178199
} else if *self <= i16::MAX as i32 {
179200
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
180-
2
201+
3
181202
} else {
182203
buf.extend(iter::once(Format::INT32).chain(self.to_be_bytes()));
183204
5
@@ -198,7 +219,7 @@ impl Packable for i64 {
198219
5
199220
} else if *self < i8::MIN as i64 {
200221
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
201-
2
222+
3
202223
} else if *self <= -33 {
203224
buf.extend(iter::once(Format::INT8).chain(iter::once((*self as i8) as u8)));
204225
2
@@ -210,7 +231,7 @@ impl Packable for i64 {
210231
1
211232
} else if *self <= i16::MAX as i64 {
212233
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
213-
2
234+
3
214235
} else if *self <= i32::MAX as i64 {
215236
buf.extend(iter::once(Format::INT32).chain((*self as i32).to_be_bytes()));
216237
5
@@ -221,6 +242,56 @@ impl Packable for i64 {
221242
}
222243
}
223244

245+
impl Packable for i128 {
246+
fn pack<T>(&self, buf: &mut T) -> usize
247+
where
248+
T: Extend<u8>,
249+
{
250+
if *self < i64::MIN as i128 {
251+
buf.extend(
252+
iter::once(Format::BIN8)
253+
.chain(iter::once(16))
254+
.chain(self.to_be_bytes()),
255+
);
256+
18
257+
} else if *self < i32::MIN as i128 {
258+
buf.extend(iter::once(Format::INT64).chain((*self as i64).to_be_bytes()));
259+
9
260+
} else if *self < i16::MIN as i128 {
261+
buf.extend(iter::once(Format::INT32).chain((*self as i32).to_be_bytes()));
262+
5
263+
} else if *self < i8::MIN as i128 {
264+
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
265+
3
266+
} else if *self <= -33 {
267+
buf.extend(iter::once(Format::INT8).chain(iter::once((*self as i8) as u8)));
268+
2
269+
} else if *self <= -1 {
270+
buf.extend(iter::once((*self | -32i128) as u8));
271+
1
272+
} else if *self <= i8::MAX as i128 {
273+
buf.extend(iter::once(*self as u8 & Format::POSITIVE_FIXINT));
274+
1
275+
} else if *self <= i16::MAX as i128 {
276+
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
277+
3
278+
} else if *self <= i32::MAX as i128 {
279+
buf.extend(iter::once(Format::INT32).chain((*self as i32).to_be_bytes()));
280+
5
281+
} else if *self <= i64::MAX as i128 {
282+
buf.extend(iter::once(Format::INT64).chain((*self as i64).to_be_bytes()));
283+
9
284+
} else {
285+
buf.extend(
286+
iter::once(Format::BIN8)
287+
.chain(iter::once(16))
288+
.chain(self.to_be_bytes()),
289+
);
290+
18
291+
}
292+
}
293+
}
294+
224295
impl Packable for isize {
225296
fn pack<T>(&self, buf: &mut T) -> usize
226297
where
@@ -234,7 +305,7 @@ impl Packable for isize {
234305
5
235306
} else if *self < i8::MIN as isize {
236307
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
237-
2
308+
3
238309
} else if *self <= -33 {
239310
buf.extend(iter::once(Format::INT8).chain(iter::once((*self as i8) as u8)));
240311
2
@@ -246,7 +317,7 @@ impl Packable for isize {
246317
1
247318
} else if *self <= i16::MAX as isize {
248319
buf.extend(iter::once(Format::INT16).chain((*self as i16).to_be_bytes()));
249-
2
320+
3
250321
} else if *self <= i32::MAX as isize {
251322
buf.extend(iter::once(Format::INT32).chain((*self as i32).to_be_bytes()));
252323
5

0 commit comments

Comments
 (0)