Buffer

Trait Buffer 

Source
pub trait Buffer {
    type Inner: AsMut<[u8]>;

    // Required methods
    fn into_inner(self) -> Self::Inner;
    fn next(&mut self, size: usize) -> Result<()>;
    fn more(&mut self, size: usize) -> Result<()>;
    fn clear(&mut self);
    fn used(&self) -> usize;
    fn offset(&self) -> usize;
    fn length(&self) -> usize;
    fn data(&self) -> &[u8] ;
    fn data_mut(&mut self) -> &mut [u8] ;
}
Expand description

A buffer to build packets in.

A Buffer is composed of multiple layers, internally an offset and length is used to keep track of the current layer, data() and data_mut() will always return a slice to the current layer.

§Example

use packet::buffer::{self, Buffer};

// Create a new dynamic buffer, `buffer::Dynamic` is backed by a `Vec<u8>`.
let mut buffer = buffer::Dynamic::new();

// Create a new layer for 20 bytes, calling `next()` increases the offset
// and zeroes the underlying memory.
buffer.next(20);

// Get more memory in the buffer.
buffer.more(4);

// Get the backing data for the buffer.
let data = buffer.into_inner();
assert_eq!(data.len(), 24);

Required Associated Types§

Source

type Inner: AsMut<[u8]>

Inner type used by the buffer.

Required Methods§

Source

fn into_inner(self) -> Self::Inner

Convert the buffer into the inner type.

Source

fn next(&mut self, size: usize) -> Result<()>

Go to the next layer requesting the given size, zeroeing the layer.

Source

fn more(&mut self, size: usize) -> Result<()>

Request more memory for the same layer, zeroeing the new buffer area.

Source

fn clear(&mut self)

Clear the buffer.

Source

fn used(&self) -> usize

Number of bytes used by the whole buffer.

Source

fn offset(&self) -> usize

Offset from the beginning of the whole buffer.

Source

fn length(&self) -> usize

Length of the current layer.

Source

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

Get a slice over the current layer.

Source

fn data_mut(&mut self) -> &mut [u8]

Get a mutable slice over the current layer.

Implementors§

Source§

impl Buffer for packet::buffer::Dynamic

Source§

impl<'a> Buffer for packet::buffer::Slice<'a>

Source§

type Inner = &'a mut [u8]