Skip to main content

ByteBuffer

Trait ByteBuffer 

pub trait ByteBuffer: Sink {
    // Required methods
    fn clear(&mut self);
    fn consume(&mut self, n: usize);
    fn filled(&self) -> &[u8] ⓘ;
    fn truncate(&mut self, len: usize);
}
Expand description

Contiguous byte storage shared by PacketReader and PacketWriter.

A buffer stores a contiguous run of live bytes. Both sides append through Sink::put and drain from the front with consume. Keeping storage behind this trait lets the framer work without allocation. ArrayBuffer is a fixed-capacity, no_std buffer, while GrowBuffer grows on the heap when alloc is available.

Implementors must keep the live bytes contiguous so a whole packet can be decoded from one borrowed slice without copying.

Required Methods§

fn clear(&mut self)

Drops all buffered bytes.

Call this when a connection is lost so a stale partial packet from the dead connection cannot corrupt framing on the next one.

fn consume(&mut self, n: usize)

Marks the leading n filled bytes as consumed.

§Panics

Implementations panic if n exceeds the length of filled.

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

The contiguous run of live bytes (appended but not yet consumed).

fn truncate(&mut self, len: usize)

Keeps the leading len filled bytes and drops the rest.

§Panics

Implementations panic if len exceeds the length of filled.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl ByteBuffer for GrowBuffer

Available on crate feature alloc only.
§

impl<const N: usize> ByteBuffer for ArrayBuffer<N>