# Struct arduboy_rust::heapless::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() -> Deque<T, N>

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) -> Deque<T, N>

Returns a copy of the value. Read more
1.0.0§

#### fn clone_from(&mut self, source: &Self)

Performs copy-assignment from `source`. Read more
source§

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

source§

#### fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

### impl<T, const N: usize> Default for Deque<T, N>

source§

#### fn default() -> Deque<T, N>

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>

§

#### 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) -> <&'a Deque<T, N> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

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

Creates an iterator from a value. Read more
source§

### impl<T, const N: usize> IntoIterator for Deque<T, N>

§

#### type Item = T

The type of the elements being iterated over.
§

#### type IntoIter = IntoIter<T, N>

Which kind of iterator are we turning this into?
source§

#### fn into_iter(self) -> <Deque<T, N> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

## Auto Trait Implementations§

§

### 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§

§

### impl<T> Any for Twhere T: 'static + ?Sized,

§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
§

### impl<T> Borrow<T> for Twhere T: ?Sized,

§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

### impl<T> BorrowMut<T> for Twhere T: ?Sized,

§

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

### impl<T> From<T> for T

§

#### fn from(t: T) -> T

Returns the argument unchanged.

§

### impl<T, U> Into<U> for Twhere U: From<T>,

§

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `[From]<T> for U` chooses to do.

§

### impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

#### type Error = Infallible

The type returned in the event of a conversion error.
§

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

### impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.