heapless::spsc

Struct Queue

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

A statically allocated single producer single consumer queue with a capacity of N - 1 elements

IMPORTANT: To get better performance use a value for N that is a power of 2 (e.g. 16, 32, etc.).

Implementations§

Source§

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

Source

pub const fn new() -> Self

Creates an empty queue with a fixed capacity of N - 1

Source

pub const fn capacity(&self) -> usize

Returns the maximum number of elements the queue can hold

Source

pub fn len(&self) -> usize

Returns the number of elements in the queue

Source

pub fn is_empty(&self) -> bool

Returns true if the queue is empty

Source

pub fn is_full(&self) -> bool

Returns true if the queue is full

Source

pub fn iter(&self) -> Iter<'_, T, N>

Iterates from the front of the queue to the back

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T, N>

Returns an iterator that allows modifying each value

Source

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

Adds an item to the end of the queue

Returns back the item if the queue is full

Source

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

Returns the item in the front of the queue, or None if the queue is empty

Source

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

Returns a reference to the item in the front of the queue without dequeuing, or None if the queue is empty.

§Examples
use heapless::spsc::Queue;

let mut queue: Queue<u8, 235> = Queue::new();
let (mut producer, mut consumer) = queue.split();
assert_eq!(None, consumer.peek());
producer.enqueue(1);
assert_eq!(Some(&1), consumer.peek());
assert_eq!(Some(1), consumer.dequeue());
assert_eq!(None, consumer.peek());
Source

pub unsafe fn enqueue_unchecked(&mut self, val: T)

Adds an item to the end of the queue, without checking if it’s full

§Unsafety

If the queue is full this operation will leak a value (T’s destructor won’t run on the value that got overwritten by item), and will allow the dequeue operation to create a copy of item, which could result in T’s destructor running on item twice.

Source

pub unsafe fn dequeue_unchecked(&mut self) -> T

Returns the item in the front of the queue, without checking if there is something in the queue

§Unsafety

If the queue is empty this operation will return uninitialized memory.

Source

pub fn split(&mut self) -> (Producer<'_, T, N>, Consumer<'_, T, N>)

Splits a queue into producer and consumer endpoints

Trait Implementations§

Source§

impl<T, const N: usize> Clone for Queue<T, N>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

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§

impl<T, const N: usize> Hash for Queue<T, N>
where T: Hash,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a Queue<T, N>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a mut Queue<T, N>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, const N: usize, const N2: usize> PartialEq<Queue<T, N2>> for Queue<T, N>
where T: PartialEq,

Source§

fn eq(&self, other: &Queue<T, N2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: usize> Eq for Queue<T, N>
where T: Eq,

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> Send for Queue<T, N>
where T: Send,

§

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

§

impl<T, const N: usize> Unpin for Queue<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Queue<T, N>
where T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.