Skip to content

Commit 1d19c4f

Browse files
committed
fix no-std usage of vec without alloc
1 parent cf3c35d commit 1d19c4f

File tree

6 files changed

+82
-76
lines changed

6 files changed

+82
-76
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.4"
3+
version = "0.4.5"
44
authors = ["Victor Lopez <[email protected]>"]
55
categories = ["compression", "encoding", "parser-implementations"]
66
edition = "2021"

msgpacker/src/format.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ impl Format {
2121
pub const STR8: u8 = 0xd9;
2222
pub const STR16: u8 = 0xda;
2323
pub const STR32: u8 = 0xdb;
24+
pub const ARRAY16: u8 = 0xdc;
25+
pub const ARRAY32: u8 = 0xdd;
26+
pub const MAP16: u8 = 0xde;
27+
pub const MAP32: u8 = 0xdf;
28+
}
29+
30+
#[cfg(feature = "alloc")]
31+
impl Format {
2432
pub const FIXEXT1: u8 = 0xd4;
2533
pub const FIXEXT2: u8 = 0xd5;
2634
pub const FIXEXT4: u8 = 0xd6;
@@ -29,8 +37,4 @@ impl Format {
2937
pub const EXT8: u8 = 0xc7;
3038
pub const EXT16: u8 = 0xc8;
3139
pub const EXT32: u8 = 0xc9;
32-
pub const ARRAY16: u8 = 0xdc;
33-
pub const ARRAY32: u8 = 0xdd;
34-
pub const MAP16: u8 = 0xde;
35-
pub const MAP32: u8 = 0xdf;
3640
}

msgpacker/src/helpers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn take_num<V, const N: usize>(buf: &mut &[u8], f: fn([u8; N]) -> V) -> Resu
2727
Ok(f(val))
2828
}
2929

30+
#[cfg(feature = "alloc")]
3031
pub fn take_buffer<'a>(buf: &mut &'a [u8], len: usize) -> Result<&'a [u8], Error> {
3132
if buf.len() < len {
3233
return Err(Error::BufferTooShort);

msgpacker/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ pub use unpack::{unpack_array, unpack_array_iter, unpack_map, unpack_map_iter};
2727
#[cfg(feature = "alloc")]
2828
pub use extension::Extension;
2929

30+
#[cfg(feature = "alloc")]
31+
use alloc::vec::Vec;
32+
3033
#[cfg(feature = "derive")]
3134
pub use msgpacker_derive::MsgPacker;
3235

3336
/// Packs the provided packable value into a vector.
37+
#[cfg(feature = "alloc")]
3438
pub fn pack_to_vec<T>(value: &T) -> Vec<u8>
3539
where
3640
T: Packable,
3741
{
38-
let mut bytes = Vec::new();
39-
40-
value.pack(&mut bytes);
41-
42-
bytes
42+
value.pack_to_vec()
4343
}
4444

4545
/// A packable type.
@@ -50,7 +50,8 @@ pub trait Packable {
5050
T: Extend<u8>;
5151

5252
/// Packs the value into a vector of bytes.
53-
fn pack_to_vec<T>(&self) -> Vec<u8> {
53+
#[cfg(feature = "alloc")]
54+
fn pack_to_vec(&self) -> Vec<u8> {
5455
let mut bytes = Vec::new();
5556

5657
self.pack(&mut bytes);

msgpacker/src/unpack/binary.rs

+64-65
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
use crate::{
2+
helpers::{take_byte_iter, take_num_iter},
3+
Unpackable,
4+
};
5+
16
use super::{
27
helpers::{take_byte, take_num},
3-
Error, Format, Unpackable,
8+
Error, Format,
49
};
10+
use alloc::{string::String, vec::Vec};
511
use core::str;
612

713
pub fn unpack_bytes(mut buf: &[u8]) -> Result<(usize, &[u8]), Error> {
@@ -34,77 +40,70 @@ pub fn unpack_str(mut buf: &[u8]) -> Result<(usize, &str), Error> {
3440
Ok((n + len, str))
3541
}
3642

37-
#[cfg(feature = "alloc")]
38-
mod alloc {
39-
use super::*;
40-
use crate::helpers::{take_byte_iter, take_num_iter};
41-
use ::alloc::{string::String, vec::Vec};
43+
impl Unpackable for Vec<u8> {
44+
type Error = Error;
4245

43-
impl Unpackable for Vec<u8> {
44-
type Error = Error;
45-
46-
fn unpack(buf: &[u8]) -> Result<(usize, Self), Self::Error> {
47-
unpack_bytes(buf).map(|(n, b)| (n, b.to_vec()))
48-
}
46+
fn unpack(buf: &[u8]) -> Result<(usize, Self), Self::Error> {
47+
unpack_bytes(buf).map(|(n, b)| (n, b.to_vec()))
48+
}
4949

50-
fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
51-
where
52-
I: IntoIterator<Item = u8>,
53-
{
54-
let mut bytes = bytes.into_iter();
55-
let format = take_byte_iter(bytes.by_ref())?;
56-
let (n, len) = match format {
57-
Format::BIN8 => (2, take_byte_iter(bytes.by_ref())? as usize),
58-
Format::BIN16 => (
59-
3,
60-
take_num_iter(bytes.by_ref(), u16::from_be_bytes)? as usize,
61-
),
62-
Format::BIN32 => (
63-
5,
64-
take_num_iter(bytes.by_ref(), u32::from_be_bytes)? as usize,
65-
),
66-
_ => return Err(Error::UnexpectedFormatTag),
67-
};
68-
let v: Vec<_> = bytes.take(len).collect();
69-
if v.len() < len {
70-
return Err(Error::BufferTooShort);
71-
}
72-
Ok((n + len, v))
50+
fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
51+
where
52+
I: IntoIterator<Item = u8>,
53+
{
54+
let mut bytes = bytes.into_iter();
55+
let format = take_byte_iter(bytes.by_ref())?;
56+
let (n, len) = match format {
57+
Format::BIN8 => (2, take_byte_iter(bytes.by_ref())? as usize),
58+
Format::BIN16 => (
59+
3,
60+
take_num_iter(bytes.by_ref(), u16::from_be_bytes)? as usize,
61+
),
62+
Format::BIN32 => (
63+
5,
64+
take_num_iter(bytes.by_ref(), u32::from_be_bytes)? as usize,
65+
),
66+
_ => return Err(Error::UnexpectedFormatTag),
67+
};
68+
let v: Vec<_> = bytes.take(len).collect();
69+
if v.len() < len {
70+
return Err(Error::BufferTooShort);
7371
}
72+
Ok((n + len, v))
7473
}
74+
}
7575

76-
impl Unpackable for String {
77-
type Error = Error;
76+
impl Unpackable for String {
77+
type Error = Error;
7878

79-
fn unpack(buf: &[u8]) -> Result<(usize, Self), Self::Error> {
80-
unpack_str(buf).map(|(n, s)| (n, s.into()))
81-
}
79+
fn unpack(buf: &[u8]) -> Result<(usize, Self), Self::Error> {
80+
unpack_str(buf).map(|(n, s)| (n, s.into()))
81+
}
8282

83-
fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
84-
where
85-
I: IntoIterator<Item = u8>,
86-
{
87-
let mut bytes = bytes.into_iter();
88-
let format = take_byte_iter(bytes.by_ref())?;
89-
let (n, len) = match format {
90-
0xa0..=0xbf => (1, format as usize & 0x1f),
91-
Format::STR8 => (2, take_byte_iter(bytes.by_ref())? as usize),
92-
Format::STR16 => (
93-
3,
94-
take_num_iter(bytes.by_ref(), u16::from_be_bytes)? as usize,
95-
),
96-
Format::STR32 => (
97-
5,
98-
take_num_iter(bytes.by_ref(), u32::from_be_bytes)? as usize,
99-
),
100-
_ => return Err(Error::UnexpectedFormatTag),
101-
};
102-
let v: Vec<_> = bytes.take(len).collect();
103-
if v.len() < len {
104-
return Err(Error::BufferTooShort);
105-
}
106-
let s = String::from_utf8(v).map_err(|_| Error::InvalidUtf8)?;
107-
Ok((n + len, s))
83+
fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
84+
where
85+
I: IntoIterator<Item = u8>,
86+
{
87+
let mut bytes = bytes.into_iter();
88+
let format = take_byte_iter(bytes.by_ref())?;
89+
let (n, len) = match format {
90+
0xa0..=0xbf => (1, format as usize & 0x1f),
91+
Format::STR8 => (2, take_byte_iter(bytes.by_ref())? as usize),
92+
Format::STR16 => (
93+
3,
94+
take_num_iter(bytes.by_ref(), u16::from_be_bytes)? as usize,
95+
),
96+
Format::STR32 => (
97+
5,
98+
take_num_iter(bytes.by_ref(), u32::from_be_bytes)? as usize,
99+
),
100+
_ => return Err(Error::UnexpectedFormatTag),
101+
};
102+
let v: Vec<_> = bytes.take(len).collect();
103+
if v.len() < len {
104+
return Err(Error::BufferTooShort);
108105
}
106+
let s = String::from_utf8(v).map_err(|_| Error::InvalidUtf8)?;
107+
Ok((n + len, s))
109108
}
110109
}

msgpacker/src/unpack/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{helpers, Error, Format, Unpackable};
22

3+
#[cfg(feature = "alloc")]
34
mod binary;
45
mod collections;
56
mod common;

0 commit comments

Comments
 (0)