Trait ringbuf::Rb

source ·
pub trait Rb<T>: RbRead<T> + RbWrite<T> {
Show 18 methods // Provided methods fn capacity(&self) -> usize { ... } fn len(&self) -> usize { ... } fn free_len(&self) -> usize { ... } fn as_slices(&self) -> (&[T], &[T]) { ... } fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { ... } fn pop(&mut self) -> Option<T> { ... } fn pop_iter(&mut self) -> PopIterator<'_, T, RbWrap<Self>> { ... } fn iter(&self) -> Chain<Iter<'_, T>, Iter<'_, T>> { ... } fn iter_mut(&mut self) -> Chain<IterMut<'_, T>, IterMut<'_, T>> { ... } fn skip(&mut self, count: usize) -> usize { ... } fn clear(&mut self) -> usize { ... } fn push(&mut self, elem: T) -> Result<(), T> { ... } fn push_overwrite(&mut self, elem: T) -> Option<T> { ... } fn push_iter<I: Iterator<Item = T>>(&mut self, iter: &mut I) { ... } fn push_iter_overwrite<I: Iterator<Item = T>>(&mut self, iter: I) { ... } fn pop_slice(&mut self, elems: &mut [T]) where T: Copy { ... } fn push_slice(&mut self, elems: &[T]) where T: Copy { ... } fn push_slice_overwrite(&mut self, elems: &[T]) where T: Copy { ... }
}
Expand description

An abstract ring buffer.

See RbBase for details of internal implementation of the ring buffer.

This trait contains methods that takes &mut self allowing you to use ring buffer without splitting it into Producer and Consumer.

There are push*_overwrite methods that cannot be used from Producer.

The ring buffer can be guarded with mutex or other synchronization primitive and be used from different threads without splitting (but now only in blocking mode, obviously).

Provided Methods§

source

fn capacity(&self) -> usize

Returns capacity of the ring buffer.

The capacity of the buffer is constant.

source

fn len(&self) -> usize

The number of items stored in the ring buffer.

source

fn free_len(&self) -> usize

The number of remaining free places in the buffer.

source

fn as_slices(&self) -> (&[T], &[T])

Returns a pair of slices which contain, in order, the contents of the ring buffer.

source

fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])

Returns a pair of mutable slices which contain, in order, the contents of the ring buffer.

source

fn pop(&mut self) -> Option<T>

Removes latest item from the ring buffer and returns it.

Returns None if the ring buffer is empty.

Examples found in repository?
examples/overwrite.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
fn main() {
    let mut rb = HeapRb::<i32>::new(2);

    assert_eq!(rb.push_overwrite(0), None);
    assert_eq!(rb.push_overwrite(1), None);
    assert_eq!(rb.push_overwrite(2), Some(0));

    assert_eq!(rb.pop(), Some(1));
    assert_eq!(rb.pop(), Some(2));
    assert_eq!(rb.pop(), None);
}
source

fn pop_iter(&mut self) -> PopIterator<'_, T, RbWrap<Self>>

Returns an iterator that removes items one by one from the ring buffer.

source

fn iter(&self) -> Chain<Iter<'_, T>, Iter<'_, T>>

Returns a front-to-back iterator containing references to items in the ring buffer.

This iterator does not remove items out of the ring buffer.

source

fn iter_mut(&mut self) -> Chain<IterMut<'_, T>, IterMut<'_, T>>

Returns a front-to-back iterator that returns mutable references to items in the ring buffer.

This iterator does not remove items out of the ring buffer.

source

fn skip(&mut self, count: usize) -> usize

Removes exactly n items from the buffer and safely drops them.

Panics if n is greater than number of items in the ring buffer.

source

fn clear(&mut self) -> usize

Removes all items from the buffer and safely drops them.

Returns the number of deleted items.

source

fn push(&mut self, elem: T) -> Result<(), T>

Appends an item to the ring buffer.

On failure returns an Err containing the item that hasn’t been appended.

source

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

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

Returns overwritten item if overwriting took place.

Examples found in repository?
examples/overwrite.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
fn main() {
    let mut rb = HeapRb::<i32>::new(2);

    assert_eq!(rb.push_overwrite(0), None);
    assert_eq!(rb.push_overwrite(1), None);
    assert_eq!(rb.push_overwrite(2), Some(0));

    assert_eq!(rb.pop(), Some(1));
    assert_eq!(rb.pop(), Some(2));
    assert_eq!(rb.pop(), None);
}
source

fn push_iter<I: Iterator<Item = T>>(&mut self, iter: &mut I)

Appends items from an iterator to the ring buffer. Elements that haven’t been added to the ring buffer remain in the iterator.

source

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

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 pop_slice(&mut self, elems: &mut [T])where T: Copy,

Removes first items from the ring buffer and writes them into a slice. Elements must be Copy.

Panics if slice length is greater than number of items in the ring buffer.

source

fn push_slice(&mut self, elems: &[T])where T: Copy,

Appends items from slice to the ring buffer. Elements must be Copy.

Panics if slice length is greater than number of free places in the ring buffer.

source

fn push_slice_overwrite(&mut self, elems: &[T])where T: 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<T, C: Container<T>> Rb<T> for LocalRb<T, C>

source§

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