Trait ringbuf::ring_buffer::RbBase
source · 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§
sourceunsafe fn slices(
&self,
head: usize,
tail: usize
) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])
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.
sourcefn capacity_nonzero(&self) -> NonZeroUsize
fn capacity_nonzero(&self) -> NonZeroUsize
Capacity of the ring buffer.
It is constant during the whole ring buffer lifetime.
Provided Methods§
sourcefn modulus(&self) -> NonZeroUsize
fn modulus(&self) -> NonZeroUsize
Modulus for head
and tail
values.
Equals to 2 * len
.
sourcefn occupied_len(&self) -> usize
fn occupied_len(&self) -> usize
The number of items stored in the buffer at the moment.
sourcefn vacant_len(&self) -> usize
fn vacant_len(&self) -> usize
The number of vacant places in the buffer at the moment.