Skip to main content

Queue

Struct Queue 

Source
pub struct Queue<T, const N: usize> { /* private fields */ }
Expand description

A bounded lock-free multi-producer multi-consumer queue with static storage.

Capacity N is fixed at compile time; the whole queue (including all slots) lives inline, so Queue::new() is const and the queue can be placed in a static with no allocator involved (requires the default build; with loom feature, use Queue::new() at runtime — see below).

push returns Result<(), T> on full push, pop returns Option<T> — plus Queue::enqueue/Queue::dequeue aliases matching the old heapless::mpmc API for drop-in migration.

§Static initialization

ⓘ
// Default build: `Queue::new()` is `const`.
static Q: Queue<u32, 8> = Queue::new();

With --features loom, new() is a regular function: construct at runtime.

Implementations§

Source§

impl<T, const N: usize> Queue<T, N>

Source

pub const fn new() -> Self

Creates an empty queue. const-compatible on the default build: static Q: Queue<u32, 8> = Queue::new();.

With the loom feature this is a regular (non-const) function because loom’s atomics are runtime-only; use it at runtime there.

Source

pub const fn capacity(&self) -> usize

Capacity in elements. Always N.

Source

pub fn push(&self, value: T) -> Result<(), T>

Attempts to push. On full queue, returns the value back as Err.

Source

pub fn enqueue(&self, value: T) -> Result<(), T>

Alias of Queue::push matching the heapless::mpmc API.

Source

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

Pushes, replacing the oldest element if full (returns it, else None). Ring-buffer mode.

Source

pub fn pop(&self) -> Option<T>

Attempts to pop. None if empty.

Source

pub fn dequeue(&self) -> Option<T>

Alias of Queue::pop matching the heapless::mpmc API.

Source

pub fn is_empty(&self) -> bool

true if no elements are stored.

Source

pub fn is_full(&self) -> bool

true if no push can succeed without Queue::force_push.

Source

pub fn len(&self) -> usize

Number of elements currently stored.

Trait Implementations§

Source§

impl<T, const N: usize> Debug for Queue<T, N>

Source§

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

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

impl<T, const N: usize> Default for Queue<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const N: usize> Drop for Queue<T, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send, const N: usize> Send for Queue<T, N>

Source§

impl<T: Send, const N: usize> Sync for Queue<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> !Freeze for Queue<T, N>

§

impl<T, const N: usize> !RefUnwindSafe for Queue<T, N>

§

impl<T, const N: usize> Forget for Queue<T, N>

§

impl<T, const N: usize> Move for Queue<T, N>

§

impl<T, const N: usize> Unpin for Queue<T, N>
where [MaybeUninit<Slot<T>>; N]: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for Queue<T, N>
where [MaybeUninit<Slot<T>>; N]: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for Queue<T, N>
where [MaybeUninit<Slot<T>>; N]: UnwindSafe,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.