ringbuf::rb::shared

Struct SharedRb

Source
pub struct SharedRb<S: Storage + ?Sized> { /* private fields */ }
Expand description

Ring buffer that can be shared between threads.

Note that there is no explicit requirement of T: Send. Instead ring buffer will work just fine even with T: !Send until you try to send its producer or consumer to another thread.

use std::thread;
use ringbuf::{SharedRb, storage::Heap, traits::*};

let rb = SharedRb::<Heap<i32>>::new(256);
let (mut prod, mut cons) = rb.split();
thread::spawn(move || {
    prod.try_push(123).unwrap();
})
.join();
thread::spawn(move || {
    assert_eq!(cons.try_pop().unwrap(), 123);
})
.join();

Implementations§

Source§

impl<S: Storage> SharedRb<S>

Source

pub unsafe fn from_raw_parts(storage: S, read: usize, write: usize) -> Self

Constructs ring buffer from storage and indices.

§Safety

The items in storage inside read..write range must be initialized, items outside this range must be uninitialized. read and write positions must be valid (see implementation details).

Source

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

Destructures ring buffer into underlying storage and read and write indices.

§Safety

Initialized contents of the storage must be properly dropped.

Source§

impl<T> SharedRb<Heap<T>>

Source

pub fn new(capacity: usize) -> Self

Creates a new instance of a ring buffer.

Panics if allocation failed or capacity is zero.

Source

pub fn try_new(capacity: usize) -> Result<Self, TryReserveError>

Creates a new instance of a ring buffer returning an error if allocation failed.

Panics if capacity is zero.

Trait Implementations§

Source§

impl<S: Storage + ?Sized> AsMut<SharedRb<S>> for SharedRb<S>

Source§

fn as_mut(&mut self) -> &mut Self

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<S: Storage + ?Sized> AsRef<SharedRb<S>> for SharedRb<S>

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<S: Storage + ?Sized> Consumer for SharedRb<S>

Source§

unsafe fn set_read_index(&self, value: usize)

Set read index. Read more
Source§

unsafe fn advance_read_index(&self, count: usize)

Moves read pointer by count places forward. Read more
Source§

fn occupied_slices( &self, ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])

Provides a direct access to the ring buffer occupied memory. The difference from Self::as_slices is that this method provides slices of MaybeUninit, so items may be moved out of slices. Read more
Source§

unsafe fn occupied_slices_mut( &mut self, ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])

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

fn as_slices(&self) -> (&[Self::Item], &[Self::Item])

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

fn as_mut_slices(&mut self) -> (&mut [Self::Item], &mut [Self::Item])

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

fn first(&self) -> Option<&Self::Item>

Returns a reference to the eldest item in the ring buffer, if exists.
Source§

fn first_mut(&mut self) -> Option<&mut Self::Item>

Returns a mutable reference to the eldest item in the ring buffer, if exists.
Source§

fn last(&self) -> Option<&Self::Item>

Returns a reference to the most recent item in the ring buffer, if exists. Read more
Source§

fn last_mut(&mut self) -> Option<&mut Self::Item>

Returns a mutable reference to the most recent item in the ring buffer, if exists. Read more
Source§

fn try_pop(&mut self) -> Option<Self::Item>

Removes the eldest item from the ring buffer and returns it. Read more
Source§

fn try_peek(&self) -> Option<&Self::Item>

Returns the reference to the eldest item without removing it from the buffer. Read more
Source§

fn peek_slice_uninit(&self, elems: &mut [MaybeUninit<Self::Item>]) -> usize

Copies items from the ring buffer to an uninit slice without removing them from the ring buffer. Read more
Source§

fn peek_slice(&self, elems: &mut [Self::Item]) -> usize
where Self::Item: Copy,

Copies items from the ring buffer to a slice without removing them from the ring buffer. Read more
Source§

fn pop_slice_uninit(&mut self, elems: &mut [MaybeUninit<Self::Item>]) -> usize

Removes items from the ring buffer and writes them into an uninit slice. Read more
Source§

fn pop_slice(&mut self, elems: &mut [Self::Item]) -> usize
where Self::Item: Copy,

Removes items from the ring buffer and writes them into a slice. Read more
Source§

fn pop_iter(&mut self) -> PopIter<'_, Self>

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

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

Returns a front-to-back iterator containing references to items in the ring buffer. Read more
Source§

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

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 at most count and at least min(count, Self::len()) 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 write_into<S: Write>( &mut self, writer: &mut S, count: Option<usize>, ) -> Option<Result<usize>>
where Self: Consumer<Item = u8>,

Removes at most first count bytes from the ring buffer and writes them into a Write instance. If count is None then as much as possible bytes will be written. Read more
Source§

impl<T, const N: usize> Default for SharedRb<Array<T, N>>

Source§

fn default() -> Self

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

impl<S: Storage + ?Sized> Drop for SharedRb<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, const N: usize> From<[T; N]> for SharedRb<Array<T, N>>

Source§

fn from(value: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Box<[T]>> for SharedRb<Heap<T>>

Source§

fn from(value: Box<[T]>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for SharedRb<Heap<T>>

Source§

fn from(value: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<S: Storage> IntoIterator for SharedRb<S>
where Self: Sized,

Source§

type Item = <SharedRb<S> as Observer>::Item

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<SharedRb<S>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<S: Storage + ?Sized> Observer for SharedRb<S>

Source§

type Item = <S as Storage>::Item

Source§

fn capacity(&self) -> NonZeroUsize

Capacity of the ring buffer. Read more
Source§

fn read_index(&self) -> usize

Index of the last item in the ring buffer. Read more
Source§

fn write_index(&self) -> usize

Index of the next empty slot in the ring buffer. Read more
Source§

unsafe fn unsafe_slices( &self, start: usize, end: usize, ) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>])

Get slice between start and end indices. Read more
Source§

unsafe fn unsafe_slices_mut( &self, start: usize, end: usize, ) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>])

Get mutable slice between start and end indices. Read more
Source§

fn read_is_held(&self) -> bool

Whether read end is held by consumer.
Source§

fn write_is_held(&self) -> bool

Whether write end is held by producer.
Source§

fn occupied_len(&self) -> usize

The number of items stored in the buffer. Read more
Source§

fn vacant_len(&self) -> usize

The number of remaining free places in the buffer. Read more
Source§

fn is_empty(&self) -> bool

Checks if the ring buffer is empty. Read more
Source§

fn is_full(&self) -> bool

Checks if the ring buffer is full. Read more
Source§

impl<S: Storage + ?Sized> Producer for SharedRb<S>

Source§

unsafe fn set_write_index(&self, value: usize)

Set read index. Read more
Source§

unsafe fn advance_write_index(&self, count: usize)

Moves write pointer by count places forward. Read more
Source§

fn vacant_slices( &self, ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])

Provides a direct access to the ring buffer vacant memory. Read more
Source§

fn vacant_slices_mut( &mut self, ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])

Mutable version of Self::vacant_slices. Read more
Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Appends an item to the ring buffer. Read more
Source§

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

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

fn push_slice(&mut self, elems: &[Self::Item]) -> usize
where Self::Item: Copy,

Appends items from slice to the ring buffer. Read more
Source§

fn read_from<S: Read>( &mut self, reader: &mut S, count: Option<usize>, ) -> Option<Result<usize>>
where Self: Producer<Item = u8>,

Reads at most count bytes from Read instance and appends them to the ring buffer. If count is None then as much as possible bytes will be read. Read more
Source§

impl<S: Storage> Read for SharedRb<S>
where Self: Consumer<Item = u8>,

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl<S: Storage + ?Sized> RingBuffer for SharedRb<S>

Source§

unsafe fn hold_read(&self, flag: bool) -> bool

Tell whether read end of the ring buffer is held by consumer or not. Read more
Source§

unsafe fn hold_write(&self, flag: bool) -> bool

Tell whether write end of the ring buffer is held by producer or not. Read more
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. Read more
Source§

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

Appends items from an iterator to the ring buffer. Read more
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. Read more
Source§

impl<S: Storage + ?Sized> Split for Box<SharedRb<S>>

Source§

type Prod = Caching<Arc<SharedRb<S>>, true, false>

Producer type.
Source§

type Cons = Caching<Arc<SharedRb<S>>, false, true>

Consumer type.
Source§

fn split(self) -> (Self::Prod, Self::Cons)

Perform splitting.
Source§

impl<S: Storage> Split for SharedRb<S>

Source§

type Prod = Caching<Arc<SharedRb<S>>, true, false>

Producer type.
Source§

type Cons = Caching<Arc<SharedRb<S>>, false, true>

Consumer type.
Source§

fn split(self) -> (Self::Prod, Self::Cons)

Perform splitting.
Source§

impl<S: Storage + ?Sized> SplitRef for SharedRb<S>

Source§

type RefProd<'a> = Caching<&'a SharedRb<S>, true, false> where Self: 'a

Ref producer type.
Source§

type RefCons<'a> = Caching<&'a SharedRb<S>, false, true> where Self: 'a

Ref consumer type.
Source§

fn split_ref(&mut self) -> (Self::RefProd<'_>, Self::RefCons<'_>)

Perform splitting by reference.
Source§

impl<S: Storage> Write for SharedRb<S>
where Self: Producer<Item = u8>,

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl<S: Storage> Write for SharedRb<S>
where Self: Producer<Item = u8>,

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<S> !Freeze for SharedRb<S>

§

impl<S> RefUnwindSafe for SharedRb<S>
where S: RefUnwindSafe + ?Sized,

§

impl<S> Send for SharedRb<S>
where S: Send + ?Sized,

§

impl<S> Sync for SharedRb<S>
where S: Sync + ?Sized,

§

impl<S> Unpin for SharedRb<S>
where S: Unpin + ?Sized,

§

impl<S> UnwindSafe for SharedRb<S>
where S: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.