Skip to main content

Decoder

Struct Decoder 

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

Parser for decoding values from a byte stream.

Decoder provides a cursor-based API for deserializing values encoded by Encoder. All integers are decoded from little-endian format.

§Example

let mut dec = Decoder::new(&bytes);
let num = dec.read_u64()?;
let text = dec.read_string()?;
let flag = dec.read_bool()?;
dec.finish()?; // Verify all bytes consumed

Implementations§

Source§

impl<'a> Decoder<'a>

Source

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

Creates a new decoder from a byte slice.

Source

pub fn remaining(&self) -> usize

Returns the number of unread bytes remaining.

Source

pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>

Reads a fixed-size byte array.

Source

pub fn read_u8(&mut self) -> Result<u8>

Reads a u8 value.

Source

pub fn read_u16(&mut self) -> Result<u16>

Reads a u16 value (little-endian).

Source

pub fn read_u32(&mut self) -> Result<u32>

Reads a u32 value (little-endian).

Source

pub fn read_u64(&mut self) -> Result<u64>

Reads a u64 value (little-endian).

Source

pub fn read_u128(&mut self) -> Result<u128>

Reads a u128 value (little-endian).

Source

pub fn read_bool(&mut self) -> Result<bool>

Reads a boolean value (0 = false, 1 = true).

Returns an error if the byte is neither 0 nor 1.

Source

pub fn read_raw(&mut self, len: usize) -> Result<&'a [u8]>

Reads exactly len raw bytes without a length prefix.

Source

pub fn read_bytes(&mut self) -> Result<&'a [u8]>

Reads a byte slice with a u32 length prefix.

Format: [length: u32][data: bytes]

Source

pub fn read_string(&mut self) -> Result<String>

Reads a UTF-8 string with a u32 length prefix.

Returns an error if the bytes are not valid UTF-8.

Source

pub fn read_codec<T: BytesCodec>(&mut self) -> Result<T>

Reads a value that implements BytesCodec with a length prefix.

Source

pub fn finish(self) -> Result<()>

Verifies that all bytes have been consumed.

Returns an error if there are unread bytes remaining. Use this at the end of decoding to ensure data integrity.

§Example
let mut dec = Decoder::new(&bytes);
let value = dec.read_u64()?;
dec.finish()?; // Ensure no trailing bytes

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> Same for T

Source§

type Output = T

Should always be Self
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.