Trait Producer
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>(&mut self, iter: I) -> usize
where I: Iterator<Item = Self::Item> { ... }
fn push_slice(&mut self, elems: &[Self::Item]) -> usize
where Self::Item: Copy { ... }
fn read_from<S>(
&mut self,
reader: &mut S,
count: Option<usize>,
) -> Option<Result<usize, Error>>
where S: Read,
Self: Producer<Item = u8> { ... }
}
Expand description
Producer part of ring buffer.
Required Methods§
unsafe fn set_write_index(&self, value: usize)
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§
unsafe fn advance_write_index(&self, count: usize)
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.
fn vacant_slices(
&self,
) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>])
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.
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>])
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.
fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>
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.
fn push_iter<I>(&mut self, iter: I) -> usize
fn push_iter<I>(&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.
fn push_slice(&mut self, elems: &[Self::Item]) -> usize
fn push_slice(&mut self, elems: &[Self::Item]) -> usize
Appends items from slice to the ring buffer.
Returns count of items been appended to the ring buffer.
fn read_from<S>(
&mut self,
reader: &mut S,
count: Option<usize>,
) -> Option<Result<usize, Error>>
fn read_from<S>( &mut self, reader: &mut S, count: Option<usize>, ) -> Option<Result<usize, Error>>
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 orcount
is0
. In this caseread
isn’t called at all.Some(Ok(n))
:read
succeeded.n
is number of bytes been read.n == 0
means thatread
also returned0
.Some(Err(e))
read
is failed ande
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 thanvacant_len
items in the buffer even if the reader is ready to provide more.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.