Trait ringbuf::traits::consumer::Consumer

source ·
pub trait Consumer: Observer {
Show 19 methods // Required method unsafe fn set_read_index(&self, value: usize); // Provided methods unsafe fn advance_read_index(&self, count: usize) { ... } fn occupied_slices( &self ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>]) { ... } unsafe fn occupied_slices_mut( &mut self ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>]) { ... } fn as_slices(&self) -> (&[Self::Item], &[Self::Item]) { ... } fn as_mut_slices(&mut self) -> (&mut [Self::Item], &mut [Self::Item]) { ... } fn first(&self) -> Option<&Self::Item> { ... } fn first_mut(&mut self) -> Option<&mut Self::Item> { ... } fn last(&self) -> Option<&Self::Item> { ... } fn last_mut(&mut self) -> Option<&mut Self::Item> { ... } fn try_pop(&mut self) -> Option<Self::Item> { ... } fn pop_slice_uninit( &mut self, elems: &mut [MaybeUninit<Self::Item>] ) -> usize { ... } fn pop_slice(&mut self, elems: &mut [Self::Item]) -> usize where Self::Item: Copy { ... } fn pop_iter(&mut self) -> PopIter<'_, Self> { ... } fn iter(&self) -> Iter<'_, Self> { ... } fn iter_mut(&mut self) -> IterMut<'_, Self> { ... } fn skip(&mut self, count: usize) -> usize { ... } fn clear(&mut self) -> usize { ... } fn write_into<S: Write>( &mut self, writer: &mut S, count: Option<usize> ) -> Option<Result<usize>> where Self: Consumer<Item = u8> { ... }
}
Expand description

Consumer part of ring buffer.

Required Methods§

source

unsafe fn set_read_index(&self, value: usize)

Set read index.

§Safety

Index must go only forward, never backward. It is recommended to use Self::advance_read_index instead.

All slots with index less than value must be uninitialized until write index, all slots with index equal or greater - must be initialized.

Provided Methods§

source

unsafe fn advance_read_index(&self, count: usize)

Moves read pointer by count places forward.

§Safety

First count items in occupied memory must be moved out or dropped.

Must not be called concurrently.

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.

Returns a pair of slices of stored items, the second one may be empty. Elements with lower indices in slice are older. First slice contains older items that second one.

§Safety

All items are initialized. Elements must be removed starting from the beginning of first slice. When all items are removed from the first slice then items must be removed from the beginning of the second slice.

This method must be followed by Self::advance_read_index call with the number of items being removed previously as argument. No other mutating calls allowed before that.

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.

Same as Self::occupied_slices.

§Safety

When some item is replaced with uninitialized value then it must not be read anymore.

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.

Returned item may not be actually the most recent if there is a concurrent producer activity.

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.

Returned item may not be actually the most recent if there is a concurrent producer activity.

source

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

Removes the eldest item from the ring buffer and returns it.

Returns None if the ring buffer is empty.

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.

Returns count of items been removed.

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.

Returns count of items been removed.

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.

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

source

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

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 at most count and at least min(count, Self::len()) items from the buffer and safely drops them.

If there is no concurring producer activity then exactly min(count, Self::len()) items are removed.

Returns the number of deleted items.

let mut rb = LocalRb::<Array<i32, 8>>::default();

assert_eq!(rb.push_iter(0..8), 8);

assert_eq!(rb.skip(4), 4);
assert_eq!(rb.skip(8), 4);
assert_eq!(rb.skip(4), 0);
source

fn clear(&mut self) -> usize

Removes all items from the buffer and safely drops them.

Returns the number of deleted items.

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.

Returns:

  • None: ring buffer is full or count is 0. In this case write isn’t called at all.
  • Some(Ok(n)): write succeeded. n is number of bytes been written. n == 0 means that write also returned 0.
  • Some(Err(e)): write is failed and e is original error. In this case it is guaranteed that no items was written to the writer. To achieve this we write only one contiguous slice at once. So this call may write less than occupied_len items even if the writer is ready to get more.

Object Safety§

This trait is not object safe.

Implementors§