Trait packet::buffer::Buffer
[−]
[src]
pub trait Buffer { type Inner: AsMut<[u8]>; 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]; }
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);
Associated Types
Required Methods
fn into_inner(self) -> Self::Inner
Convert the buffer into the inner type.
fn next(&mut self, size: usize) -> Result<()>
Go to the next layer requesting the given size, zeroeing the layer.
fn more(&mut self, size: usize) -> Result<()>
Request more memory for the same layer, zeroeing the new buffer area.
fn clear(&mut self)
Clear the buffer.
fn used(&self) -> usize
Number of bytes used by the whole buffer.
fn offset(&self) -> usize
Offset from the beginning of the whole buffer.
fn length(&self) -> usize
Length of the current layer.
fn data(&self) -> &[u8]
Get a slice over the current layer.
fn data_mut(&mut self) -> &mut [u8]
Get a mutable slice over the current layer.