Trait ringbuf::traits::producer::Producer

source ·
pub trait Producer: Observer {
    // Required method
    unsafe fn set_write_index(&self, value: usize);

    // Provided methods
    unsafe fn advance_write_index(&self, count: usize) { ... }
    fn vacant_slices(
        &self
    ) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>]) { ... }
    fn vacant_slices_mut(
        &mut self
    ) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>]) { ... }
    fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item> { ... }
    fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, iter: I) -> usize { ... }
    fn push_slice(&mut self, elems: &[Self::Item]) -> usize
       where Self::Item: Copy { ... }
    fn read_from<S: Read>(
        &mut self,
        reader: &mut S,
        count: Option<usize>
    ) -> Option<Result<usize>>
       where Self: Producer<Item = u8> { ... }
}
Expand description

Producer part of ring buffer.

Required Methods§

source

unsafe fn set_write_index(&self, value: usize)

Set read index.

§Safety

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

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

Provided Methods§

source

unsafe fn advance_write_index(&self, count: usize)

Moves write pointer by count places forward.

§Safety

First count items in free space must be initialized.

Must not be called concurrently.

source

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

Provides a direct access to the ring buffer vacant memory.

Returns a pair of slices of uninitialized memory, the second one may be empty.

source

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

Mutable version of Self::vacant_slices.

Vacant memory is uninitialized. Initialized items must be put starting from the beginning of first slice. When first slice is fully filled then items must be put to the beginning of the second slice.

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

Vacant slices must not be used to store any data because their contents aren’t synchronized properly.

source

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

Appends an item to the ring buffer.

If buffer is full returns an Err containing the item that hasn’t been appended.

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.

Returns count of items been appended to the ring buffer.

Inserted items are committed to the ring buffer all at once in the end, e.g. when buffer is full or iterator has ended.

source

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

Appends items from slice to the ring buffer.

Returns count of items been appended to the ring buffer.

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.

Returns:

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

Object Safety§

This trait is not object safe.

Implementors§