Struct concurrent_queue::ConcurrentQueue
source · [−]pub struct ConcurrentQueue<T>(_);
Expand description
A concurrent queue.
Examples
use concurrent_queue::{ConcurrentQueue, PopError, PushError};
let q = ConcurrentQueue::bounded(2);
assert_eq!(q.push('a'), Ok(()));
assert_eq!(q.push('b'), Ok(()));
assert_eq!(q.push('c'), Err(PushError::Full('c')));
assert_eq!(q.pop(), Ok('a'));
assert_eq!(q.pop(), Ok('b'));
assert_eq!(q.pop(), Err(PopError::Empty));
Implementations
sourceimpl<T> ConcurrentQueue<T>
impl<T> ConcurrentQueue<T>
sourcepub fn bounded(cap: usize) -> ConcurrentQueue<T>
pub fn bounded(cap: usize) -> ConcurrentQueue<T>
sourcepub fn unbounded() -> ConcurrentQueue<T>
pub fn unbounded() -> ConcurrentQueue<T>
Creates a new unbounded queue.
Examples
use concurrent_queue::ConcurrentQueue;
let q = ConcurrentQueue::<i32>::unbounded();
sourcepub fn push(&self, value: T) -> Result<(), PushError<T>>
pub fn push(&self, value: T) -> Result<(), PushError<T>>
Attempts to push an item into the queue.
If the queue is full or closed, the item is returned back as an error.
Examples
use concurrent_queue::{ConcurrentQueue, PushError};
let q = ConcurrentQueue::bounded(1);
// Push succeeds because there is space in the queue.
assert_eq!(q.push(10), Ok(()));
// Push errors because the queue is now full.
assert_eq!(q.push(20), Err(PushError::Full(20)));
// Close the queue, which will prevent further pushes.
q.close();
// Pushing now errors indicating the queue is closed.
assert_eq!(q.push(20), Err(PushError::Closed(20)));
// Pop the single item in the queue.
assert_eq!(q.pop(), Ok(10));
// Even though there is space, no more items can be pushed.
assert_eq!(q.push(20), Err(PushError::Closed(20)));
sourcepub fn pop(&self) -> Result<T, PopError>
pub fn pop(&self) -> Result<T, PopError>
Attempts to pop an item from the queue.
If the queue is empty, an error is returned.
Examples
use concurrent_queue::{ConcurrentQueue, PopError};
let q = ConcurrentQueue::bounded(1);
// Pop errors when the queue is empty.
assert_eq!(q.pop(), Err(PopError::Empty));
// Push one item and close the queue.
assert_eq!(q.push(10), Ok(()));
q.close();
// Remaining items can be popped.
assert_eq!(q.pop(), Ok(10));
// Again, pop errors when the queue is empty,
// but now also indicates that the queue is closed.
assert_eq!(q.pop(), Err(PopError::Closed));
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the queue is empty.
Examples
use concurrent_queue::ConcurrentQueue;
let q = ConcurrentQueue::<i32>::unbounded();
assert!(q.is_empty());
q.push(1).unwrap();
assert!(!q.is_empty());
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true
if the queue is full.
An unbounded queue is never full.
Examples
use concurrent_queue::ConcurrentQueue;
let q = ConcurrentQueue::bounded(1);
assert!(!q.is_full());
q.push(1).unwrap();
assert!(q.is_full());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the queue.
Examples
use concurrent_queue::ConcurrentQueue;
let q = ConcurrentQueue::unbounded();
assert_eq!(q.len(), 0);
assert_eq!(q.push(10), Ok(()));
assert_eq!(q.len(), 1);
assert_eq!(q.push(20), Ok(()));
assert_eq!(q.len(), 2);
sourcepub fn capacity(&self) -> Option<usize>
pub fn capacity(&self) -> Option<usize>
Returns the capacity of the queue.
Unbounded queues have infinite capacity, represented as None
.
Examples
use concurrent_queue::ConcurrentQueue;
let q = ConcurrentQueue::<i32>::bounded(7);
assert_eq!(q.capacity(), Some(7));
let q = ConcurrentQueue::<i32>::unbounded();
assert_eq!(q.capacity(), None);
sourcepub fn close(&self) -> bool
pub fn close(&self) -> bool
Closes the queue.
Returns true
if this call closed the queue, or false
if it was already closed.
When a queue is closed, no more items can be pushed but the remaining items can still be popped.
Examples
use concurrent_queue::{ConcurrentQueue, PopError, PushError};
let q = ConcurrentQueue::unbounded();
assert_eq!(q.push(10), Ok(()));
assert!(q.close()); // `true` because this call closes the queue.
assert!(!q.close()); // `false` because the queue is already closed.
// Cannot push any more items when closed.
assert_eq!(q.push(20), Err(PushError::Closed(20)));
// Remaining items can still be popped.
assert_eq!(q.pop(), Ok(10));
// When no more items are present, the error is `Closed`.
assert_eq!(q.pop(), Err(PopError::Closed));
Trait Implementations
sourceimpl<T> Debug for ConcurrentQueue<T>
impl<T> Debug for ConcurrentQueue<T>
impl<T> RefUnwindSafe for ConcurrentQueue<T>
impl<T: Send> Send for ConcurrentQueue<T>
impl<T: Send> Sync for ConcurrentQueue<T>
impl<T> UnwindSafe for ConcurrentQueue<T>
Auto Trait Implementations
impl<T> Unpin for ConcurrentQueue<T> where
T: Unpin,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more