Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<'a> { /* private fields */ }
Expand description

Decoder that reads Crous binary data and produces values.

§Example

use crous_core::{Encoder, Decoder, Value};

let mut enc = Encoder::new();
enc.encode_value(&Value::Str("hello".into())).unwrap();
let bytes = enc.finish().unwrap();

let mut dec = Decoder::new(&bytes);
let val = dec.decode_next().unwrap();
assert_eq!(val.to_owned_value(), Value::Str("hello".into()));

Implementations§

Source§

impl<'a> Decoder<'a>

Source

pub fn new(data: &'a [u8]) -> Self

Create a new decoder over the given data.

Source

pub fn with_limits(data: &'a [u8], limits: Limits) -> Self

Create a decoder with custom limits.

Source

pub fn skip_value_at(&mut self, block_end: usize) -> Result<()>

Skip a value at the current block_pos without allocating. Used for forward-compatible skipping of unknown fields.

All branches are bounds-checked against block_end and enforce the decoder’s Limits to prevent denial-of-service via crafted counts or deeply nested structures.

Source

pub fn header(&mut self) -> Result<&FileHeader>

Get the parsed file header.

Source

pub fn decode_next(&mut self) -> Result<CrousValue<'a>>

Decode the next value from the input. Automatically reads blocks as needed.

Returns a zero-copy CrousValue that borrows from the input data.

Note: Zero-copy decoding requires uncompressed blocks. If a compressed block is encountered, this method returns DecompressionError because borrowed CrousValue<'a> cannot reference decompressed (owned) data. Use decode_all_owned() or decode_next_owned() for compressed data.

Source

pub fn decode_next_owned(&mut self) -> Result<Value>

Decode the next value as an owned Value.

Unlike decode_next(), this works transparently with both compressed and uncompressed blocks.

Source

pub fn decode_all(&mut self) -> Result<Vec<CrousValue<'a>>>

Decode all remaining values from the input.

Source

pub fn decode_all_owned(&mut self) -> Result<Vec<Value>>

Decode all remaining values as owned Values.

This works transparently with both compressed and uncompressed blocks.

Source

pub fn position(&self) -> usize

Current position in the input.

Source

pub fn memory_used(&self) -> usize

Cumulative bytes tracked as allocated during this decode session.

Auto Trait Implementations§

§

impl<'a> Freeze for Decoder<'a>

§

impl<'a> RefUnwindSafe for Decoder<'a>

§

impl<'a> Send for Decoder<'a>

§

impl<'a> Sync for Decoder<'a>

§

impl<'a> Unpin for Decoder<'a>

§

impl<'a> UnsafeUnpin for Decoder<'a>

§

impl<'a> UnwindSafe for Decoder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.