Consumer

Struct Consumer 

Source
pub struct Consumer<'a, const N: usize> { /* private fields */ }
Expand description

A Consumer is a smart pointer to a Buffer, which is endowed with the right to remove data from the buffer. Only one Consumer may exist at one time for any given buffer. The methods of a Consumer are the only way to read data out of a Buffer.

Implementations§

Source§

impl<'a, const N: usize> Consumer<'a, N>

Source

pub fn read<'b>(&'b mut self, target_len: usize) -> Region<'b, Self>

Return a Region for up to target_len bytes to be read from the buffer. The returned region may be shorter than target_len. The returned region has length zero if and only if the buffer is empty. The returned region is guaranteed to be not longer than target_len. To read the largest possible length, set target_len = usize::MAX.

Even though we are reading from the buffer, the Region which is returned is mutable. Its memory is available for arbitrary use by the caller for as long as the Region remains in scope.

Examples found in repository?
examples/two_threads.rs (line 19)
16fn consumer(mut c: fring::Consumer<N>) {
17    loop {
18        std::thread::sleep(std::time::Duration::from_millis(60));
19        let r = c.read(usize::MAX);
20        if r.len() == 0 {  // buffer is empty
21            break;
22        }
23        println!("            read \"{}\"", std::str::from_utf8(&*r).unwrap());
24    }
25}
Source

pub unsafe fn read_ref<T: Copy + ?Sized>( &mut self, item: &mut T, ) -> Result<(), ()>

If the buffer contains enough bytes to make an instance of T, then write them into *item and return Ok. Otherwise, return Err. UNSAFE: caller must guarantee that the bytes contained in the buffer constitute a valid instance of T. In consequence, if T is an integer type or an integer array or slice type, then it is safe to call this function.

Source

pub fn data_size(&self) -> usize

Return the amount of data currently stored in the buffer. If the producer is writing concurrently with this call, then the amount of data may increase, but it will not decrease below the value which is returned.

Source

pub fn flush(&mut self)

Discard all data which is currently stored in the buffer. If the producer is writing concurrently with this call, then the producer’s newest data may not be discarded.

Auto Trait Implementations§

§

impl<'a, const N: usize> Freeze for Consumer<'a, N>

§

impl<'a, const N: usize> !RefUnwindSafe for Consumer<'a, N>

§

impl<'a, const N: usize> Send for Consumer<'a, N>

§

impl<'a, const N: usize> Sync for Consumer<'a, N>

§

impl<'a, const N: usize> Unpin for Consumer<'a, N>

§

impl<'a, const N: usize> !UnwindSafe for Consumer<'a, N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.