Struct StackArrayDeque

Source
pub struct StackArrayDeque<T, const N: usize> { /* private fields */ }
Expand description

A fixed-capacity, stack-allocated double-ended queue backed by a circular buffer.

StackArrayDeque<T, N> stores up to N elements inline on the stack with no heap allocation. All insertions and removals at either end run in O(1) time. Once full, further push_back calls will overwrite the oldest front element, and push_front calls will overwrite the oldest back element (FIFO overwrite behavior).

§Examples

use array_deque::StackArrayDeque;

let mut dq: StackArrayDeque<i32, 3> = StackArrayDeque::new();
dq.push_back(10);
dq.push_back(20);
dq.push_front(5);
assert_eq!(dq.len(), 3);
assert_eq!(dq[0], 5);
assert_eq!(dq[2], 20);

// Overflow: overwrites the front (5)
dq.push_back(30);
assert_eq!(dq.pop_front(), Some(10));

Implementations§

Source§

impl<T, const N: usize> StackArrayDeque<T, N>

Source

pub const fn new() -> Self

Creates a new empty StackArrayDeque.

§Examples
use array_deque::StackArrayDeque;

let deque: StackArrayDeque<i32, 10> = StackArrayDeque::new();
assert_eq!(deque.capacity(), 10);
assert!(deque.is_empty());
Source

pub fn push_back(&mut self, value: T)

Appends an element to the back of the deque.

If the deque is at capacity, this will overwrite the front element and advance the front pointer.

§Arguments
  • value - The element to append
§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.len(), 2);
Source

pub fn push_front(&mut self, value: T)

Prepends an element to the front of the deque.

If the deque is at capacity, this will overwrite the back element.

§Arguments
  • value - The element to prepend
§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_front(1);
deque.push_front(2);
assert_eq!(deque[0], 2);
assert_eq!(deque[1], 1);
Source

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

Removes and returns the last element from the deque.

§Returns

Some(T) if the deque is not empty, None otherwise.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_back(), Some(2));
assert_eq!(deque.pop_back(), Some(1));
assert_eq!(deque.pop_back(), None);
Source

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

Removes and returns the first element from the deque.

§Returns

Some(T) if the deque is not empty, None otherwise.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), None);
Source

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

Returns a reference to the front element without removing it.

§Examples
use array_deque::StackArrayDeque;

let mut dq: StackArrayDeque<i32, 3> = StackArrayDeque::new();
assert_eq!(dq.front(), None);
dq.push_back(42);
assert_eq!(dq.front(), Some(&42));
Source

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

Returns a reference to the back element without removing it.

§Examples
use array_deque::StackArrayDeque;

let mut dq: StackArrayDeque<i32, 3> = StackArrayDeque::new();
dq.push_back(1);
dq.push_back(2);
assert_eq!(dq.back(), Some(&2));
Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Returns an iterator over the elements of the deque.

The iterator yields elements from front to back.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_back(1);
deque.push_back(2);
deque.push_back(3);

let mut iter = deque.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), None);
Source

pub const fn capacity(&self) -> usize

Returns the maximum capacity of the deque.

§Examples
use array_deque::StackArrayDeque;

let deque: StackArrayDeque<i32, 10> = StackArrayDeque::new();
assert_eq!(deque.capacity(), 10);
Source

pub const fn len(&self) -> usize

Returns the number of elements currently in the deque.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
assert_eq!(deque.len(), 0);
deque.push_back(1);
assert_eq!(deque.len(), 1);
Source

pub const fn is_empty(&self) -> bool

Returns true if the deque contains no elements.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
assert!(deque.is_empty());
deque.push_back(1);
assert!(!deque.is_empty());
Source

pub const fn is_full(&self) -> bool

Returns true if the deque has reached its maximum capacity.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 2> = StackArrayDeque::new();
assert!(!deque.is_full());
deque.push_back(1);
deque.push_back(2);
assert!(deque.is_full());
Source

pub fn clear(&mut self)

Removes all elements from the deque.

This operation properly drops all contained elements and resets the deque to an empty state.

§Examples
use array_deque::StackArrayDeque;

let mut deque: StackArrayDeque<i32, 3> = StackArrayDeque::new();
deque.push_back(1);
deque.push_back(2);
deque.clear();
assert!(deque.is_empty());
assert_eq!(deque.len(), 0);

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for StackArrayDeque<T, N>

Source§

fn clone(&self) -> Self

Creates a deep copy of the deque with the same capacity and elements.

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 StackArrayDeque<T, N>

Source§

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

Formats the deque as a debug list showing all elements from front to back.

Source§

impl<T, const N: usize> Default for StackArrayDeque<T, N>

Source§

fn default() -> Self

Creates an empty deque.

Source§

impl<'de, T: Deserialize<'de>, const N: usize> Deserialize<'de> for StackArrayDeque<T, N>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserializes a sequence into a StackArrayDeque, erroring if it exceeds capacity.

Source§

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

Source§

fn drop(&mut self)

Properly drops all contained elements.

Source§

impl<T, const N: usize> Extend<T> for StackArrayDeque<T, N>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends the deque with items from an iterator, pushing to the back.

§Examples
use array_deque::StackArrayDeque;

let mut dq: StackArrayDeque<i32, 5> = StackArrayDeque::new();
dq.extend([1,2,3]);
assert_eq!(dq.len(), 3);
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, const N: usize> FromIterator<T> for StackArrayDeque<T, N>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a deque by collecting an iterator into its back, up to capacity.

§Examples
use array_deque::StackArrayDeque;

let dq: StackArrayDeque<_, 4> = (0..3).collect();
assert_eq!(dq.len(), 3);
Source§

impl<T, const N: usize> Index<usize> for StackArrayDeque<T, N>

Source§

fn index(&self, i: usize) -> &Self::Output

Provides indexed access to elements in the deque.

Index 0 corresponds to the front element, index 1 to the second element, etc.

§Panics

Panics if the index is out of bounds (>= len()).

Source§

type Output = T

The returned type after indexing.
Source§

impl<T, const N: usize> IndexMut<usize> for StackArrayDeque<T, N>

Source§

fn index_mut(&mut self, i: usize) -> &mut Self::Output

Provides mutable indexed access to elements in the deque.

§Panics

Panics if the index is out of bounds (>= len()).

Source§

impl<'a, T, const N: usize> IntoIterator for &'a StackArrayDeque<T, N>

Source§

fn into_iter(self) -> Self::IntoIter

Borrows the deque and returns an iterator over &T.

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = StackArrayDequeIter<'a, T, N>

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

impl<T, const N: usize> IntoIterator for StackArrayDeque<T, N>

Source§

fn into_iter(self) -> Self::IntoIter

Consumes the deque and returns an iterator over its elements.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = StackArrayDequeIntoIter<T, N>

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

impl<T: PartialEq, const N: usize> PartialEq for StackArrayDeque<T, N>

Source§

fn eq(&self, other: &Self) -> bool

Compares two deques for equality based on their elements and order.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Serialize, const N: usize> Serialize for StackArrayDeque<T, N>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serializes the deque as a sequence of its elements from front to back.

Source§

impl<T: Eq, const N: usize> Eq for StackArrayDeque<T, N>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for StackArrayDeque<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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,