pub struct Deque<T, const N: usize> { /* private fields */ }
Expand description
A fixed capacity double-ended queue.
§Examples
use heapless::Deque;
// A deque with a fixed capacity of 8 elements allocated on the stack
let mut deque = Deque::<_, 8>::new();
// You can use it as a good old FIFO queue.
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.len(), 2);
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.len(), 0);
// Deque is double-ended, you can push and pop from the front and back.
deque.push_back(1);
deque.push_front(2);
deque.push_back(3);
deque.push_front(4);
assert_eq!(deque.pop_front(), Some(4));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(3));
// You can iterate it, yielding all the elements front-to-back.
for x in &deque {
println!("{}", x);
}
Implementations§
source§impl<T, const N: usize> Deque<T, N>
impl<T, const N: usize> Deque<T, N>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, empty deque with a fixed capacity of N
§Examples
use heapless::Deque;
// allocate the deque on the stack
let mut x: Deque<u8, 16> = Deque::new();
// allocate the deque in a static variable
static mut X: Deque<u8, 16> = Deque::new();
sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the maximum number of elements the deque can hold.
sourcepub fn as_slices(&self) -> (&[T], &[T])
pub fn as_slices(&self) -> (&[T], &[T])
Returns a pair of slices which contain, in order, the contents of the Deque
.
sourcepub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
Returns a pair of mutable slices which contain, in order, the contents of the Deque
.
sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Provides a reference to the front element, or None if the Deque
is empty.
sourcepub fn front_mut(&mut self) -> Option<&mut T>
pub fn front_mut(&mut self) -> Option<&mut T>
Provides a mutable reference to the front element, or None if the Deque
is empty.
sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Provides a reference to the back element, or None if the Deque
is empty.
sourcepub fn back_mut(&mut self) -> Option<&mut T>
pub fn back_mut(&mut self) -> Option<&mut T>
Provides a mutable reference to the back element, or None if the Deque
is empty.
sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes the item from the front of the deque and returns it, or None
if it’s empty
sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes the item from the back of the deque and returns it, or None
if it’s empty
sourcepub fn push_front(&mut self, item: T) -> Result<(), T>
pub fn push_front(&mut self, item: T) -> Result<(), T>
Appends an item
to the front of the deque
Returns back the item
if the deque is full
sourcepub fn push_back(&mut self, item: T) -> Result<(), T>
pub fn push_back(&mut self, item: T) -> Result<(), T>
Appends an item
to the back of the deque
Returns back the item
if the deque is full
sourcepub unsafe fn pop_front_unchecked(&mut self) -> T
pub unsafe fn pop_front_unchecked(&mut self) -> T
Removes an item from the front of the deque and returns it, without checking that the deque is not empty
§Safety
It’s undefined behavior to call this on an empty deque
sourcepub unsafe fn pop_back_unchecked(&mut self) -> T
pub unsafe fn pop_back_unchecked(&mut self) -> T
Removes an item from the back of the deque and returns it, without checking that the deque is not empty
§Safety
It’s undefined behavior to call this on an empty deque