Struct arraydeque::ArrayDeque[][src]

pub struct ArrayDeque<A: Array, B: Behavior = Saturating> { /* fields omitted */ }

A fixed capacity ring buffer.

It can be stored directly on the stack if needed.

The "default" usage of this type as a queue is to use push_back to add to the queue, and pop_front to remove from the queue. Iterating over ArrayDeque goes front to back.

Methods

impl<A: Array> ArrayDeque<A, Saturating>
[src]

Add an element to the front of the deque.

Return Ok(()) if the push succeeds, or return Err(CapacityError { *element* }) if the vector is full.

Examples

// 1 -(+)-> [_, _, _] => [1, _, _] -> Ok(())
// 2 -(+)-> [1, _, _] => [2, 1, _] -> Ok(())
// 3 -(+)-> [2, 1, _] => [3, 2, 1] -> Ok(())
// 4 -(+)-> [3, 2, 1] => [3, 2, 1] -> Err(CapacityError { element: 4 })

use arraydeque::{ArrayDeque, CapacityError};

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_front(1);
buf.push_front(2);
buf.push_front(3);

let overflow = buf.push_front(4);

assert_eq!(overflow, Err(CapacityError { element: 4 }));
assert_eq!(buf.back(), Some(&1));

Add an element to the back of the deque.

Return Ok(()) if the push succeeds, or return Err(CapacityError { *element* }) if the vector is full.

Examples

// [_, _, _] <-(+)- 1 => [_, _, 1] -> Ok(())
// [_, _, 1] <-(+)- 2 => [_, 1, 2] -> Ok(())
// [_, 1, 2] <-(+)- 3 => [1, 2, 3] -> Ok(())
// [1, 2, 3] <-(+)- 4 => [1, 2, 3] -> Err(CapacityError { element: 4 })

use arraydeque::{ArrayDeque, CapacityError};

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);
buf.push_back(3);

let overflow = buf.push_back(4);

assert_eq!(overflow, Err(CapacityError { element: 4 }));
assert_eq!(buf.back(), Some(&3));

Inserts an element at index within the ArrayDeque. Whichever end is closer to the insertion point will be moved to make room, and all the affected elements will be moved to new positions.

Return Ok(()) if the push succeeds, or return Err(CapacityError { *element* }) if the vector is full.

Element at index 0 is the front of the queue.

Panics

Panics if index is greater than ArrayDeque's length

Examples

// [_, _, _] <-(#0)- 3 => [3, _, _] -> Ok(())
// [3, _, _] <-(#0)- 1 => [1, 3, _] -> Ok(())
// [1, 3, _] <-(#1)- 2 => [1, 2, 3] -> Ok(())
// [1, 2, 3] <-(#1)- 4 => [1, 2, 3] -> Err(CapacityError { element: 4 })

use arraydeque::{ArrayDeque, CapacityError};

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.insert(0, 3);
buf.insert(0, 1);
buf.insert(1, 2);

let overflow = buf.insert(1, 4);

assert_eq!(overflow, Err(CapacityError { element: 4 }));
assert_eq!(buf.back(), Some(&3));

Extend deque from front with the contents of an iterator.

Does not extract more items than there is space for. No error occurs if there are more iterator elements.

Examples

// [9, 8, 7] -(+)-> [_, _, _, _, _, _, _] => [7, 8, 9, _, _, _, _]
// [6, 5, 4] -(+)-> [7, 8, 9, _, _, _, _] => [4, 5, 6, 7, 8, 9, _]
// [3, 2, 1] -(+)-> [4, 5, 6, 7, 8, 9, _] => [3, 4, 5, 6, 7, 8, 9]

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 7]> = ArrayDeque::new();

buf.extend_front(vec![9, 8, 7].into_iter());
buf.extend_front(vec![6, 5, 4].into_iter());

assert_eq!(buf.len(), 6);

// max capacity reached
buf.extend_front(vec![3, 2, 1].into_iter());

assert_eq!(buf.len(), 7);
assert_eq!(buf, vec![3, 4, 5, 6, 7, 8, 9].into());

Extend deque from back with the contents of an iterator.

Does not extract more items than there is space for. No error occurs if there are more iterator elements.

Examples

// [_, _, _, _, _, _, _] <-(+)- [1, 2, 3] => [_, _, _, _, 1, 2, 3]
// [_, _, _, _, 1, 2, 3] <-(+)- [4, 5, 6] => [_, 1, 2, 3, 4, 5, 6]
// [_, 1, 2, 3, 4, 5, 6] <-(+)- [7, 8, 9] => [1, 2, 3, 4, 5, 6, 7]

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 7]> = ArrayDeque::new();

buf.extend_back(vec![1, 2, 3].into_iter());
buf.extend_back(vec![4, 5, 6].into_iter());

assert_eq!(buf.len(), 6);

// max capacity reached
buf.extend_back(vec![7, 8, 9].into_iter());

assert_eq!(buf.len(), 7);
assert_eq!(buf, vec![1, 2, 3, 4, 5, 6, 7].into());

impl<A: Array> ArrayDeque<A, Wrapping>
[src]

Add an element to the front of the deque.

Return None if deque still has capacity, or Some(existing) if the deque is full, where existing is the backmost element being kicked out.

Examples

// 1 -(+)-> [_, _, _] => [1, _, _] -> None
// 2 -(+)-> [1, _, _] => [2, 1, _] -> None
// 3 -(+)-> [2, 1, _] => [3, 2, 1] -> None
// 4 -(+)-> [3, 2, 1] => [4, 3, 2] -> Some(1)

use arraydeque::{ArrayDeque, Wrapping};

let mut buf: ArrayDeque<[_; 3], Wrapping> = ArrayDeque::new();

buf.push_front(1);
buf.push_front(2);
buf.push_front(3);

let existing = buf.push_front(4);

assert_eq!(existing, Some(1));
assert_eq!(buf.back(), Some(&2));

Appends an element to the back of a buffer

Return None if deque still has capacity, or Some(existing) if the deque is full, where existing is the frontmost element being kicked out.

Examples

// [_, _, _] <-(+)- 1 => [_, _, 1] -> None
// [_, _, 1] <-(+)- 2 => [_, 1, 2] -> None
// [_, 1, 2] <-(+)- 3 => [1, 2, 3] -> None
// [1, 2, 3] <-(+)- 4 => [2, 3, 4] -> Some(1)

use arraydeque::{ArrayDeque, Wrapping};

let mut buf: ArrayDeque<[_; 3], Wrapping> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);
buf.push_back(3);

let existing = buf.push_back(4);

assert_eq!(existing, Some(1));
assert_eq!(buf.back(), Some(&4));

Extend deque from front with the contents of an iterator.

Extracts all items from iterator and kicks out the backmost element if necessary.

Examples

// [9, 8, 7] -(+)-> [_, _, _, _, _, _, _] => [7, 8, 9, _, _, _, _]
// [6, 5, 4] -(+)-> [7, 8, 9, _, _, _, _] => [4, 5, 6, 7, 8, 9, _]
// [3, 2, 1] -(+)-> [4, 5, 6, 7, 8, 9, _] => [1, 2, 3, 4, 5, 6, 7]

use arraydeque::{ArrayDeque, Wrapping};

let mut buf: ArrayDeque<[_; 7], Wrapping> = ArrayDeque::new();

buf.extend_front(vec![9, 8, 7].into_iter());
buf.extend_front(vec![6, 5, 4].into_iter());

assert_eq!(buf.len(), 6);

// max capacity reached
buf.extend_front(vec![3, 2, 1].into_iter());

assert_eq!(buf.len(), 7);
assert_eq!(buf, vec![1, 2, 3, 4, 5, 6, 7].into());

Extend deque from back with the contents of an iterator.

Extracts all items from iterator and kicks out the frontmost element if necessary.

Examples

// [_, _, _, _, _, _, _] <-(+)- [1, 2, 3] => [_, _, _, _, 1, 2, 3]
// [_, _, _, _, 1, 2, 3] <-(+)- [4, 5, 6] => [_, 1, 2, 3, 4, 5, 6]
// [_, 1, 2, 3, 4, 5, 6] <-(+)- [7, 8, 9] => [3, 4, 5, 6, 7, 8, 9]

use arraydeque::{ArrayDeque, Wrapping};

let mut buf: ArrayDeque<[_; 7], Wrapping> = ArrayDeque::new();

buf.extend_back(vec![1, 2, 3].into_iter());
buf.extend_back(vec![4, 5, 6].into_iter());

assert_eq!(buf.len(), 6);

// max capacity reached
buf.extend_back(vec![7, 8, 9].into_iter());

assert_eq!(buf.len(), 7);
assert_eq!(buf, vec![3, 4, 5, 6, 7, 8, 9].into());

impl<A: Array, B: Behavior> ArrayDeque<A, B>
[src]

Creates an empty ArrayDeque.

Examples

use arraydeque::ArrayDeque;

let buf: ArrayDeque<[usize; 2]> = ArrayDeque::new();

Return the capacity of the ArrayDeque.

Examples

use arraydeque::ArrayDeque;

let buf: ArrayDeque<[usize; 2]> = ArrayDeque::new();

assert_eq!(buf.capacity(), 2);

Returns the number of elements in the ArrayDeque.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 1]> = ArrayDeque::new();

assert_eq!(buf.len(), 0);

buf.push_back(1);

assert_eq!(buf.len(), 1);

Returns true if the buffer contains no elements

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 1]> = ArrayDeque::new();

assert!(buf.is_empty());

buf.push_back(1);

assert!(!buf.is_empty());

Returns true if the buffer is full.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 1]> = ArrayDeque::new();

assert!(!buf.is_full());

buf.push_back(1);

assert!(buf.is_full());

Returns true if the ArrayDeque contains an element equal to the given value.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.contains(&1), true);
assert_eq!(buf.contains(&3), false);

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();
assert_eq!(buf.front(), None);

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.front(), Some(&1));

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();
assert_eq!(buf.front_mut(), None);

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.front_mut(), Some(&mut 1));

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.back(), Some(&2));

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.back_mut(), Some(&mut 2));

Retrieves an element in the ArrayDeque by index.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.get(1), Some(&1));

Retrieves an element in the ArrayDeque mutably by index.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.get_mut(1), Some(&mut 1));

Important traits for Iter<'a, T>

Returns a front-to-back iterator.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

let expected = vec![0, 1, 2];

assert!(buf.iter().eq(expected.iter()));

Important traits for IterMut<'a, T>

Returns a front-to-back iterator that returns mutable references.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[usize; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

let mut expected = vec![0, 1, 2];

assert!(buf.iter_mut().eq(expected.iter_mut()));

Removes the first element and returns it, or None if the sequence is empty.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.pop_front(), Some(1));
assert_eq!(buf.pop_front(), Some(2));
assert_eq!(buf.pop_front(), None);

Removes the last element from a buffer and returns it, or None if it is empty.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 2]> = ArrayDeque::new();
assert_eq!(buf.pop_back(), None);

buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.pop_back(), Some(2));
assert_eq!(buf.pop_back(), Some(1));

Clears the buffer, removing all values.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 1]> = ArrayDeque::new();

buf.push_back(1);
buf.clear();

assert!(buf.is_empty());

Important traits for Drain<'a, A, B>

Create a draining iterator that removes the specified range in the ArrayDeque and yields the removed items.

Note 1: The element range is removed even if the iterator is not consumed until the end.

Note 2: It is unspecified how many elements are removed from the deque, if the Drain value is not dropped, but the borrow it holds expires (eg. due to mem::forget).

Panics

Panics if the starting point is greater than the end point or if the end point is greater than the length of the deque.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

{
    let drain = buf.drain(2..);
    assert!(vec![2].into_iter().eq(drain));
}

{
    let iter = buf.iter();
    assert!(vec![0, 1].iter().eq(iter));
}

// A full range clears all contents
buf.drain(..);
assert!(buf.is_empty());

Swaps elements at indices i and j.

i and j may be equal.

Fails if there is no element with either index.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

buf.swap(0, 2);

assert_eq!(buf, vec![2, 1, 0].into());

Removes an element from anywhere in the ArrayDeque and returns it, replacing it with the last element.

This does not preserve ordering, but is O(1).

Returns None if index is out of bounds.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();
assert_eq!(buf.swap_remove_back(0), None);

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.swap_remove_back(0), Some(0));
assert_eq!(buf, vec![2, 1].into());

Removes an element from anywhere in the ArrayDeque and returns it, replacing it with the first element.

This does not preserve ordering, but is O(1).

Returns None if index is out of bounds.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();
assert_eq!(buf.swap_remove_back(0), None);

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.swap_remove_front(2), Some(2));
assert_eq!(buf, vec![1, 0].into());

Removes and returns the element at index from the ArrayDeque. Whichever end is closer to the removal point will be moved to make room, and all the affected elements will be moved to new positions. Returns None if index is out of bounds.

Element at index 0 is the front of the queue.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

assert_eq!(buf.remove(1), Some(1));
assert_eq!(buf, vec![0, 2].into());

Splits the collection into two at the given index.

Returns a newly allocated Self. self contains elements [0, at), and the returned Self contains elements [at, len).

Element at index 0 is the front of the queue.

Panics

Panics if at > len

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);
buf.push_back(2);

// buf = [0], buf2 = [1, 2]
let buf2 = buf.split_off(1);

assert_eq!(buf.len(), 1);
assert_eq!(buf2.len(), 2);

Retains only the elements specified by the predicate.

In other words, remove all elements e such that f(&e) returns false. This method operates in place and preserves the order of the retained elements.

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new();

buf.extend_back(0..4);
buf.retain(|&x| x % 2 == 0);

assert_eq!(buf, vec![0, 2].into());

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 7]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);

assert_eq!(buf.as_slices(), (&[0, 1][..], &[][..]));

buf.push_front(2);

assert_eq!(buf.as_slices(), (&[2][..], &[0, 1][..]));

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

Examples

use arraydeque::ArrayDeque;

let mut buf: ArrayDeque<[_; 7]> = ArrayDeque::new();

buf.push_back(0);
buf.push_back(1);

assert_eq!(buf.as_mut_slices(), (&mut [0, 1][..], &mut[][..]));

buf.push_front(2);

assert_eq!(buf.as_mut_slices(), (&mut[2][..], &mut[0, 1][..]));

Trait Implementations

impl<A: Array> Extend<A::Item> for ArrayDeque<A, Saturating>
[src]

Extends a collection with the contents of an iterator. Read more

impl<A: Array> FromIterator<A::Item> for ArrayDeque<A, Saturating>
[src]

Creates a value from an iterator. Read more

impl<A: Array> Clone for ArrayDeque<A, Saturating> where
    A::Item: Clone
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: Array> Extend<A::Item> for ArrayDeque<A, Wrapping>
[src]

Extends a collection with the contents of an iterator. Read more

impl<A: Array> FromIterator<A::Item> for ArrayDeque<A, Wrapping>
[src]

Creates a value from an iterator. Read more

impl<A: Array> Clone for ArrayDeque<A, Wrapping> where
    A::Item: Clone
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: Array> From<ArrayDeque<A, Wrapping>> for ArrayDeque<A, Saturating>
[src]

Performs the conversion.

impl<A: Array> From<ArrayDeque<A, Saturating>> for ArrayDeque<A, Wrapping>
[src]

Performs the conversion.

impl<A: Array, B: Behavior> From<Vec<A::Item>> for ArrayDeque<A, B> where
    Self: FromIterator<A::Item>, 
[src]

Performs the conversion.

impl<A: Array, B: Behavior> Into<Vec<A::Item>> for ArrayDeque<A, B> where
    Self: FromIterator<A::Item>, 
[src]

Performs the conversion.

impl<A: Array, B: Behavior> Drop for ArrayDeque<A, B>
[src]

Executes the destructor for this type. Read more

impl<A: Array, B: Behavior> Default for ArrayDeque<A, B>
[src]

Returns the "default value" for a type. Read more

impl<A: Array, B: Behavior> PartialEq for ArrayDeque<A, B> where
    A::Item: PartialEq
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<A: Array, B: Behavior> Eq for ArrayDeque<A, B> where
    A::Item: Eq
[src]

impl<A: Array, B: Behavior> PartialOrd for ArrayDeque<A, B> where
    A::Item: PartialOrd
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<A: Array, B: Behavior> Ord for ArrayDeque<A, B> where
    A::Item: Ord
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl<A: Array, B: Behavior> Hash for ArrayDeque<A, B> where
    A::Item: Hash
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<A: Array, B: Behavior> Index<usize> for ArrayDeque<A, B>
[src]

The returned type after indexing.

Performs the indexing (container[index]) operation.

impl<A: Array, B: Behavior> IndexMut<usize> for ArrayDeque<A, B>
[src]

Performs the mutable indexing (container[index]) operation.

impl<A: Array, B: Behavior> IntoIterator for ArrayDeque<A, B>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, A: Array, B: Behavior> IntoIterator for &'a ArrayDeque<A, B>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, A: Array, B: Behavior> IntoIterator for &'a mut ArrayDeque<A, B>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<A: Array, B: Behavior> Debug for ArrayDeque<A, B> where
    A::Item: Debug
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<A, B> Send for ArrayDeque<A, B> where
    A: Send,
    B: Send,
    <A as Array>::Index: Send

impl<A, B> Sync for ArrayDeque<A, B> where
    A: Sync,
    B: Sync,
    <A as Array>::Index: Sync