pub trait RingBuffer: Observer + Consumer + Producer {
    // Required methods
    unsafe fn hold_read(&self, flag: bool);
    unsafe fn hold_write(&self, flag: bool);

    // Provided methods
    fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item> { ... }
    fn push_iter_overwrite<I>(&mut self, iter: I)
       where I: Iterator<Item = Self::Item> { ... }
    fn push_slice_overwrite(&mut self, elems: &[Self::Item])
       where Self::Item: Copy { ... }
}
Expand description

An abstract ring buffer.

Details

The ring buffer consists of an array (of capacity size) and two indices: read and write. When an item is extracted from the ring buffer it is taken from the read index and after that read is incremented. New item is appended to the write index and write is incremented after that.

The read and write indices are modulo 2 * capacity (not just capacity). It allows us to distinguish situations when the buffer is empty (read == write) and when the buffer is full (write - read modulo 2 * capacity equals to capacity) without using the space for an extra element in container. And obviously we cannot store more than capacity items in the buffer, so write - read modulo 2 * capacity is not allowed to be greater than capacity.

Required Methods§

source

unsafe fn hold_read(&self, flag: bool)

source

unsafe fn hold_write(&self, flag: bool)

Provided Methods§

source

fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item>

Pushes an item to the ring buffer overwriting the latest item if the buffer is full.

Returns overwritten item if overwriting took place.

source

fn push_iter_overwrite<I>(&mut self, iter: I)where I: Iterator<Item = Self::Item>,

Appends items from an iterator to the ring buffer.

This method consumes iterator until its end. Exactly last min(iter.len(), capacity) items from the iterator will be stored in the ring buffer.

source

fn push_slice_overwrite(&mut self, elems: &[Self::Item])where Self::Item: Copy,

Appends items from slice to the ring buffer overwriting existing items in the ring buffer.

If the slice length is greater than ring buffer capacity then only last capacity items from slice will be stored in the buffer.

Implementors§

source§

impl<D> RingBuffer for Dwhere D: DelegateRingBuffer, <D as Based>::Base: RingBuffer,

source§

impl<S> RingBuffer for LocalRb<S>where S: Storage,

source§

impl<S> RingBuffer for SharedRb<S>where S: Storage,

source§

impl<S: Storage> RingBuffer for AsyncRb<S>