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§
Required Methods§
Sourcefn push(&self, item: Self::Item) -> Result<(), Self::Item>
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));Sourcefn pop(&self) -> Option<Self::Item>
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());Provided Methods§
Sourcefn is_empty(&self) -> bool
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.
Sourcefn is_full(&self) -> bool
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.
Sourcefn force_push(&self, item: Self::Item) -> Option<Self::Item>
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));Sourcefn force_push_and_do<F>(&self, item: Self::Item, f: F)
fn force_push_and_do<F>(&self, item: Self::Item, f: F)
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".