diff --git a/ciborium/src/de/mod.rs b/ciborium/src/de/mod.rs index 1952a4b..7c6bcad 100644 --- a/ciborium/src/de/mod.rs +++ b/ciborium/src/de/mod.rs @@ -45,7 +45,8 @@ impl Expected for Header { } } -struct Deserializer<'b, R: Read> { +/// Deserializer +pub struct Deserializer<'b, R: Read> { decoder: Decoder, scratch: &'b mut [u8], recurse: usize, @@ -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( + 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( + 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, + } +}