From aa6b08319be382f310b323343d0c49268e17af84 Mon Sep 17 00:00:00 2001 From: jkalez <91279640+jkalez@users.noreply.github.com> Date: Sat, 5 Aug 2023 13:08:30 -0400 Subject: [PATCH] Implement UperReader for ScopedBitReader over Bits (gh-81) Previous implementations of UperReader required that a user have an implementation of `Bits` or a handful of other types (some primitives) in order to get a `UperReader` instance. However, the underlying implementation was designed to be generic over anything implementing `ScopedBitReader`. This commit removes the hard reliance on `Bits` in favor of being generic over `ScopedBitReader`. Users can now provide their own type implementing `ScopedBitReader` so and get their own implementation of `UperReader`. This is useful for streams of data which may not be easily represented as a `Bits`. --- src/syn/io/uper.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/syn/io/uper.rs b/src/syn/io/uper.rs index d685aa2..eba21bf 100644 --- a/src/syn/io/uper.rs +++ b/src/syn/io/uper.rs @@ -733,17 +733,10 @@ pub struct UperReader { scope_description: Vec, } -/* impl From for UperReader { fn from(bits: B) -> Self { - UperReader { bits, scope: None } - } -}*/ - -impl<'a, I: Into>> From for UperReader> { - fn from(bits: I) -> Self { - Self { - bits: bits.into(), + UperReader { + bits, scope: None, #[cfg(feature = "descriptive-deserialize-errors")] scope_description: Vec::new(), @@ -751,6 +744,12 @@ impl<'a, I: Into>> From for UperReader> { } } +impl<'a> From<(&'a [u8], usize)> for UperReader> { + fn from(bits: (&'a [u8], usize)) -> Self { + UperReader::from(Bits::from(bits)) + } +} + impl UperReader { #[inline] fn read_length_determinant( @@ -1406,14 +1405,14 @@ impl Reader for UperReader { } } -pub trait UperDecodable<'a, I: Into> + 'a> { - fn decode_from_uper(bits: I) -> Result +pub trait UperDecodable<'a, B: ScopedBitRead> { + fn decode_from_uper(bits: B) -> Result where Self: Sized; } -impl<'a, R: Readable, I: Into> + 'a> UperDecodable<'a, I> for R { - fn decode_from_uper(bits: I) -> Result +impl<'a, R: Readable, B: ScopedBitRead> UperDecodable<'a, B> for R { + fn decode_from_uper(bits: B) -> Result where Self: Sized, {