pub struct Buffer<A: Allocator + Default = DefaultAllocator> { /* private fields */ }Expand description
A transactional, single-producer, single-consumer ring buffer.
The caller is responsible for ensuring that there is only one reader and one writer; no internal synchronization is provided to enforce this constraint.
Backing storage is allocated dynamically during the init method. The requested size must be a
power of two for correct functionality.
Implementations§
Source§impl<A: Allocator + Default> Buffer<A>
impl<A: Allocator + Default> Buffer<A>
Sourcepub fn try_new_in(size: u32, allocator: A) -> Result<Self, Status>
pub fn try_new_in(size: u32, allocator: A) -> Result<Self, Status>
Constructs a new Buffer with a dynamically allocated backing storage of the given size,
using the given allocator.
Sourcepub fn reserve(&mut self, size: u32) -> Result<Reservation<'_>, Status>
pub fn reserve(&mut self, size: u32) -> Result<Reservation<'_>, Status>
Reserves a block of the given size in the buffer.
Any data written into this block will not be visible to readers until commit is called on
the returned Reservation.
Sourcepub fn read<F>(&self, copy_fn: F, len: u32) -> Result<u32, Status>where
F: FnMut(u32, &[u8]) -> Result<(), Status>,
pub fn read<F>(&self, copy_fn: F, len: u32) -> Result<u32, Status>where
F: FnMut(u32, &[u8]) -> Result<(), Status>,
Copies len bytes out of the buffer using the provided copy_fn.
The copy function has the signature copy_fn(offset: u32, src: &[u8]) -> Result<(), Status>
and may be invoked multiple times.
Returns the number of bytes read on success. If copy_fn returns an error, that error is
propagated.
Importantly, even if an error is returned, copy_fn might have already processed a partial
amount of data (between 0 and len bytes). However, these partially processed bytes are
considered not read. Consequently, the internal read pointer of the ring buffer will not
be advanced for these unread bytes, meaning that these same bytes will remain available for
reading in subsequent calls to read.
Source§impl Buffer<NoOpAllocator>
impl Buffer<NoOpAllocator>
Sourcepub unsafe fn from_raw_parts(storage: *mut u8, size: usize) -> Self
pub unsafe fn from_raw_parts(storage: *mut u8, size: usize) -> Self
Constructs a Buffer from raw pointers using a no-op allocator.
The returned buffer does not own the memory and will not deallocate it when dropped.
§Safety
storagemust point to a valid, initialized slice of bytes whose length is a power of two and does not exceedMAX_STORAGE_SIZE.