Skip to main content

Buffer

Trait Buffer 

Source
pub trait Buffer {
    type Slice: AsMut<[u8]> + AsRef<[u8]> + Into<Slice>;

    // Required methods
    fn reserve(&mut self, len: usize) -> &mut [u8] ⓘ;
    fn commit(&mut self, len: usize);
    fn get_data_owned(&mut self) -> Self::Slice;
    fn frame(&self) -> &[u8] ⓘ;
    fn frame_mut(&mut self) -> &mut [u8] ⓘ;
    fn len(&self) -> usize;
    fn truncate(&mut self, len: usize);

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

Interface for working with memory buffers.

Implemented by BufferPool, which hands out slices of preallocated memory, and by BufferFromSystemMemory, which hands out Vec<u8>s. Utilities are provided to reserve writable memory, read back the frame being written, and hand it out.

The encoders and decoders of the codec_sv2 crate write frames through this trait.

Required Associated Types§

Source

type Slice: AsMut<[u8]> + AsRef<[u8]> + Into<Slice>

The type of slice that the buffer uses.

Required Methods§

Source

fn reserve(&mut self, len: usize) -> &mut [u8] ⓘ

Makes room for len more bytes after the data written so far and returns that space, without counting any of it as written.

Only Buffer::commit counts bytes as written. Reserving again before committing replaces the reservation, and bytes written into it are not kept.

Source

fn commit(&mut self, len: usize)

Counts the first len bytes of the last reservation as written, and ends that reservation.

Panics if len is larger than the last reservation.

Source

fn get_data_owned(&mut self) -> Self::Slice

Provides ownership of a slice in the buffer pool to the caller and updates the buffer pool’s state by modifying the position in shared_state that the slice occupies. The pool now points to the next set of uninitialized space.

Source

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

Returns the committed bytes of the frame being written, without transferring ownership of the buffer.

Source

fn frame_mut(&mut self) -> &mut [u8] ⓘ

Returns the committed bytes of the frame being written, mutably, without transferring ownership of the buffer.

Source

fn len(&self) -> usize

Returns the size of the written portion of the buffer. This is useful for tracking how much of the buffer has been filled with data. The number of bytes currently written in the buffer is returned.

Source

fn truncate(&mut self, len: usize)

Drops the committed bytes past len, like Vec::truncate: a len at or past the committed length changes nothing, so the frame can only shrink.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the buffer is empty, false otherwise.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§