Skip to main content

StreamDecoder

Struct StreamDecoder 

Source
pub struct StreamDecoder<F> { /* private fields */ }
Expand description

Stateful wrapper that drives a Framer over a growing input buffer.

It owns the Framer::next_frame_from progress hint, so a delimiter search resumes where the previous call stopped. Use it whenever input arrives in pieces (a socket read loop, a serial port, a message queue).

The input contract is the one described on Framer::next_frame_from: between calls, only append to the buffer; after a frame is returned, drop its consumed() bytes from the front.

§Example

use wire_codec::framing::{Delimited, StreamDecoder};

let mut decoder = StreamDecoder::new(Delimited::bounded(b"\n").unwrap());
let mut buffer: Vec<u8> = Vec::new();
let mut lines: Vec<Vec<u8>> = Vec::new();

for chunk in [&b"PI"[..], b"NG\nQU", b"IT\n"] {
    buffer.extend_from_slice(chunk);
    let mut start = 0;
    while let Some(frame) = decoder.decode(&buffer[start..]).unwrap() {
        lines.push(frame.payload().to_vec());
        start += frame.consumed();
    }
    buffer.drain(..start);
}
assert_eq!(lines, [b"PING".to_vec(), b"QUIT".to_vec()]);

Implementations§

Source§

impl<F: Framer> StreamDecoder<F>

Source

pub const fn new(framer: F) -> Self

Wrap framer with fresh streaming state.

Source

pub fn decode<'a>(&mut self, input: &'a [u8]) -> Result<Option<Frame<'a>>>

Try to extract the next frame from the start of input.

§Errors

Propagates the wrapped framer’s errors. The scan progress is cleared on error, because the usual response (discarding the buffered input and resynchronising) breaks the append-only contract the progress hint relies on. Call StreamDecoder::reset yourself whenever you discard or replace buffered input without an error.

Source

pub fn reset(&mut self)

Forget any partial-scan progress, for example after discarding the buffered input or switching to a new connection.

Source

pub const fn framer(&self) -> &F

Borrow the wrapped framer, for example to call Framer::write_frame on the outbound path.

Source

pub fn into_inner(self) -> F

Consume the decoder and return the wrapped framer.

Trait Implementations§

Source§

impl<F: Clone> Clone for StreamDecoder<F>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for StreamDecoder<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Default> Default for StreamDecoder<F>

Source§

fn default() -> Self

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

impl<F: Eq> Eq for StreamDecoder<F>

Source§

impl<F: Hash> Hash for StreamDecoder<F>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<F: PartialEq> PartialEq for StreamDecoder<F>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<F: PartialEq> StructuralPartialEq for StreamDecoder<F>

Auto Trait Implementations§

§

impl<F> Forget for StreamDecoder<F>

§

impl<F> Freeze for StreamDecoder<F>
where F: Freeze,

§

impl<F> Move for StreamDecoder<F>

§

impl<F> RefUnwindSafe for StreamDecoder<F>
where F: RefUnwindSafe,

§

impl<F> Send for StreamDecoder<F>
where F: Send,

§

impl<F> Sync for StreamDecoder<F>
where F: Sync,

§

impl<F> Unpin for StreamDecoder<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for StreamDecoder<F>
where F: UnsafeUnpin,

§

impl<F> UnwindSafe for StreamDecoder<F>
where F: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.