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

A simple multi-producer, multi-consumer blocking queue.

This queue allows producers and consumers to respectively send and receive data from the provided buffer.

A blocking queue can be paired with BiChannelIO to maintain an input buffer:

use lc3_ensemble::sim::io::{BiChannelIO, BlockingQueue, Stop};
 
let queue = BlockingQueue::new(None);
 
// fn writer(_: u8) -> Result<(), Stop> { ... }
// let mcr = ...
 
let io = BiChannelIO::new(queue.reader(), writer, mcr);

Implementations§

source§

impl<T> BlockingQueue<T>

source

pub fn new(size: Option<usize>) -> Self

Creates a new blocking queue, optionally with a maximum size.

source

pub fn push(&self, t: T)

Adds an element to the front of the queue, blocking if the queue is full.

source

pub fn try_push(&self, t: T) -> Result<(), TrySendError<T>>

Adds an element to the front of the queue, returning an error if the queue is full.

This uses crossbeam_channel::TrySendError as its error, but this function cannot return TrySendError::Disconnected, since the BlockingQueue has to exist to call this function.

source

pub fn pop(&self) -> T

Removes an element from the back of the queue, blocking if the queue is full.

source

pub fn try_pop(&self) -> Result<T, TryRecvError>

Removes an element from the back of the queue, returning an error if the queue is full.

This uses crossbeam_channel::TryRecvError as its error, but this function cannot return TryRecvError::Disconnected, since the BlockingQueue has to exist to call this function.

source

pub fn head(&self) -> Sender<T>

Exposes the sending head of the queue.

This enables all methods of crossbeam_channel::Sender to be used. However, because the sender’s lifetime is not dependent on the queue’s lifetime, users of this function should verify that the sender is not disconnected when calling Sender::send and similar functions.

source

pub fn tail(&self) -> Receiver<T>

Exposes the receiving tail of the queue.

This enables all methods of crossbeam_channel::Receiver to be used. However, because the receiver’s lifetime is not dependent on the queue’s lifetime, users of this function should verify that the sender is not disconnected when calling Receiver::recv and similar functions.

source

pub fn len(&self) -> usize

Returns the number of bytes currently in the channel.

source

pub fn is_empty(&self) -> bool

Returns whether there are any bytes in the channel (true if not).

source

pub fn reader(&self) -> impl Fn() -> Result<T, Stop>

A utility to allow this queue to interop with BiChannelIO.

This can be used as the reader parameter of BiChannelIO::new to allow the IO device to poll this queue for data. See the struct-level documentation for an example.

Trait Implementations§

source§

impl<T> Default for BlockingQueue<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<E> Extend<E> for BlockingQueue<E>

source§

fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BlockingQueue<T>

§

impl<T> RefUnwindSafe for BlockingQueue<T>

§

impl<T> Send for BlockingQueue<T>
where T: Send,

§

impl<T> Sync for BlockingQueue<T>
where T: Send,

§

impl<T> Unpin for BlockingQueue<T>
where T: Unpin,

§

impl<T> UnwindSafe for BlockingQueue<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>,

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V