Struct heapless::spsc::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>

§

type Item = &'a T

The type of the elements being iterated over.
§

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>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> !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> 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.