Skip to main content

PacketReader

Struct PacketReader 

pub struct PacketReader<B> {
    buf: B,
    max_packet_size: Option<NonZeroU32>,
}
Expand description

A buffering, sans-io packet framer.

Bytes are appended with feed in whatever chunks the transport provides. Complete packets are then pulled out with peek + decode + consume.

The byte storage is supplied as a ByteBuffer, so the framer itself is no-alloc: pair it with ArrayBuffer on no_std targets or GrowBuffer when alloc is available (the default).

Parse state is stored in the reader, never in a future. This lets an I/O driver read in one cancellation-safe step (e.g. one AsyncReadExt::read in a select!) and feed whatever it got. A cancelled read does not consume any bytes, and bytes already fed are never lost. This is the property a per-call read_exact loop lacks, and it also lets the driver read in large chunks instead of one syscall per byte.

Fields§

§buf: B§max_packet_size: Option<NonZeroU32>

Implementations§

§

impl PacketReader<GrowBuffer>

pub const fn new() -> Self

Available on crate feature alloc only.

Creates an empty heap-backed reader with no packet-size limit.

pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>

Available on crate feature alloc only.

Creates an empty heap-backed reader preallocating capacity bytes.

§Errors

Returns TryReserveError if the allocation fails.

§

impl<B: ByteBuffer> PacketReader<B>

pub const fn from_buffer(buf: B) -> Self

Creates a reader over the byte storage buf with no packet-size limit.

pub const fn set_max_packet_size(&mut self, max: NonZeroU32)

Rejects any packet whose total on-wire size exceeds max bytes.

peek returns DecodeError::TooLong as soon as the announced size is known, before the body is buffered, so an oversized declaration cannot force an unbounded allocation (§3.1.2.11.4).

pub fn buffered_len(&self) -> usize

Number of buffered bytes not yet consumed.

pub fn feed(&mut self, chunk: &[u8]) -> Result<(), B::Error>

Appends freshly read bytes.

§Errors

Returns the buffer’s Sink::Error if it cannot take the bytes.

pub fn clear(&mut self)

Drops all buffered bytes; call on connection loss to discard a stale partial packet before reusing the reader.

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

pub fn peek(&self) -> Result<Option<Frame>, DecodeError>

Inspects the next packet without consuming it.

Returns the Frame once a whole packet is buffered, or None if more bytes are needed.

§Errors

Returns DecodeError if the fixed header is malformed, or DecodeError::TooLong if the announced size exceeds the configured set_max_packet_size.

pub fn decode(&self, frame: &Frame) -> Result<Packet<'_>, DecodeError>

Decodes the packet in frame.

frame must come from the latest peek, with nothing consumed or cleared since. A stale frame may panic or point at the wrong bytes.

§Errors

Returns DecodeError if the body is malformed.

pub fn body(&self, frame: &Frame) -> &[u8] ⓘ

Returns the body (variable header + payload) of the packet in frame, so an I/O driver can send it across a task boundary before consuming it.

frame must come from the latest peek, with nothing consumed or cleared since. A stale frame may panic or point at the wrong bytes.

pub fn consume(&mut self, frame: Frame)

Discards the packet in frame.

frame must come from the latest peek, with nothing consumed or cleared since. A stale frame may panic or point at the wrong bytes.

Trait Implementations§

§

impl<B: Debug> Debug for PacketReader<B>

§

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

Formats the value using the given formatter. Read more
§

impl Default for PacketReader<GrowBuffer>

Available on crate feature alloc only.
§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<B> Freeze for PacketReader<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for PacketReader<B>
where B: RefUnwindSafe,

§

impl<B> Send for PacketReader<B>
where B: Send,

§

impl<B> Sync for PacketReader<B>
where B: Sync,

§

impl<B> Unpin for PacketReader<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for PacketReader<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for PacketReader<B>
where B: 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 = !

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.