pub trait RbBase<T> {
    // Required methods
    unsafe fn slices(
        &self,
        head: usize,
        tail: usize
    ) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>]);
    fn capacity_nonzero(&self) -> NonZeroUsize;
    fn head(&self) -> usize;
    fn tail(&self) -> usize;

    // Provided methods
    fn modulus(&self) -> NonZeroUsize { ... }
    fn occupied_len(&self) -> usize { ... }
    fn vacant_len(&self) -> usize { ... }
    fn is_empty(&self) -> bool { ... }
    fn is_full(&self) -> bool { ... }
}
Expand description

Basic ring buffer functionality.

Provides an access to raw underlying memory and head/tail counters.

It is recommended not to use this trait directly. Use Producer and Consumer instead.

Details

The ring buffer consists of an array (of capacity size) and two counters: head and tail. When an item is extracted from the ring buffer it is taken from the head position and after that head is incremented. New item is appended to the tail position and tail is incremented after that.

The head and tail counters are modulo 2 * capacity (not just capacity). It allows us to distinguish situations when the buffer is empty (head == tail) and when the buffer is full (tail - head 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 tail - head modulo 2 * capacity is not allowed to be greater than capacity.

Required Methods§

source

unsafe fn slices( &self, head: usize, tail: usize ) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])

Returns part of underlying raw ring buffer memory as slices.

For more information see SharedStorage::as_mut_slices.

Safety

Only non-overlapping slices allowed to exist at the same time.

Modifications of this data must properly update head and tail positions.

Accessing raw data is extremely unsafe. It is recommended to use Consumer::as_slices and Producer::free_space_as_slices instead.

source

fn capacity_nonzero(&self) -> NonZeroUsize

Capacity of the ring buffer.

It is constant during the whole ring buffer lifetime.

source

fn head(&self) -> usize

Head position.

source

fn tail(&self) -> usize

Tail position.

Provided Methods§

source

fn modulus(&self) -> NonZeroUsize

Modulus for head and tail values.

Equals to 2 * len.

source

fn occupied_len(&self) -> usize

The number of items stored in the buffer at the moment.

source

fn vacant_len(&self) -> usize

The number of vacant places in the buffer at the moment.

source

fn is_empty(&self) -> bool

Checks if the occupied range is empty.

source

fn is_full(&self) -> bool

Checks if the vacant range is empty.

Implementors§

source§

impl<T, C: Container<T>> RbBase<T> for LocalRb<T, C>

source§

impl<T, C: Container<T>> RbBase<T> for SharedRb<T, C>

source§

impl<T, R: RbRef> RbBase<T> for RbReadCache<T, R>where R::Rb: RbRead<T>,

source§

impl<T, R: RbRef> RbBase<T> for RbWriteCache<T, R>where R::Rb: RbWrite<T>,