pub struct Buffer<T: Sized> { /* private fields */ }
Expand description
Static buffer to raw bytes
The size of the storage must be known at compile time, therefore it is suitable only for non-dynamic storages.
While write semantics are pretty obvious, read behaviour is more complicated due to it being a static buffer.
When performing ReadBuf::read
memory is always being read from the beginning.
So as with any other implementation read leads to consumption.
But as this buffer is single chunk of static memory, such operation will require to shift
already written bytes to the beginning (meaning each ReadBuf::consume
involves a memmove
unless consumed len
is not equal to current len
)
In general it would be more effective to access memory as slice and then consume it, if needed.
Implementations§
Source§impl<S: Sized> Buffer<S>
impl<S: Sized> Buffer<S>
Sourcepub const fn into_circular(self) -> Ring<S>
pub const fn into_circular(self) -> Ring<S>
Transforms buffer into ring buffer.
Sourcepub const unsafe fn from_parts(inner: MaybeUninit<S>, cursor: usize) -> Self
pub const unsafe fn from_parts(inner: MaybeUninit<S>, cursor: usize) -> Self
Creates new instance from parts.
cursor
- number of elements written. It is user responsibility to make sure it is not over
actual capacity
Sourcepub const fn into_parts(self) -> (MaybeUninit<S>, usize)
pub const fn into_parts(self) -> (MaybeUninit<S>, usize)
Splits buffer into parts.
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8]
pub fn as_mut_slice(&mut self) -> &mut [u8]
Returns mutable slice to already written data.
Sourcepub fn truncate(&mut self, cursor: usize)
pub fn truncate(&mut self, cursor: usize)
Shortens the buffer.
Does nothing if new cursor
is after current position.