pub struct Producer<T> { /* private fields */ }
Expand description
Producer part of ring buffer.
Implementations
sourceimpl<T: Sized> Producer<T>
impl<T: Sized> Producer<T>
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns capacity of the ring buffer.
The capacity of the buffer is constant.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the ring buffer is empty.
The result is relevant until you push items to the producer.
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Checks if the ring buffer is full.
The result may become irrelevant at any time because of concurring activity of the consumer.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The length of the data stored in the buffer.
Actual length may be equal to or less than the returned value.
sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
The remaining space in the buffer.
Actual remaining space may be equal to or greater than the returning value.
sourcepub unsafe fn push_access<F>(&mut self, f: F) -> usizewhere
F: FnOnce(&mut [MaybeUninit<T>], &mut [MaybeUninit<T>]) -> usize,
pub unsafe fn push_access<F>(&mut self, f: F) -> usizewhere
F: FnOnce(&mut [MaybeUninit<T>], &mut [MaybeUninit<T>]) -> usize,
Allows to write into ring buffer memory directly.
This function is unsafe because it gives access to possibly uninitialized memory
The method takes a function f
as argument.
f
takes two slices of ring buffer content (the second one or both of them may be empty).
First slice contains older elements.
f
should return number of elements been written.
There is no checks for returned number - it remains on the developer’s conscience.
The method always calls f
even if ring buffer is full.
The method returns number returned from f
.
Safety
The method gives access to ring buffer underlying memory which may be uninitialized.
sourcepub unsafe fn push_copy(&mut self, elems: &[MaybeUninit<T>]) -> usize
pub unsafe fn push_copy(&mut self, elems: &[MaybeUninit<T>]) -> usize
Copies data from the slice to the ring buffer in byte-to-byte manner.
The elems
slice should contain initialized data before the method call.
After the call the copied part of data in elems
should be interpreted as un-initialized.
Returns the number of items been copied.
Safety
The method copies raw data into the ring buffer.
You should properly fill the slice and manage remaining elements after copy.
sourcepub fn push(&mut self, elem: T) -> Result<(), T>
pub fn push(&mut self, elem: T) -> Result<(), T>
Appends an element to the ring buffer. On failure returns an error containing the element that hasn’t been appended.
sourcepub fn push_each<F: FnMut() -> Option<T>>(&mut self, f: F) -> usize
pub fn push_each<F: FnMut() -> Option<T>>(&mut self, f: F) -> usize
Repeatedly calls the closure f
and pushes elements returned from it to the ring buffer.
The closure is called until it returns None
or the ring buffer is full.
The method returns number of elements been put into the buffer.
sourcepub fn push_iter<I: Iterator<Item = T>>(&mut self, elems: &mut I) -> usize
pub fn push_iter<I: Iterator<Item = T>>(&mut self, elems: &mut I) -> usize
Appends elements from an iterator to the ring buffer. Elements that haven’t been added to the ring buffer remain in the iterator.
Returns count of elements been appended to the ring buffer.
sourcepub fn move_from(
&mut self,
other: &mut Consumer<T>,
count: Option<usize>
) -> usize
pub fn move_from(
&mut self,
other: &mut Consumer<T>,
count: Option<usize>
) -> usize
Removes at most count
elements from the consumer and appends them to the producer.
If count
is None
then as much as possible elements will be moved.
The producer and consumer parts may be of different buffers as well as of the same one.
On success returns number of elements been moved.
sourceimpl<T: Sized + Copy> Producer<T>
impl<T: Sized + Copy> Producer<T>
sourcepub fn push_slice(&mut self, elems: &[T]) -> usize
pub fn push_slice(&mut self, elems: &[T]) -> usize
Appends elements from slice to the ring buffer.
Elements should be Copy
.
Returns count of elements been appended to the ring buffer.
sourceimpl Producer<u8>
impl Producer<u8>
sourcepub fn read_from(
&mut self,
reader: &mut dyn Read,
count: Option<usize>
) -> Result<usize>
pub fn read_from(
&mut self,
reader: &mut dyn Read,
count: Option<usize>
) -> Result<usize>
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 Ok(n)
if read
succeeded. n
is number of bytes been read.
n == 0
means that either read
returned zero or ring buffer is full.
If read
is failed or returned an invalid number then error is returned.
Trait Implementations
sourceimpl Write for Producer<u8>
impl Write for Producer<u8>
sourcefn write(&mut self, buffer: &[u8]) -> Result<usize>
fn write(&mut self, buffer: &[u8]) -> Result<usize>
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)