Skip to main content

RingBuffer

Struct RingBuffer 

Source
pub struct RingBuffer { /* private fields */ }
Expand description

Chunked ring buffer.

A ring buffer which is logically split into chunks where each chunk is at least chunk_size. A transaction API is provided if the caller needs to make multiple writes that must not be split across chunks. Chunks are discarded in their entirety when needed to make space.

A smaller chunk size has the advantage of higher utilization of the space in the ring buffer in exchange for more memory used keeping track of chunk boundaries and more work done when discarding chunks. Conversely a larger chunk size means that utilization of the buffer space has higher variance, but less memory is needed and discards are less often. Users should choose a chunk size appropriate for their use case.

Implementations§

Source§

impl RingBuffer

Source

pub fn new(size: usize, chunk_size: usize) -> Self

Create a new chunked ring buffer.

The capacity of the buffer is set to the next multiple of chunk_size larger than or equal to size.

Source

pub fn len(&self) -> usize

Compute the total number of bytes stored in the buffer.

Source

pub fn get_view(&self) -> (&[u8], &[u8])

Return the data stored in the ring buffer as two byte slices.

Source

pub fn start_transaction(&mut self) -> Transaction<'_>

Start a transaction that groups a series of writes.

Source

pub fn write(&mut self, slice: &[u8]) -> Result<(), WriteTooLarge>

Writes slice to the buffer and records a chunk if it is large enough.

Trait Implementations§

Source§

impl Debug for RingBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.