From ea2b03e0b459012581611ce1a0e2d19d2aced921 Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Sun, 25 Sep 2022 19:43:31 -0700 Subject: [PATCH 1/2] Add support for encoding/decoding with bincode. --- Cargo.toml | 1 + src/impl_bincode.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 src/impl_bincode.rs diff --git a/Cargo.toml b/Cargo.toml index 26e6bcd..252cb98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ substr-usize-indices = ["substr"] [dependencies] serde = { version = "1", default-features = false, optional = true } +bincode = { version = "2.0.0-rc.1", optional = true } [dev-dependencies] serde_test = { version = "1", default-features = false } diff --git a/src/impl_bincode.rs b/src/impl_bincode.rs new file mode 100644 index 0000000..3511e17 --- /dev/null +++ b/src/impl_bincode.rs @@ -0,0 +1,24 @@ +use super::ArcStr; +#[cfg(feature = "substr")] +use super::Substr; + +use alloc::string::String; +use bincode::{Decode, Encode}; +use bincode::error::EncodeError; +use bincode::error::DecodeError; +use bincode::enc::Encoder; +use bincode::de::Decoder; + +impl Decode for ArcStr { + fn decode(decoder: &mut D) -> Result { + let s: String = bincode::Decode::decode(decoder)?; + Ok(Self::from(s)) + } +} + +impl Encode for ArcStr { + fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { + bincode::Encode::encode(&self.as_str(), encoder)?; + Ok(()) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 4f0cae3..63de301 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,8 @@ mod mac; mod arc_str; #[cfg(feature = "serde")] mod impl_serde; +#[cfg(feature = "bincode")] +mod impl_bincode; pub use arc_str::ArcStr; #[cfg(feature = "substr")] From b43120cd13db16a1c1f61a12d1637b047b9bcd89 Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Wed, 5 Oct 2022 11:17:49 -0700 Subject: [PATCH 2/2] Add Substr implementation. Also added tests for bincode, bump bincode version to new RC. --- Cargo.toml | 2 +- src/impl_bincode.rs | 83 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 252cb98..c132fc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ substr-usize-indices = ["substr"] [dependencies] serde = { version = "1", default-features = false, optional = true } -bincode = { version = "2.0.0-rc.1", optional = true } +bincode = { version = "2.0.0-rc.2", optional = true } [dev-dependencies] serde_test = { version = "1", default-features = false } diff --git a/src/impl_bincode.rs b/src/impl_bincode.rs index 3511e17..da93fe9 100644 --- a/src/impl_bincode.rs +++ b/src/impl_bincode.rs @@ -1,13 +1,14 @@ +//! Implements Decode and Encode traits for use of ArcStr and Substr types with bincode. + use super::ArcStr; #[cfg(feature = "substr")] use super::Substr; use alloc::string::String; use bincode::{Decode, Encode}; -use bincode::error::EncodeError; -use bincode::error::DecodeError; +use bincode::error::{DecodeError, EncodeError}; use bincode::enc::Encoder; -use bincode::de::Decoder; +use bincode::de::{BorrowDecode, BorrowDecoder, Decoder}; impl Decode for ArcStr { fn decode(decoder: &mut D) -> Result { @@ -21,4 +22,80 @@ impl Encode for ArcStr { bincode::Encode::encode(&self.as_str(), encoder)?; Ok(()) } +} + +impl<'de> BorrowDecode<'de> for ArcStr { + fn borrow_decode>( + decoder: &mut D + ) -> Result { + let s: String = bincode::BorrowDecode::borrow_decode(decoder)?; + Ok(Self::from(s)) + } +} + +#[cfg(feature = "substr")] +impl Decode for Substr { + fn decode(decoder: &mut D) -> Result { + let s: String = bincode::Decode::decode(decoder)?; + Ok(Self::from(s)) + } +} + +#[cfg(feature = "substr")] +impl Encode for Substr { + fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { + bincode::Encode::encode(&self.as_str(), encoder)?; + Ok(()) + } +} + +#[cfg(feature = "substr")] +impl<'de> BorrowDecode<'de> for Substr { + fn borrow_decode>( + decoder: &mut D + ) -> Result { + let s: String = bincode::BorrowDecode::borrow_decode(decoder)?; + Ok(Self::from(s)) + } +} + + +#[cfg(test)] +mod tests { + #[test] + fn arcstr_decode_encode() { + use crate::ArcStr; + + let mut slice = [0u8; 14]; + let input = ArcStr::from("Hello, world!"); + + let length = bincode::encode_into_slice( + &input, + &mut slice, + bincode::config::standard() + ).unwrap(); + assert_eq!(length, 14); + + let decoded: ArcStr = bincode::decode_from_slice(&slice, bincode::config::standard()).unwrap().0; + assert_eq!(decoded, input); + } + + #[cfg(feature = "substr")] + #[test] + fn substr_decode_encode() { + use crate::Substr; + + let mut slice = [0u8; 14]; + let input = Substr::from("Hello, world!"); + + let length = bincode::encode_into_slice( + &input, + &mut slice, + bincode::config::standard() + ).unwrap(); + assert_eq!(length, 14); + + let decoded: Substr = bincode::decode_from_slice(&slice, bincode::config::standard()).unwrap().0; + assert_eq!(decoded, input); + } } \ No newline at end of file