Skip to main content

MPMCQueue

Trait MPMCQueue 

Source
pub trait MPMCQueue {
    type Item;

    // Required methods
    fn push(&self, item: Self::Item) -> Result<(), Self::Item>;
    fn pop(&self) -> Option<Self::Item>;
    fn len(&self) -> usize;
    fn capacity(&self) -> usize;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn is_full(&self) -> bool { ... }
    fn force_push(&self, item: Self::Item) -> Option<Self::Item> { ... }
    fn force_push_and_do<F>(&self, item: Self::Item, f: F)
       where F: FnMut(Self::Item) { ... }
}
Expand description

The main trait used to interface with a MPMCQueue. All implementations provided by this crate are atomic and non-blocking. Fallible operations of this trait may fail spuriously.

§Examples

use nblf_queue::{StaticQueue, MPMCQueue};

let q: StaticQueue<_, 2> = StaticQueue::new();

assert!(q.push(&42).is_ok());
assert!(q.push(&2).is_ok());

assert_eq!(q.len(), 2);
assert!(q.is_full());

assert_eq!(q.force_push(&0), Some(&42));
assert!(q.is_full());

assert_eq!(q.pop(), Some(&2));
assert_eq!(q.pop(), Some(&0));
assert_eq!(q.len(), 0);
assert!(q.is_empty());

Required Associated Types§

Source

type Item

The item stored in the queue

Required Methods§

Source

fn push(&self, item: Self::Item) -> Result<(), Self::Item>

Attempts to push an item into the queue. Returns the item as an error if the queue is full.

§Examples
use nblf_queue::{StaticQueue, MPMCQueue};

let q: StaticQueue<_, 2> = StaticQueue::new();

assert!(q.push(&10).is_ok());
assert!(q.push(&20).is_ok());
assert_eq!(q.push(&30), Err(&30));
assert_eq!(q.pop(), Some(&10));
Source

fn pop(&self) -> Option<Self::Item>

Attempts to pop an item from the queue. Returns None if the queue was empty.

§Examples
use nblf_queue::{StaticQueue, MPMCQueue};

let q: StaticQueue<_, 2> = StaticQueue::new();

assert!(q.push(&10).is_ok());
assert!(q.push(&42).is_ok());
assert_eq!(q.pop(), Some(&10));
assert_eq!(q.pop(), Some(&42));
assert!(q.pop().is_none());
Source

fn len(&self) -> usize

Returns the current len of the queue. The returned value may be stale under concurrent access and should not be used for synchronization.

Source

fn capacity(&self) -> usize

Returns the total capacity of the queue.

Provided Methods§

Source

fn is_empty(&self) -> bool

Indicates whether the queue is empty. The returned value may be stale under concurrent access and should not be used for synchronization.

Source

fn is_full(&self) -> bool

Indicates whether the queue is full. The returned value may be stale under concurrent access and should not be used for synchronization.

Source

fn force_push(&self, item: Self::Item) -> Option<Self::Item>

Pushes an item into the queue, removing an existing item if the queue is full.

If the queue is full, this method will remove items until space becomes available. The last removed item is returned.

Under contention this method may spin for some time, however it will never block.

§Examples
use nblf_queue::{StaticQueue, MPMCQueue};

let q: StaticQueue<_, 2> = StaticQueue::new();

assert!(q.force_push(&10).is_none());
assert!(q.force_push(&20).is_none());
assert_eq!(q.force_push(&30), Some(&10));
assert_eq!(q.pop(), Some(&20));
Source

fn force_push_and_do<F>(&self, item: Self::Item, f: F)
where F: FnMut(Self::Item),

Pushes an item into the queue, removing an existing item if the queue is full.

If the queue is full, this method will remove items until space becomes available. The provided closure will be called on each removed item.

Under contention this method may spin for some time, however it will never block, provided the passed closure does not block.

§Examples
use nblf_queue::{StaticQueue, MPMCQueue};

let q: StaticQueue<_, 2> = StaticQueue::new();

q.force_push_and_do(&10, |item| {});
q.force_push_and_do(&20, |item| {});
q.force_push_and_do(&30, |item| {
    assert_eq!(item, &10)
});
assert_eq!(q.pop(), Some(&20));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T, S> MPMCQueue for PooledQueue<T, S>
where S: SlotType<ItemHandle<T>>,

Source§

type Item = T

Source§

impl<T, S> MPMCQueue for Queue<T, S>
where T: AsPackedValue, S: SlotType<T>,

Source§

type Item = T

Source§

impl<T, const N: usize, S> MPMCQueue for PooledStaticQueue<T, N, S>
where S: SlotType<ItemHandle<T>>,

Source§

type Item = T

Source§

impl<T, const N: usize, S> MPMCQueue for StaticQueue<T, N, S>
where T: AsPackedValue, S: SlotType<T>,

Source§

type Item = T