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§
Sourcepub unsafe fn from_raw_parts(storage: S, read: usize, write: usize) -> Self
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).
Sourcepub unsafe fn into_raw_parts(self) -> (S, usize, usize)
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.
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new instance of a ring buffer.
Panics if allocation failed or capacity
is zero.
Sourcepub fn try_new(capacity: usize) -> Result<Self, TryReserveError>
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§unsafe fn set_read_index(&self, value: usize)
unsafe fn set_read_index(&self, value: usize)
Source§unsafe fn advance_read_index(&self, count: usize)
unsafe fn advance_read_index(&self, count: usize)
Source§fn occupied_slices(
&self,
) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])
fn occupied_slices( &self, ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])
Self::as_slices
is that this method provides slices of MaybeUninit
, so items may be moved out of slices. Read moreSource§unsafe fn occupied_slices_mut(
&mut self,
) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])
unsafe fn occupied_slices_mut( &mut self, ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])
Source§fn as_slices(&self) -> (&[Self::Item], &[Self::Item])
fn as_slices(&self) -> (&[Self::Item], &[Self::Item])
Source§fn as_mut_slices(&mut self) -> (&mut [Self::Item], &mut [Self::Item])
fn as_mut_slices(&mut self) -> (&mut [Self::Item], &mut [Self::Item])
Source§fn first(&self) -> Option<&Self::Item>
fn first(&self) -> Option<&Self::Item>
Source§fn first_mut(&mut self) -> Option<&mut Self::Item>
fn first_mut(&mut self) -> Option<&mut Self::Item>
Source§fn last(&self) -> Option<&Self::Item>
fn last(&self) -> Option<&Self::Item>
Source§fn last_mut(&mut self) -> Option<&mut Self::Item>
fn last_mut(&mut self) -> Option<&mut Self::Item>
Source§fn try_pop(&mut self) -> Option<Self::Item>
fn try_pop(&mut self) -> Option<Self::Item>
Source§fn try_peek(&self) -> Option<&Self::Item>
fn try_peek(&self) -> Option<&Self::Item>
Source§fn peek_slice_uninit(&self, elems: &mut [MaybeUninit<Self::Item>]) -> usize
fn peek_slice_uninit(&self, elems: &mut [MaybeUninit<Self::Item>]) -> usize
Source§fn peek_slice(&self, elems: &mut [Self::Item]) -> usize
fn peek_slice(&self, elems: &mut [Self::Item]) -> usize
Source§fn pop_slice_uninit(&mut self, elems: &mut [MaybeUninit<Self::Item>]) -> usize
fn pop_slice_uninit(&mut self, elems: &mut [MaybeUninit<Self::Item>]) -> usize
Source§fn pop_slice(&mut self, elems: &mut [Self::Item]) -> usize
fn pop_slice(&mut self, elems: &mut [Self::Item]) -> usize
Source§fn pop_iter(&mut self) -> PopIter<'_, Self> ⓘ
fn pop_iter(&mut self) -> PopIter<'_, Self> ⓘ
Source§fn iter(&self) -> Iter<'_, Self>
fn iter(&self) -> Iter<'_, Self>
Source§fn iter_mut(&mut self) -> IterMut<'_, Self>
fn iter_mut(&mut self) -> IterMut<'_, Self>
Source§fn skip(&mut self, count: usize) -> usize
fn skip(&mut self, count: usize) -> usize
count
and at least min(count, Self::len())
items from the buffer and safely drops them. Read moreSource§fn clear(&mut self) -> usize
fn clear(&mut self) -> usize
type Item = <S as Storage>::Item
Source§fn capacity(&self) -> NonZeroUsize
fn capacity(&self) -> NonZeroUsize
Source§fn read_index(&self) -> usize
fn read_index(&self) -> usize
Source§fn write_index(&self) -> usize
fn write_index(&self) -> usize
Source§unsafe fn unsafe_slices(
&self,
start: usize,
end: usize,
) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>])
unsafe fn unsafe_slices( &self, start: usize, end: usize, ) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>])
Source§unsafe fn unsafe_slices_mut(
&self,
start: usize,
end: usize,
) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>])
unsafe fn unsafe_slices_mut( &self, start: usize, end: usize, ) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>])
Source§fn read_is_held(&self) -> bool
fn read_is_held(&self) -> bool
Source§fn write_is_held(&self) -> bool
fn write_is_held(&self) -> bool
Source§fn occupied_len(&self) -> usize
fn occupied_len(&self) -> usize
Source§fn vacant_len(&self) -> usize
fn vacant_len(&self) -> usize
Source§unsafe fn set_write_index(&self, value: usize)
unsafe fn set_write_index(&self, value: usize)
Source§unsafe fn advance_write_index(&self, count: usize)
unsafe fn advance_write_index(&self, count: usize)
Source§fn vacant_slices(
&self,
) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])
fn vacant_slices( &self, ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])
Source§fn vacant_slices_mut(
&mut self,
) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])
fn vacant_slices_mut( &mut self, ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>])
Self::vacant_slices
. Read moreSource§fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>
fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>
Source§fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, iter: I) -> usize
fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, iter: I) -> usize
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§unsafe fn hold_read(&self, flag: bool) -> bool
unsafe fn hold_read(&self, flag: bool) -> bool
Source§unsafe fn hold_write(&self, flag: bool) -> bool
unsafe fn hold_write(&self, flag: bool) -> bool
Source§fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item>
fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)