pub struct LocalRb<T, C: Container<T>> { /* private fields */ }
Expand description

Ring buffer for using in single thread.

Does not implement Sync. And its Producer and Consumer do not implement Send.

This code must fail to compile:

use std::{thread, vec::Vec};
use ringbuf::LocalRb;

let (mut prod, mut cons) = LocalRb::<i32, Vec<_>>::new(256).split();
thread::spawn(move || {
    prod.push(123).unwrap();
})
.join();
thread::spawn(move || {
    assert_eq!(cons.pop().unwrap(), 123);
})
.join();

Implementations§

source§

impl<T> LocalRb<T, Vec<MaybeUninit<T>>>

source

pub fn new(capacity: usize) -> Self

Creates a new instance of a ring buffer.

Panics if capacity is zero.

source§

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

source

pub unsafe fn from_raw_parts(container: C, head: usize, tail: usize) -> Self

Constructs ring buffer from container and counters.

Safety

The items in container inside head..tail range must be initialized, items outside this range must be uninitialized. head and tail values must be valid (see RbBase).

source

pub unsafe fn into_raw_parts(self) -> (C, usize, usize)

Destructures ring buffer into underlying container and head and tail counters.

Safety

Initialized contents of the container must be properly dropped.

source

pub fn split(self) -> (Producer<T, Rc<Self>>, Consumer<T, Rc<Self>>)where Self: Sized,

Splits ring buffer into producer and consumer.

This method consumes the ring buffer and puts it on heap in Rc. If you don’t want to use heap the see Self::split_ref.

source

pub fn split_ref(&mut self) -> (Producer<T, &Self>, Consumer<T, &Self>)where Self: Sized,

Splits ring buffer into producer and consumer without using the heap.

In this case producer and consumer stores a reference to the ring buffer, so you also need to store the buffer somewhere.

Trait Implementations§

source§

impl<T, const N: usize> Default for LocalRb<T, [MaybeUninit<T>; N]>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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

source§

fn capacity(&self) -> usize

Returns capacity of the ring buffer. Read more
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. Read more
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. Read more
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. Read more
source§

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

Removes exactly n items from the buffer and safely drops them. Read more
source§

fn clear(&mut self) -> usize

Removes all items from the buffer and safely drops them. Read more
source§

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

Appends an item to the ring buffer. Read more
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. Read more
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. Read more
source§

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

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. Read more
source§

fn capacity_nonzero(&self) -> NonZeroUsize

Capacity of the ring buffer. Read more
source§

fn head(&self) -> usize

Head position.
source§

fn tail(&self) -> usize

Tail position.
source§

fn modulus(&self) -> NonZeroUsize

Modulus for head and tail values. Read more
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.
source§

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

source§

unsafe fn set_head(&self, value: usize)

Sets the new head position. Read more
source§

unsafe fn advance_head(&self, count: usize)

Move head position by count items forward. Read more
source§

fn occupied_ranges(&self) -> (Range<usize>, Range<usize>)

Returns a pair of ranges of Self::occupied_slices location in underlying container.
source§

unsafe fn occupied_slices( &self ) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])

Provides a direct mutable access to the ring buffer occupied memory. Read more
source§

unsafe fn skip_internal(&self, count_or_all: Option<usize>) -> usize

Removes items from the head of ring buffer and drops them. Read more
source§

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

source§

unsafe fn set_tail(&self, value: usize)

Sets the new tail position. Read more
source§

unsafe fn advance_tail(&self, count: usize)

Move tail position by count items forward. Read more
source§

fn vacant_ranges(&self) -> (Range<usize>, Range<usize>)

Returns a pair of ranges of Self::vacant_slices location in underlying container.
source§

unsafe fn vacant_slices(&self) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])

Provides a direct access to the ring buffer vacant memory. Returns a pair of slices of uninitialized memory, the second one may be empty. Read more

Auto Trait Implementations§

§

impl<T, C> !RefUnwindSafe for LocalRb<T, C>

§

impl<T, C> Send for LocalRb<T, C>where T: Send, <C as Container<T>>::Internal: Send,

§

impl<T, C> !Sync for LocalRb<T, C>

§

impl<T, C> Unpin for LocalRb<T, C>where T: Unpin, <C as Container<T>>::Internal: Unpin,

§

impl<T, C> UnwindSafe for LocalRb<T, C>where T: UnwindSafe, <C as Container<T>>::Internal: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.