Skip to main content

Buffer

Struct Buffer 

Source
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>

Source

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.

Source

pub fn size(&self) -> u32

Returns the size of the backing storage.

Source

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.

Source

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

pub fn drain(&self) -> Result<(), Status>

Empties the contents of the buffer.

This is logically a read operation, so a read and drain cannot be called concurrently. Additionally, the behavior of this method is non-deterministic if a write is in-progress.

Source§

impl Buffer<DefaultAllocator>

Source

pub fn try_new(size: u32) -> Result<Self, Status>

Constructs a new Buffer with a dynamically allocated backing storage of the given size, using the default allocator.

Source§

impl Buffer<NoOpAllocator>

Source

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
  • storage must point to a valid, initialized slice of bytes whose length is a power of two and does not exceed MAX_STORAGE_SIZE.

Trait Implementations§

Source§

impl<A: Allocator + Default> Drop for Buffer<A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<A = DefaultAllocator> !Freeze for Buffer<A>

§

impl<A> RefUnwindSafe for Buffer<A>
where A: RefUnwindSafe,

§

impl<A = DefaultAllocator> !Send for Buffer<A>

§

impl<A = DefaultAllocator> !Sync for Buffer<A>

§

impl<A> Unpin for Buffer<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for Buffer<A>

§

impl<A> UnwindSafe for Buffer<A>
where A: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.