Skip to content

Commit

Permalink
Implement UperReader for ScopedBitReader over Bits (gh-81)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jkalez authored Aug 5, 2023
1 parent c88cd0d commit aa6b083
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/syn/io/uper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,24 +733,23 @@ pub struct UperReader<B: ScopedBitRead> {
scope_description: Vec<ScopeDescription>,
}

/*
impl<B: ScopedBitRead> From<B> for UperReader<B> {
fn from(bits: B) -> Self {
UperReader { bits, scope: None }
}
}*/

impl<'a, I: Into<Bits<'a>>> From<I> for UperReader<Bits<'a>> {
fn from(bits: I) -> Self {
Self {
bits: bits.into(),
UperReader {
bits,
scope: None,
#[cfg(feature = "descriptive-deserialize-errors")]
scope_description: Vec::new(),
}
}
}

impl<'a> From<(&'a [u8], usize)> for UperReader<Bits<'a>> {
fn from(bits: (&'a [u8], usize)) -> Self {
UperReader::from(Bits::from(bits))
}
}

impl<B: ScopedBitRead> UperReader<B> {
#[inline]
fn read_length_determinant(
Expand Down Expand Up @@ -1406,14 +1405,14 @@ impl<B: ScopedBitRead> Reader for UperReader<B> {
}
}

pub trait UperDecodable<'a, I: Into<Bits<'a>> + 'a> {
fn decode_from_uper(bits: I) -> Result<Self, Error>
pub trait UperDecodable<'a, B: ScopedBitRead> {
fn decode_from_uper(bits: B) -> Result<Self, Error>
where
Self: Sized;
}

impl<'a, R: Readable, I: Into<Bits<'a>> + 'a> UperDecodable<'a, I> for R {
fn decode_from_uper(bits: I) -> Result<Self, Error>
impl<'a, R: Readable, B: ScopedBitRead> UperDecodable<'a, B> for R {
fn decode_from_uper(bits: B) -> Result<Self, Error>
where
Self: Sized,
{
Expand Down

0 comments on commit aa6b083

Please sign in to comment.