ByteBuffer

Trait ByteBuffer 

pub trait ByteBuffer {
    // Required methods
    fn filled(&self) -> &[u8];
    fn spare(&mut self, len: usize) -> Result<&mut [u8], CapacityExceeded>;
    fn commit(&mut self, n: usize);
    fn consume(&mut self, n: usize);
    fn clear(&mut self);

    // Provided method
    fn extend(&mut self, chunk: &[u8]) -> Result<(), CapacityExceeded> { ... }
}
Expand description

Contiguous byte storage shared by PacketReader and PacketWriter.

A buffer holds a contiguous run of live bytes. The read side appends with extend and drains from the front with consume; the write side reserves a writable tail with spare, fills it, then commits the bytes actually written. Keeping storage behind this trait is what lets the framer stay no-alloc: 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 (or encoded into) a single borrowed slice without copying.

Required Methods§

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

The contiguous run of live bytes (fed/committed but not yet consumed).

fn spare(&mut self, len: usize) -> Result<&mut [u8], CapacityExceeded>

Reserves a contiguous, writable tail region of exactly len bytes.

The returned slice is uninitialized scratch space; the caller writes into it and then calls commit with the number of bytes actually written. It is not part of filled until committed.

§Errors

Returns CapacityExceeded when a fixed-capacity buffer cannot hold len more bytes even after reclaiming consumed space.

fn commit(&mut self, n: usize)

Commits the leading n bytes of the region returned by the preceding spare call, making them part of filled.

fn consume(&mut self, n: usize)

Marks the leading n filled bytes as consumed.

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.

Provided Methods§

fn extend(&mut self, chunk: &[u8]) -> Result<(), CapacityExceeded>

Appends a known slice. Convenience over spare + commit for the read side.

§Errors

Returns CapacityExceeded when a fixed-capacity buffer cannot hold the additional bytes even after reclaiming consumed space.

Implementors§

§

impl ByteBuffer for GrowBuffer

Available on crate feature alloc only.
§

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