From a5af3b286a62ea2b399530a514fa3525892cfb27 Mon Sep 17 00:00:00 2001 From: Tjalling Tolle Date: Wed, 14 Feb 2024 09:44:54 +0100 Subject: [PATCH 1/2] feat: add support for Rc and Arc --- src/readable_impl.rs | 28 ++++++++++++++++++++++++++++ src/writable_impl.rs | 26 ++++++++++++++++++++++++++ tests/serialization_tests.rs | 14 ++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/readable_impl.rs b/src/readable_impl.rs index 8d26912..6cb65ed 100644 --- a/src/readable_impl.rs +++ b/src/readable_impl.rs @@ -2,6 +2,8 @@ use std::mem; use std::borrow::{Cow, ToOwned}; use std::ops::{Range, RangeInclusive}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::rc::Rc; +use std::sync::Arc; use std::hash::{BuildHasher, Hash}; use core::mem::MaybeUninit; @@ -352,6 +354,32 @@ impl< 'a, C: Context, T: Readable< 'a, C >, E: Readable< 'a, C > > Readable< 'a, } } +impl< 'a, C: Context, T: Readable< 'a, C > > Readable< 'a, C > for Rc< T > { + #[inline] + fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > { + let value: T = reader.read_value()?; + Ok(Rc::new(value)) + } + + #[inline] + fn minimum_bytes_needed() -> usize { + >::minimum_bytes_needed() + } +} + +impl< 'a, C: Context, T: Readable< 'a, C > > Readable< 'a, C > for Arc< T > { + #[inline] + fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > { + let value: T = reader.read_value()?; + Ok(Arc::new(value)) + } + + #[inline] + fn minimum_bytes_needed() -> usize { + >::minimum_bytes_needed() + } +} + impl< 'a, C: Context > Readable< 'a, C > for () { #[inline] fn read_from< R: Reader< 'a, C > >( _: &mut R ) -> Result< Self, C::Error > { diff --git a/src/writable_impl.rs b/src/writable_impl.rs index fb0046c..729fc31 100644 --- a/src/writable_impl.rs +++ b/src/writable_impl.rs @@ -2,6 +2,8 @@ use std::mem; use std::borrow::{Cow, ToOwned}; use std::ops::{Range, RangeInclusive}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::rc::Rc; +use std::sync::Arc; use std::hash::Hash; use crate::endianness::Endianness; @@ -416,6 +418,30 @@ impl< C: Context, T: Writable< C >, E: Writable< C > > Writable< C > for Result< } } +impl< C: Context, T: Writable< C > > Writable< C > for Rc< T > { + #[inline] + fn write_to< W: ?Sized + Writer< C > >( &self, writer: &mut W ) -> Result< (), C::Error > { + (&**self).write_to( writer ) + } + + #[inline] + fn bytes_needed( &self ) -> Result< usize, C::Error > { + Writable::< C >::bytes_needed( &**self ) + } +} + +impl< C: Context, T: Writable< C > > Writable< C > for Arc< T > { + #[inline] + fn write_to< W: ?Sized + Writer< C > >( &self, writer: &mut W ) -> Result< (), C::Error > { + (&**self).write_to( writer ) + } + + #[inline] + fn bytes_needed( &self ) -> Result< usize, C::Error > { + Writable::< C >::bytes_needed( &**self ) + } +} + impl< C: Context > Writable< C > for () { #[inline] fn write_to< W: ?Sized + Writer< C > >( &self, _: &mut W ) -> Result< (), C::Error > { diff --git a/tests/serialization_tests.rs b/tests/serialization_tests.rs index 5c1de89..668cae1 100644 --- a/tests/serialization_tests.rs +++ b/tests/serialization_tests.rs @@ -1,6 +1,8 @@ use std::borrow::Cow; use std::ops::{Range, RangeInclusive}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::rc::Rc; +use std::sync::Arc; use std::fmt::Debug; use std::num::NonZeroU32; @@ -1407,6 +1409,18 @@ symmetric_tests! { be = [0, 0, 10], minimum_bytes = 1 } + rc_u16 for Rc< u16 > { + in = Rc::new(10), + le = [10, 0], + be = [0, 10], + minimum_bytes = 2 + } + arc_u16 for Arc< u16 > { + in = Arc::new(10), + le = [10, 0], + be = [0, 10], + minimum_bytes = 2 + } hashmap_u16_bool for HashMap< u16, bool > { in = vec![ (10, true) ].into_iter().collect(), le = [1, 0, 0, 0, 10, 0, 1], From bdc6f01d367f6b8d6d222f06077b6dfa31d423f5 Mon Sep 17 00:00:00 2001 From: Tjalling Tolle Date: Wed, 14 Feb 2024 09:50:13 +0100 Subject: [PATCH 2/2] docs: add to supported types table --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 82f5f55..314be10 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ Out-of-box the following types are supported: | `RangeInclusive` | `(T, T)` | | `Option` | `(1_u8, T)` or `0_u8` | | `Result` | `(1_u8, T)` or `(0_u8, E)` | +| `Rc` | as-is | +| `Arc` | as-is | | `()` | nothing | | `(T)` | as-is | | `(T, T)` | as-is |