Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<P, S> { /* private fields */ }
Expand description

Owned-buffer streaming decoder, parameterised on the Partial type.

Accepts bytes via feed and yields decoded values via next. A None from next means “need more bytes, call feed again” - malformed packets are silently skipped (the framed bytes are drained to guarantee forward progress).

§Example

// fresh mode (no in-flight partial): seek sentinel + decode framed body
let mut dec = Decoder::<<MyPacket as DecodePartial<&[u8]>>::Partial>::new();
let mut scratch = [0u8; 2048];
loop {
    let n = socket.recv(&mut scratch)?;
    dec.feed(&scratch[..n]);
    while let Some(pkt) = dec.next() {
        handle(pkt);
    }
}

// resume mode: handed back from `Packet::NeedMore`
match MyPacket::decode_partial(&mut input) {
    Ok(Packet::Ready(t)) => use_packet(t),
    Ok(Packet::NeedMore(partial)) => {
        let mut dec = Decoder::new();
        dec.feed(more_bytes);
        while let Some(pkt) = dec.next() {
            use_packet(pkt);
            break;
        }
    }
    Err(label) => report(label),
}

Implementations§

Source§

impl<P, S> Decoder<P, S>

Decoder implementation

Source

pub fn new() -> Self

Construct an empty decoder in fresh mode (no in-flight partial).

Source

pub fn with_capacity(cap: usize) -> Self

Construct an empty decoder with a pre-allocated buffer capacity, in fresh mode.

Source

pub fn buffered(&self) -> &[u8]

The bytes currently buffered but not yet decoded. Useful for observability and diagnostics.

Source

pub fn partial(&self) -> Option<&P>

Borrow the in-flight partial, if any. Some only after the decoder was constructed from a Packet::NeedMore return and before the next Self::next completes the packet.

Source

pub fn into_partial(self) -> Option<P>

Consume the decoder and return its in-flight partial, if any. Used by the derive-generated decode_partial wrapper to lift “input fully consumed, NeedMore returned” into a finalisation at the fresh-mode boundary.

Source

pub fn clear(&mut self)

Drop all buffered bytes and any in-flight partial. Call after a Malformed error if you want to resync cleanly rather than byte-walk through corrupt input.

Source

pub fn feed(&mut self, bytes: &[u8])

Append bytes to the internal buffer. Does not trigger decoding on its own - call next to try to consume complete values.

Source

pub fn iter(&mut self) -> DecoderIter<'_, P, S>

Returns an iterator over the buffered values, consuming them as they are decoded

Source

pub fn next<T>(&mut self) -> Option<T>
where S: Stream, P: Partial<Final = T> + Default, Self: PartialIterator<P>,

Decode the next complete value from the buffer, if one is available

Source§

impl<P, T> Decoder<P, &[u8]>
where P: Partial<Final = T> + Default, for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,

Decoder decoding implementation - resume mode

This is just the &u8 specialization of Decoder

When the decoder carries an in-flight partial, next resumes DecodePartial::decode_partial without re-seeking the sentinel: the partial state already lives past the framing.

Source

pub fn finish(self) -> Result<T, DecodeIterError>

Force-finalise the current partial. Use when the upstream signals “no more bytes coming” and you want the accumulated partial surfaced (or its required-field failure reported). The decoder is consumed; the buffered bytes are discarded.

Trait Implementations§

Source§

impl<P, S> Default for Decoder<P, S>

Decoder implementation of Default

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'d, P, T, S> IntoIterator for &'d mut Decoder<P, S>
where Decoder<P, S>: PartialIterator<P>, S: Stream, P: Partial<Final = T> + Default, for<'b> T: DecodePartial<S, Partial = P> + ResumePartial<S> + SeekSentinel<S>,

Decoder implementation of IntoIterator

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = DecoderIter<'d, P, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, P, T> PartialIterator<P> for &mut Decoder<P, &'a [u8]>
where P: Partial<Final = T> + Default, Decoder<P, &'a [u8]>: PartialIterator<P>,

&mut Decoder implementation of PartialIterator for [&[u8]]

Source§

fn next_resume(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>

Resume-mode entry
Source§

fn next_fresh(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>

Fresh-mode entry Read more
Source§

impl<P, T> PartialIterator<P> for Decoder<P, &[u8]>
where P: Partial<Final = T> + Default, for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,

Decoder implementation of PartialIterator for [&[u8]]

Source§

fn next_resume(&mut self) -> Result<T, DecodeIterError>

Resume-mode entry

Source§

fn next_fresh(&mut self) -> Result<T, DecodeIterError>

Fresh-mode entry

Auto Trait Implementations§

§

impl<P, S> Freeze for Decoder<P, S>
where P: Freeze,

§

impl<P, S> RefUnwindSafe for Decoder<P, S>

§

impl<P, S> Send for Decoder<P, S>
where P: Send, S: Send,

§

impl<P, S> Sync for Decoder<P, S>
where P: Sync, S: Sync,

§

impl<P, S> Unpin for Decoder<P, S>
where P: Unpin, S: Unpin,

§

impl<P, S> UnsafeUnpin for Decoder<P, S>
where P: UnsafeUnpin,

§

impl<P, S> UnwindSafe for Decoder<P, S>
where P: UnwindSafe, S: UnwindSafe,

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.