Struct ArrayDeque

Source
pub struct ArrayDeque<T> { /* private fields */ }
Expand description

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

ArrayDeque<T> allocates a buffer on the heap with the given capacity. All insertions and removals at either end run in O(1) time. Once full, further push_back calls overwrite the oldest front element, and push_front calls overwrite the oldest back element (FIFO overwrite behavior).

§Examples

use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(3);
dq.push_back("a");
dq.push_back("b");
dq.push_front("c");
assert_eq!(dq.len(), 3);
assert_eq!(dq[0], "c");

// Overflow: overwrites the back ("b")
dq.push_front("x");
assert_eq!(dq.pop_back(), Some("a"));

Implementations§

Source§

impl<T> ArrayDeque<T>

Source

pub fn new(cap: usize) -> Self

Creates a new ArrayDeque with the specified capacity.

§Arguments
  • cap - The fixed capacity of the deque. Must be greater than zero.
§Panics

Panics if cap is zero or if memory allocation fails.

§Examples
use array_deque::ArrayDeque;

let deque: ArrayDeque<i32> = ArrayDeque::new(10);
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::ArrayDeque;

let mut deque = ArrayDeque::new(3);
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::ArrayDeque;

let mut deque = ArrayDeque::new(3);
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::ArrayDeque;

let mut deque = ArrayDeque::new(3);
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::ArrayDeque;

let mut deque = ArrayDeque::new(3);
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::ArrayDeque;

let mut dq = ArrayDeque::new(3);
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::ArrayDeque;

let mut dq = ArrayDeque::new(3);
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 (front to back).

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(3);
dq.push_back(1);
dq.push_back(2);
let v: Vec<_> = dq.iter().cloned().collect();
assert_eq!(v, vec![1,2]);
Source

pub fn capacity(&self) -> usize

Returns the maximum capacity of the deque.

§Examples
use array_deque::ArrayDeque;

let dq: ArrayDeque<i32> = ArrayDeque::new(5);
assert_eq!(dq.capacity(), 5);
Source

pub fn len(&self) -> usize

Returns the number of elements currently in the deque.

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(3);
assert_eq!(dq.len(), 0);
dq.push_back(1);
assert_eq!(dq.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the deque contains no elements.

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(3);
assert!(dq.is_empty());
dq.push_back(1);
assert!(!dq.is_empty());
Source

pub fn is_full(&self) -> bool

Returns true if the deque has reached its maximum capacity.

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(2);
dq.push_back(1);
dq.push_back(2);
assert!(dq.is_full());
Source

pub fn clear(&mut self)

Removes all elements from the deque, properly dropping them, and resets it to an empty state.

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(3);
dq.push_back(1);
dq.clear();
assert!(dq.is_empty());
assert_eq!(dq.len(), 0);

Trait Implementations§

Source§

impl<T: Clone> Clone for ArrayDeque<T>

Source§

fn clone(&self) -> Self

Creates a deep copy of the deque with identical capacity and contents.

§Examples
use array_deque::ArrayDeque;

let mut deque = ArrayDeque::new(3);
deque.push_back(1);
deque.push_back(2);
let cloned = deque.clone();
assert_eq!(deque.len(), cloned.len());
assert_eq!(deque[0], cloned[0]);
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ArrayDeque<T>

Source§

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

Formats the deque as a debug list (front to back).

Source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArrayDeque<T>

Source§

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

Deserializes a sequence into a deque (capacity == item count).

Source§

impl<T> Drop for ArrayDeque<T>

Source§

fn drop(&mut self)

Drops all elements and deallocates the heap buffer.

Source§

impl<T> Extend<T> for ArrayDeque<T>

Source§

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

Extends the deque by pushing each item of the iterator to the back.

§Examples
use array_deque::ArrayDeque;

let mut dq = ArrayDeque::new(5);
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: Clone> From<&[T]> for ArrayDeque<T>

Source§

fn from(slice: &[T]) -> Self

Clones all elements from a slice into a new deque.

Source§

impl<T: Clone, const N: usize> From<&[T; N]> for ArrayDeque<T>

Source§

fn from(array: &[T; N]) -> Self

Clones each element from the array reference.

Source§

impl<T: Clone> From<&Vec<T>> for ArrayDeque<T>

Source§

fn from(vec: &Vec<T>) -> Self

Clones each element from the vector.

Source§

impl<T, const N: usize> From<[T; N]> for ArrayDeque<T>

Source§

fn from(array: [T; N]) -> Self

Takes ownership of each element in the array.

Source§

impl<T> From<Vec<T>> for ArrayDeque<T>

Source§

fn from(vec: Vec<T>) -> Self

Takes ownership of each element in the vector.

Source§

impl<T> FromIterator<T> for ArrayDeque<T>

Source§

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

Creates a deque from an iterator by collecting all items. Capacity == number of items (min 1).

§Examples
use array_deque::ArrayDeque;

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

impl<T> Index<usize> for ArrayDeque<T>

Source§

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

Indexed access into the deque (0 is front).

§Panics

Panics if index >= len().

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<usize> for ArrayDeque<T>

Source§

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

Mutable indexed access into the deque (0 is front).

§Panics

Panics if index >= len().

Source§

impl<'a, T> IntoIterator for &'a ArrayDeque<T>

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 = ArrayDequeIter<'a, T>

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

impl<T> IntoIterator for ArrayDeque<T>

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 = ArrayDequeIntoIter<T>

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

impl<T: PartialEq> PartialEq for ArrayDeque<T>

Source§

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

Two deques are equal if they have the same length and each element compares equal in 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> Serialize for ArrayDeque<T>

Source§

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

Serializes the deque as a sequence (front to back).

Source§

impl<T: Eq> Eq for ArrayDeque<T>

Auto Trait Implementations§

§

impl<T> Freeze for ArrayDeque<T>

§

impl<T> RefUnwindSafe for ArrayDeque<T>
where T: RefUnwindSafe,

§

impl<T> !Send for ArrayDeque<T>

§

impl<T> !Sync for ArrayDeque<T>

§

impl<T> Unpin for ArrayDeque<T>
where T: Unpin,

§

impl<T> UnwindSafe for ArrayDeque<T>

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