Consumer

Struct Consumer 

Source
pub struct Consumer<T> { /* private fields */ }
Expand description

A handle to the queue which allows consuming values from the buffer

Implementations§

Source§

impl<T> Consumer<T>

Source

pub fn pop(&self) -> T

Pop a value off the queue.

If the buffer contains values, this method will execute immediately and return a value. If the buffer is empty, this method will block until a value becomes available. The waiting strategy is a simple spin-wait. If you do not want a spin-wait burning CPU, you should call try_push() directly and implement a different waiting strategy.

§Examples
let (_, consumer) = make(100);

// Block until a value becomes available
let t = consumer.pop();
Source

pub fn try_pop(&self) -> Option<T>

Attempt to pop a value off the queue.

This method does not block. If the queue is empty, the method will return None. If there is a value available, the method will return Some(v), where v is the value being popped off the queue.

§Examples
use bounded_spsc_queue::*;

let (_, consumer) = make(100);

// Attempt to pop a value off the queue
let t = consumer.try_pop();
match t {
    Some(v) => {},      // Successfully popped a value
    None => {}          // Queue empty, try again later
}
Source

pub fn skip_n(&self, n: usize) -> usize

Attempts to pop (and discard) at most n values off the buffer.

Returns the amount of values successfully skipped.

§Safety

WARNING: This will leak at most n values from the buffer, i.e. the destructors of the objects skipped over will not be called. This function is intended to be used on buffers that contain non-Drop data, such as a Buffer<f32>.

§Examples
use bounded_spsc_queue::*;

let (_, consumer) = make(100);

let mut read_position = 0; // current buffer index
read_position += consumer.skip_n(512); // try to skip at most 512 elements
Source

pub fn capacity(&self) -> usize

Returns the total capacity of this queue

This value represents the total capacity of the queue when it is full. It does not represent the current usage. For that, call size().

§Examples
let (_, consumer) = make(100);

assert!(consumer.capacity() == 100);
let t = consumer.pop();
assert!(producer.capacity() == 100);
Source

pub fn size(&self) -> usize

Returns the current size of the queue

This value represents the current size of the queue. This value can be from 0-capacity inclusive.

§Examples
let (_, consumer) = make(100);

//... producer pushes somewhere ...

assert!(consumer.size() == 10);
consumer.pop();
assert!(producer.size() == 9);

Trait Implementations§

Source§

impl<T: Send> Send for Consumer<T>

Auto Trait Implementations§

§

impl<T> Freeze for Consumer<T>

§

impl<T> !RefUnwindSafe for Consumer<T>

§

impl<T> !Sync for Consumer<T>

§

impl<T> Unpin for Consumer<T>

§

impl<T> !UnwindSafe for Consumer<T>

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.