ArrayDeque

Struct ArrayDeque 

Source
pub struct ArrayDeque<T, const CAP: usize>(/* private fields */);
Expand description

A fixed capacity deque. Capacity must be in the power of two. If you have plain data, better use ArrayDequePlain.

Can be stored directly on the stack.

The “default” usage of this as a queue is to use push_last to add to the queue, and pop_first to consume from the queue.

Implementations§

Source§

impl<T, const CAP: usize> ArrayDeque<T, CAP>

Source

pub const fn new() -> Self

Creates an empty ArrayDeque.

§Examples
use array_buf::ArrayDeque;
                
let buf: ArrayDeque<usize, 2> = ArrayDeque::new();
Source

pub const fn capacity(&self) -> usize

Returns the capacity of the array.

§Examples
use array_buf::ArrayDeque;
                
let buf: ArrayDeque<usize, 2> = ArrayDeque::new();
                
assert_eq!(buf.capacity(), 2);
Source

pub fn len(&self) -> usize

Returns the number of elements in the array.

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 2> = ArrayDeque::new();
assert_eq!(buf.len(), 0);

buf.push_last(1).unwrap();

assert_eq!(buf.len(), 1);
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 2> = ArrayDeque::new();
assert_eq!(buf.len(), 0);

buf.push_first(-1).unwrap();

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

pub fn is_empty(&self) -> bool

Returns true if the array contains no elements.

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 2> = ArrayDeque::new();
assert!(buf.is_empty());

buf.push_last(1).unwrap();

assert!(!buf.is_empty());
Source

pub fn is_full(&self) -> bool

Returns true if the array is full.

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 2> = ArrayDeque::new();
assert!(!buf.is_full());

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

assert!(buf.is_full());
Source

pub fn is_contiguous_any_order(&self) -> bool

Order can be compromised once full.

Source

pub fn is_contiguous(&self) -> bool

Source

pub unsafe fn pop_first_unchecked(&mut self) -> T

§Safety

Must not be empty.

Source

pub unsafe fn pop_last_unchecked(&mut self) -> T

§Safety

Must not be empty.

Source

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

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

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

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

Removes the last element and returns it, or None if empty.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

pub unsafe fn push_first_unchecked(&mut self, element: T)

§Safety

Must not be full.

Source

pub unsafe fn push_last_unchecked(&mut self, element: T)

§Safety

Must not be full.

Source

pub fn push_first(&mut self, element: T) -> Result<(), &'static str>

Add an element to the start of the deque.

Return Ok if the push succeeds, or Err if the array is full.

§Examples
use array_buf::ArrayDeque;

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

buf.push_first(-1);
buf.push_first(-2);

let overflow = buf.push_first(-3);

assert!(overflow.is_err());
assert_eq!(buf.first(), Some(&-2));
Source

pub fn push_last(&mut self, element: T) -> Result<(), &'static str>

Add an element to the end of the deque.

Return Ok if the push succeeds, or Err if the array is full.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1);
buf.push_last(2);

let overflow = buf.push_last(3);

assert!(overflow.is_err());
assert_eq!(buf.last(), Some(&2));
Source

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

Provides a reference to the first element, or None if empty.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

assert_eq!(buf.first(), Some(&1));
Source

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

Provides a mut reference to the first element, or None if empty.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

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

Provides a reference to the last element, or None if empty.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

assert_eq!(buf.last(), Some(&2));
Source

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

Provides a mut reference to the last element, or None if empty.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

pub unsafe fn as_slice(&self) -> &[T]

Returns a slice which contains the content of the inner buffer.

§Safety

Must be contiguous. If it’s not, use linearize().

Source

pub unsafe fn as_mut_slice(&mut self) -> &mut [T]

Returns a slice which contains the content of the inner buffer.

§Safety

Must be contiguous. If it’s not, use linearize().

Source

pub fn as_slices(&self) -> (&[T], &[T])

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

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 8> = ArrayDeque::new();

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

buf.push_first(-1).unwrap();

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

pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])

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

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<_, 8> = ArrayDeque::new();

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

buf.push_first(-1);

assert_eq!(buf.as_mut_slices(), (&mut[-1][..], &mut[1, 2][..]));
use array_buf::ArrayDeque;

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

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

buf.push_last(1).unwrap();
buf.push_last(2).unwrap();

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

buf.push_first(-1).unwrap();
buf.push_first(-2).unwrap();

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

pub fn linearize(&mut self)

Make the buffer contiguous.

The linearization may be required when interacting with external interfaces requiring contiguous slices.

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<isize, 4> = ArrayDeque::new();

buf.push_last(1).unwrap();
buf.push_first(-1).unwrap();

assert!(!buf.is_contiguous());

buf.linearize();

assert!(buf.is_contiguous());
Source

pub fn linearize_one(&mut self)

Make the buffer contiguous.

The linearization may be required when interacting with external interfaces requiring contiguous slices.

§Examples
use array_buf::ArrayDeque;

let mut buf: ArrayDeque<isize, 2> = ArrayDeque::new();

buf.push_first(-1).unwrap();

assert!(!buf.is_contiguous());

buf.linearize_one();

assert!(buf.is_contiguous());
Source§

impl<T, const CAP: usize> ArrayDeque<T, CAP>

Source

pub fn clear(&mut self)

Clears the buffer by dropping and resetting the indexes.

§Examples
use array_buf::ArrayDeque;

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

buf.push_last(1).unwrap();
buf.push_first(-1).unwrap();
buf.clear();

assert!(buf.is_empty());

Trait Implementations§

Source§

impl<T: Clone, const CAP: usize> Clone for ArrayDeque<T, CAP>

Source§

fn clone(&self) -> Self

Returns a duplicate 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 CAP: usize> Debug for ArrayDeque<T, CAP>

Source§

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

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

impl<T: Default, const CAP: usize> Default for ArrayDeque<T, CAP>

Source§

fn default() -> ArrayDeque<T, CAP>

Returns the “default value” for a type. Read more
Source§

impl<T, const CAP: usize> Drop for ArrayDeque<T, CAP>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, const CAP: usize> Freeze for ArrayDeque<T, CAP>
where T: Freeze,

§

impl<T, const CAP: usize> RefUnwindSafe for ArrayDeque<T, CAP>
where T: RefUnwindSafe,

§

impl<T, const CAP: usize> Send for ArrayDeque<T, CAP>
where T: Send,

§

impl<T, const CAP: usize> Sync for ArrayDeque<T, CAP>
where T: Sync,

§

impl<T, const CAP: usize> Unpin for ArrayDeque<T, CAP>
where T: Unpin,

§

impl<T, const CAP: usize> UnwindSafe for ArrayDeque<T, CAP>
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.