From 6f1a3e3be9a899f46f6065045eeef1d8eb171f90 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 18 Aug 2023 13:19:00 +0200 Subject: [PATCH 1/2] rmp: Stop using byteorder I feel like using the methods from core for this byte order conversion makes the code simpler than doing the same with the byteorder crate. Signed-off-by: Uli Schlachter --- rmp/Cargo.toml | 3 +-- rmp/src/decode/mod.rs | 6 ++---- rmp/src/encode/mod.rs | 9 ++------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/rmp/Cargo.toml b/rmp/Cargo.toml index 07bad9ae..34752b4e 100644 --- a/rmp/Cargo.toml +++ b/rmp/Cargo.toml @@ -12,7 +12,6 @@ categories = ["encoding"] 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" @@ -20,7 +19,7 @@ paste = "1.0" [features] default = ["std"] -std = ["byteorder/std", "num-traits/std"] +std = ["num-traits/std"] [dev-dependencies] quickcheck = "1.0.2" diff --git a/rmp/src/decode/mod.rs b/rmp/src/decode/mod.rs index b04c9cf5..02495e6c 100644 --- a/rmp/src/decode/mod.rs +++ b/rmp/src/decode/mod.rs @@ -58,9 +58,7 @@ macro_rules! read_byteorder_utils { 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! { - ::[](&mut buf) - }) + Ok($tp::from_be_bytes(buf)) } )* }; @@ -80,7 +78,7 @@ mod sealed{ /// The methods of this trait should be considered an implementation detail (for now). /// It is currently sealed (can not be implemented by the user). /// -/// See also [std::io::Read] and [byteorder::ReadBytesExt] +/// See also [std::io::Read] /// /// Its primary implementations are [std::io::Read] and [Bytes]. pub trait RmpRead: sealed::Sealed { diff --git a/rmp/src/encode/mod.rs b/rmp/src/encode/mod.rs index a35b86db..5292690e 100644 --- a/rmp/src/encode/mod.rs +++ b/rmp/src/encode/mod.rs @@ -122,12 +122,7 @@ macro_rules! write_byteorder_utils { #[inline] #[doc(hidden)] fn $name(&mut self, val: $tp) -> Result<(), DataWriteError> where Self: Sized { - const SIZE: usize = core::mem::size_of::<$tp>(); - let mut buf: [u8; SIZE] = [0u8; SIZE]; - paste::paste! { - ::[](&mut buf, val); - } - self.write_bytes(&buf).map_err(DataWriteError) + self.write_bytes(&val.to_be_bytes()).map_err(DataWriteError) } )* }; @@ -138,7 +133,7 @@ macro_rules! write_byteorder_utils { /// The methods of this trait should be considered an implementation detail (for now). /// It is currently sealed (can not be implemented by the user). /// -/// See also [std::uo::Write] and [byteorder::WriteBytesExt] +/// See also [std::uo::Write] /// /// Its primary implementations are [std::io::Write] and [ByteBuf]. pub trait RmpWrite: sealed::Sealed { From 670233196099d1f86f7dc8efa3e2b89d406a0fe6 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 18 Aug 2023 13:22:47 +0200 Subject: [PATCH 2/2] rmp-serde: Stop using byteorder The byteorder crate does not do much magic and for what is done here, it might be simpler to instead use the relevant methods from the standard library. Signed-off-by: Uli Schlachter --- rmp-serde/Cargo.toml | 1 - rmp-serde/src/decode.rs | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/rmp-serde/Cargo.toml b/rmp-serde/Cargo.toml index 395dc8a7..8122002b 100644 --- a/rmp-serde/Cargo.toml +++ b/rmp-serde/Cargo.toml @@ -15,7 +15,6 @@ edition = "2021" tag-prefix = "{{crate_name}}/" [dependencies] -byteorder = "1.4.3" serde = "1.0.136" rmp = { version = "0.8.11", path = "../rmp" } diff --git a/rmp-serde/src/decode.rs b/rmp-serde/src/decode.rs index 0f655945..8c6e69d9 100644 --- a/rmp-serde/src/decode.rs +++ b/rmp-serde/src/decode.rs @@ -7,8 +7,6 @@ use std::io::{self, Cursor, ErrorKind, Read}; use std::num::TryFromIntError; use std::str::{self, Utf8Error}; -use byteorder::{self, ReadBytesExt}; - use serde; use serde::de::{self, Deserialize, DeserializeOwned, DeserializeSeed, Unexpected, Visitor}; @@ -362,17 +360,24 @@ fn read_bin_data<'a, 'de, R: ReadSlice<'de>>(rd: &'a mut R, len: u32) -> Result< } fn read_u8(rd: &mut R) -> Result { - byteorder::ReadBytesExt::read_u8(rd).map_err(Error::InvalidDataRead) + let mut data = [0; 1]; + rd.read_exact(&mut data) + .map_err(Error::InvalidDataRead)?; + Ok(u8::from_be_bytes(data)) } fn read_u16(rd: &mut R) -> Result { - rd.read_u16::() - .map_err(Error::InvalidDataRead) + let mut data = [0; 2]; + rd.read_exact(&mut data) + .map_err(Error::InvalidDataRead)?; + Ok(u16::from_be_bytes(data)) } fn read_u32(rd: &mut R) -> Result { - rd.read_u32::() - .map_err(Error::InvalidDataRead) + let mut data = [0; 4]; + rd.read_exact(&mut data) + .map_err(Error::InvalidDataRead)?; + Ok(u32::from_be_bytes(data)) } fn ext_len(rd: &mut R, marker: Marker) -> Result {