concurrent_queue

Struct ConcurrentQueue

Source
pub struct ConcurrentQueue<T>(/* private fields */);
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§

Source§

impl<T> ConcurrentQueue<T>

Source

pub fn bounded(cap: usize) -> ConcurrentQueue<T>

Creates a new bounded queue.

The queue allocates enough space for cap items.

§Panics

If the capacity is zero, this constructor will panic.

§Examples
use concurrent_queue::ConcurrentQueue;

let q = ConcurrentQueue::<i32>::bounded(100);
Source

pub const fn unbounded() -> ConcurrentQueue<T>

Creates a new unbounded queue.

§Examples
use concurrent_queue::ConcurrentQueue;

let q = ConcurrentQueue::<i32>::unbounded();
Source

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)));
Source

pub fn force_push(&self, value: T) -> Result<Option<T>, ForcePushError<T>>

Push an element into the queue, potentially displacing another element.

Attempts to push an element into the queue. If the queue is full, one item from the queue is replaced with the provided item. The displaced item is returned as Some(T). If the queue is closed, an error is returned.

§Examples
use concurrent_queue::{ConcurrentQueue, ForcePushError, PushError};

let q = ConcurrentQueue::bounded(3);

// We can push to the queue.
for i in 1..=3 {
    assert_eq!(q.force_push(i), Ok(None));
}

// Push errors because the queue is now full.
assert_eq!(q.push(4), Err(PushError::Full(4)));

// Pushing a new value replaces the old ones.
assert_eq!(q.force_push(5), Ok(Some(1)));
assert_eq!(q.force_push(6), Ok(Some(2)));

// Close the queue to stop further pushes.
q.close();

// Pushing will return an error.
assert_eq!(q.force_push(7), Err(ForcePushError(7)));

// Popping items will return the force-pushed ones.
assert_eq!(q.pop(), Ok(3));
assert_eq!(q.pop(), Ok(5));
assert_eq!(q.pop(), Ok(6));
Source

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));
Source

pub fn try_iter(&self) -> TryIter<'_, T>

Get an iterator over the items in the queue.

The iterator will continue until the queue is empty or closed. It will never block; if the queue is empty, the iterator will return None. If new items are pushed into the queue, the iterator may return Some in the future after returning None.

§Examples
use concurrent_queue::ConcurrentQueue;

let q = ConcurrentQueue::bounded(5);
q.push(1).unwrap();
q.push(2).unwrap();
q.push(3).unwrap();

let mut iter = q.try_iter();
assert_eq!(iter.by_ref().sum::<i32>(), 6);
assert_eq!(iter.next(), None);

// Pushing more items will make them available to the iterator.
q.push(4).unwrap();
assert_eq!(iter.next(), Some(4));
assert_eq!(iter.next(), None);
Source

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());
Source

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());
Source

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);
Source

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);
Source

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));
Source

pub fn is_closed(&self) -> bool

Returns true if the queue is closed.

§Examples
use concurrent_queue::ConcurrentQueue;

let q = ConcurrentQueue::<i32>::unbounded();

assert!(!q.is_closed());
q.close();
assert!(q.is_closed());

Trait Implementations§

Source§

impl<T> Debug for ConcurrentQueue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> RefUnwindSafe for ConcurrentQueue<T>

Source§

impl<T: Send> Send for ConcurrentQueue<T>

Source§

impl<T: Send> Sync for ConcurrentQueue<T>

Source§

impl<T> UnwindSafe for ConcurrentQueue<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ConcurrentQueue<T>

§

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

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.