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.