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).

All parse state lives in the reader, never in a future. An I/O driver can therefore read in a single cancellation-safe step (e.g. one AsyncReadExt::read in a select!) and feed whatever it got: a cancelled read consumes nothing, 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 with_capacity(capacity: usize) -> Self

Available on crate feature alloc only.

Creates an empty heap-backed reader preallocating capacity bytes.

§

impl<B: ByteBuffer> PacketReader<B>

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

Creates a reader over the given byte storage 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<(), CapacityExceeded>

Appends freshly read bytes.

§Errors

Returns CapacityExceeded if the underlying ByteBuffer is fixed-capacity and full (never for GrowBuffer).

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<(FixedHeader, usize)>, DecodeError>

Inspects the next packet without consuming it.

Returns the fixed header and the total on-wire length (fixed header + body) 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, header: FixedHeader, total: usize, ) -> Result<Packet<'_>, DecodeError>

Decodes the packet reported by peek.

header and total must come from the same peek call on an unchanged buffer.

§Errors

Returns DecodeError if the body is malformed.

pub fn body(&self, header: FixedHeader, total: usize) -> &[u8]

Returns the body (variable header + payload) of the packet reported by peek, so an I/O driver can ship it across a task boundary before consuming it. header and total must come from the same peek call on an unchanged buffer.

pub fn consume(&mut self, total: usize)

Discards the leading total bytes (the packet just handled).

Trait Implementations§

§

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