heapless

Struct Deque

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

Source

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();
Source

pub const fn capacity(&self) -> usize

Returns the maximum number of elements the deque can hold.

Source

pub const fn len(&self) -> usize

Returns the number of elements currently in the deque.

Source

pub fn clear(&mut self)

Clears the deque, removing all values.

Source

pub fn is_empty(&self) -> bool

Returns whether the deque is empty.

Source

pub fn is_full(&self) -> bool

Returns whether the deque is full (i.e. if len() == capacity().

Source

pub fn as_slices(&self) -> (&[T], &[T])

Returns a pair of slices which contain, in order, the contents of the Deque.

Source

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.

Source

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

Provides a reference to the front element, or None if the Deque is empty.

Source

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

Provides a mutable reference to the front element, or None if the Deque is empty.

Source

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

Provides a reference to the back element, or None if the Deque is empty.

Source

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

Provides a mutable reference to the back element, or None if the Deque is empty.

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub unsafe fn push_front_unchecked(&mut self, item: T)

Appends an item to the front of the deque

§Safety

This assumes the deque is not full.

Source

pub unsafe fn push_back_unchecked(&mut self, item: T)

Appends an item to the back of the deque

§Safety

This assumes the deque is not full.

Source

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

Returns an iterator over the deque.

Source

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

Returns an iterator that allows modifying each value.

Trait Implementations§

Source§

impl<T, const N: usize> Clone for Deque<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: Debug, const N: usize> Debug for Deque<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 Deque<T, N>

Source§

fn default() -> Self

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a Deque<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 Deque<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> IntoIterator for Deque<T, N>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<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

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Deque<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Deque<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Deque<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Deque<T, N>
where T: Sync,

§

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

§

impl<T, const N: usize> UnwindSafe for Deque<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 T)

🔬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.