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>
impl<T, const N: usize> Queue<T, N>
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the maximum number of elements the queue can hold
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, N> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T, N> ⓘ
Returns an iterator that allows modifying each value
Sourcepub fn enqueue(&mut self, val: T) -> Result<(), T>
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
Sourcepub fn dequeue(&mut self) -> Option<T>
pub fn dequeue(&mut self) -> Option<T>
Returns the item in the front of the queue, or None
if the queue is empty
Sourcepub fn peek(&self) -> Option<&T>
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());
Sourcepub unsafe fn enqueue_unchecked(&mut self, val: T)
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.
Sourcepub unsafe fn dequeue_unchecked(&mut self) -> T
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.
Trait Implementations§
Source§impl<'a, T, const N: usize> IntoIterator for &'a Queue<T, N>
impl<'a, T, const N: usize> IntoIterator for &'a Queue<T, N>
Source§impl<'a, T, const N: usize> IntoIterator for &'a mut Queue<T, N>
impl<'a, T, const N: usize> IntoIterator for &'a mut Queue<T, N>
Source§impl<T, const N: usize, const N2: usize> PartialEq<Queue<T, N2>> for Queue<T, N>where
T: PartialEq,
impl<T, const N: usize, const N2: usize> PartialEq<Queue<T, N2>> for Queue<T, N>where
T: PartialEq,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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.