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§
Required Methods§
Sourcefn reserve(&mut self, len: usize) -> &mut [u8] ⓘ
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.
Sourcefn commit(&mut self, len: usize)
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.
Sourcefn get_data_owned(&mut self) -> Self::Slice
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.
Sourcefn frame(&self) -> &[u8] ⓘ
fn frame(&self) -> &[u8] ⓘ
Returns the committed bytes of the frame being written, without transferring ownership of the buffer.
Sourcefn frame_mut(&mut self) -> &mut [u8] ⓘ
fn frame_mut(&mut self) -> &mut [u8] ⓘ
Returns the committed bytes of the frame being written, mutably, without transferring ownership of the buffer.
Sourcefn len(&self) -> usize
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.
Sourcefn truncate(&mut self, len: usize)
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§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".