Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions rmp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ edition = "2021"
[dependencies]
byteorder = { version = "1.4.2", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
# This is macro_only ;)
paste = "1.0"

[features]
default = ["std"]
Expand Down
50 changes: 19 additions & 31 deletions rmp/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ impl RmpReadErr for std::io::Error {}
impl RmpReadErr for core::convert::Infallible {}

macro_rules! read_byteorder_utils {
($($name:ident => $tp:ident),* $(,)?) => {
($($name:ident => $tp:ty => $func:ident),* $(,)?) => {
$(
#[inline]
#[doc(hidden)]
fn $name(&mut self) -> Result<$tp, ValueReadError<Self::Error>> where Self: Sized {
const SIZE: usize = core::mem::size_of::<$tp>();
let mut buf: [u8; SIZE] = [0u8; SIZE];
self.read_exact_buf(&mut buf).map_err(ValueReadError::InvalidDataRead)?;
Ok(paste::paste! {
<byteorder::BigEndian as byteorder::ByteOrder>::[<read_ $tp>](&mut buf)
})
Ok(<byteorder::BigEndian as byteorder::ByteOrder>::$func(&mut buf))
}
)*
};
Expand Down Expand Up @@ -119,22 +117,22 @@ pub trait RmpRead: sealed::Sealed {
}

read_byteorder_utils!(
read_data_u16 => u16,
read_data_u32 => u32,
read_data_u64 => u64,
read_data_i16 => i16,
read_data_i32 => i32,
read_data_i64 => i64,
read_data_f32 => f32,
read_data_f64 => f64
read_data_u16 => u16 => read_u16,
read_data_u32 => u32 => read_u32,
read_data_u64 => u64 => read_u64,
read_data_i16 => i16 => read_i16,
read_data_i32 => i32 => read_i32,
read_data_i64 => i64 => read_i64,
read_data_f32 => f32 => read_f32,
read_data_f64 => f64 => read_f64,
);
}

/*
* HACK: rmpv & rmp-erde used the internal read_data_* functions.
*
* Since adding no_std support moved these functions to the RmpRead trait,
* this broke compatiblity (despite changing no public APIs).
* this broke compatibility (despite changing no public APIs).
*
* In theory, we could update rmpv and rmp-serde to use the new APIS,
* but that would be needless churn (and might surprise users who just want to update rmp proper).
Expand All @@ -147,21 +145,21 @@ pub trait RmpRead: sealed::Sealed {
*/

macro_rules! wrap_data_funcs_for_compatibility {
($($tp:ident),* $(,)?) => {
$(paste::paste! {
($($tp:ty => $func:ident),* $(,)?) => {
$(
#[cfg(feature = "std")]
#[doc(hidden)]
#[deprecated(note = "internal function. rmpv & rmp-serde need to switch to RmpRead")]
pub fn [<read_data_ $tp>] <R: std::io::Read>(buf: &mut R) -> Result<$tp, ValueReadError> {
buf.[<read_data_ $tp>]()
pub fn $func<R: std::io::Read>(buf: &mut R) -> Result<$tp, ValueReadError> {
buf.$func()
}
})*
)*
};
}
wrap_data_funcs_for_compatibility!(
u8, u16, u32, u64,
i8, i16, i32, i64,
f32, f64
u8 => read_data_u8, u16 => read_data_u16, u32 => read_data_u32, u64 => read_data_u64,
i8 => read_data_i8, i16 => read_data_i16, i32 => read_data_i32, i64 => read_data_i64,
f32 => read_data_f32, f64 => read_data_f64,
);

#[cfg(feature = "std")]
Expand All @@ -174,16 +172,6 @@ impl<T: std::io::Read> RmpRead for T {
}
}

// An error returned from the `write_marker` and `write_fixval` functions.
struct MarkerWriteError<E: RmpReadErr>(E);

impl<E: RmpReadErr> From<E> for MarkerWriteError<E> {
#[cold]
fn from(err: E) -> Self {
Self(err)
}
}

/// An error that can occur when attempting to read a MessagePack marker from the reader.
#[derive(Debug)]
#[allow(deprecated)] // Needed for backwards compat
Expand Down
22 changes: 10 additions & 12 deletions rmp/src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,14 @@ mod sealed {
}

macro_rules! write_byteorder_utils {
($($name:ident => $tp:ident),* $(,)?) => {
($($name:ident => $tp:ty => $func:ident),* $(,)?) => {
$(
#[inline]
#[doc(hidden)]
fn $name(&mut self, val: $tp) -> Result<(), DataWriteError<Self::Error>> where Self: Sized {
const SIZE: usize = core::mem::size_of::<$tp>();
let mut buf: [u8; SIZE] = [0u8; SIZE];
paste::paste! {
<byteorder::BigEndian as byteorder::ByteOrder>::[<write_ $tp>](&mut buf, val);
}
<byteorder::BigEndian as byteorder::ByteOrder>::$func(&mut buf, val);
self.write_bytes(&buf).map_err(DataWriteError)
}
)*
Expand Down Expand Up @@ -170,14 +168,14 @@ pub trait RmpWrite: sealed::Sealed {
}

write_byteorder_utils!(
write_data_u16 => u16,
write_data_u32 => u32,
write_data_u64 => u64,
write_data_i16 => i16,
write_data_i32 => i32,
write_data_i64 => i64,
write_data_f32 => f32,
write_data_f64 => f64
write_data_u16 => u16 => write_u16,
write_data_u32 => u32 => write_u32,
write_data_u64 => u64 => write_u64,
write_data_i16 => i16 => write_i16,
write_data_i32 => i32 => write_i32,
write_data_i64 => i64 => write_i64,
write_data_f32 => f32 => write_f32,
write_data_f64 => f64 => write_f64,
);
}

Expand Down