Struct lc3_ensemble::sim::io::BlockingQueue
source · 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>
impl<T> BlockingQueue<T>
sourcepub fn new(size: Option<usize>) -> Self
pub fn new(size: Option<usize>) -> Self
Creates a new blocking queue, optionally with a maximum size.
sourcepub fn push(&self, t: T)
pub fn push(&self, t: T)
Adds an element to the front of the queue, blocking if the queue is full.
sourcepub fn try_push(&self, t: T) -> Result<(), TrySendError<T>>
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.
sourcepub fn pop(&self) -> T
pub fn pop(&self) -> T
Removes an element from the back of the queue, blocking if the queue is full.
sourcepub fn try_pop(&self) -> Result<T, TryRecvError>
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.
sourcepub fn head(&self) -> Sender<T>
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.
sourcepub fn tail(&self) -> Receiver<T>
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.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether there are any bytes in the channel (true if not).
sourcepub fn reader(&self) -> impl Fn() -> Result<T, Stop>
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>
impl<T> Default for BlockingQueue<T>
source§impl<E> Extend<E> for BlockingQueue<E>
impl<E> Extend<E> for BlockingQueue<E>
source§fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)