Skip to main content

FrameDecoder

Struct FrameDecoder 

Source
pub struct FrameDecoder { /* private fields */ }
Expand description

An owning frame buffer: push received bytes in, take complete frames out.

Built on split_frame_limited. Frames come out as raw payloads, so a caller can route them as it likes: handshake frames to the handshake, reply frames to whatever classifies them afterwards.

Consumed bytes are reclaimed lazily: next only advances a cursor, and a later push moves the unconsumed tail to the front before appending once the consumed prefix is at least as large as that tail. Draining a burst of frames then costs at most one move of the leftover partial frame, and a caller that interleaves pushes with single next calls still pays amortised constant work per byte.

§After an error

Once next has reported FrameTooLarge the decoder is poisoned: the stream has no trustworthy frame boundary left. Every later next returns the same error, and push discards its input rather than buffer bytes that can never be framed. Drop the connection and the decoder with it.

Implementations§

Source§

impl FrameDecoder

Source

pub fn new() -> Self

A decoder enforcing the protocol’s MAX_FRAME_SIZE.

Source

pub fn with_max_payload(max_payload: usize) -> Self

A decoder refusing any frame whose payload exceeds max_payload.

Preallocates room for one maximal frame (capped at one MAX_FRAME_SIZE frame, so a generous limit does not reserve memory up front), sparing the first pushes a reallocation.

Source

pub fn max_payload(&self) -> usize

The payload limit this decoder enforces.

Source

pub fn is_poisoned(&self) -> bool

Whether a FrameTooLarge has poisoned the decoder.

Source

pub fn pending(&self) -> &[u8] ⓘ

Bytes received but not yet returned as a frame: a partial frame, or complete frames still waiting for next.

Source

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

Append received bytes.

Discarded once the decoder is poisoned.

Source

pub fn next(&mut self) -> Result<Option<&[u8]>, FrameTooLarge>

The next complete frame’s payload, or None until more bytes are pushed. The slice stays valid until the next call on the decoder.

Not an Iterator: the payload borrows the decoder, and None means “not yet”, not “never”.

Trait Implementations§

Source§

impl Debug for FrameDecoder

Source§

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

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

impl Default for FrameDecoder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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 = !

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.