Skip to content

Commit

Permalink
feat: add methods returning just the deserializer
Browse files Browse the repository at this point in the history
- fn deserializer_from_reader_with_buffer()
- fn deserializer_from_reader_with_buffer_and_recursion_limit()

Signed-off-by: Stanislav Ravas <[email protected]>
  • Loading branch information
elrafoon authored and rjzak committed Feb 17, 2024
1 parent 422face commit c19bf22
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl<E: de::Error> Expected<E> for Header {
}
}

struct Deserializer<'b, R: Read> {
/// Deserializer
pub struct Deserializer<'b, R: Read> {
decoder: Decoder<R>,
scratch: &'b mut [u8],
recurse: usize,
Expand Down Expand Up @@ -873,3 +874,40 @@ where

T::deserialize(&mut reader)
}

/// Returns a deserializer with a specified scratch buffer
#[inline]
pub fn deserializer_from_reader_with_buffer<R: Read>(
reader: R,
scratch_buffer: &mut [u8],
) -> Deserializer<'_, R>
where
R::Error: core::fmt::Debug,
{
Deserializer {
decoder: reader.into(),
scratch: scratch_buffer,
recurse: 256,
}
}

/// Returns a deserializer with a specified scratch buffer
/// amd maximum recursion limit. Inputs that are nested beyond the specified limit
/// will result in [`Error::RecursionLimitExceeded`] .
///
/// Set a high recursion limit at your own risk (of stack exhaustion)!
#[inline]
pub fn deserializer_from_reader_with_buffer_and_recursion_limit<R: Read>(
reader: R,
scratch_buffer: &mut [u8],
recurse_limit: usize,
) -> Deserializer<'_, R>
where
R::Error: core::fmt::Debug,
{
Deserializer {
decoder: reader.into(),
scratch: scratch_buffer,
recurse: recurse_limit,
}
}

0 comments on commit c19bf22

Please sign in to comment.