Struct fixed_deque::Deque

source ·
pub struct Deque<T> { /* private fields */ }
Expand description

Implementations§

source§

impl<T> Deque<T>

source

pub fn new(maxlen: usize) -> Self

Creates a new Deque with a given maximum length.

§Examples
use fixed_deque::Deque;

let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
deque.push_back(3);
deque.push_back(4);
assert_eq!(deque.len(), 3);
assert_eq!(deque.get(0), Some(&2));
source

pub fn new_from(value: T, maxlen: usize) -> Self

Creates a new Deque from a given single value and maximum length.

§Examples
use fixed_deque::Deque;

let deque: Deque<i32> = Deque::new_from(1, 3);
assert_eq!(deque.len(), 1);
assert_eq!(deque.get(0), Some(&1));
source

pub fn new_from_vec(vec: Vec<T>, maxlen: usize) -> Self

Creates a new Deque from an existing Vec with a given maximum length.

§Examples
use fixed_deque::Deque;

let deque: Deque<i32> = Deque::new_from_vec(vec![1, 2, 3], 3);
assert_eq!(deque.len(), 3);
source

pub const fn new_from_vec_deque(deque: VecDeque<T>, maxlen: usize) -> Self

Creates a new Deque from an existing VecDeque with a given maximum length.

§Examples
use std::collections::VecDeque;
use fixed_deque::Deque;

let vec_deque: VecDeque<i32> = VecDeque::from(vec![1, 2, 3]);
let deque: Deque<i32> = Deque::new_from_vec_deque(vec_deque, 3);
assert_eq!(deque.len(), 3);
source

pub fn push_back(&mut self, value: T) -> Option<T>

Add an element to the back of the Deque. If the Deque exceeds its maximum length, the front element is removed.

§Examples
use fixed_deque::Deque;

let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
deque.push_back(3);
assert_eq!(deque.len(), 3);
let overflow = deque.push_back(4);
assert_eq!(overflow, Some(1));
assert_eq!(deque.len(), 3);
source

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

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

§Examples
use fixed_deque::Deque;

let mut deque = Deque::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 pop_back(&mut self) -> Option<T>

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

source

pub fn len(&self) -> usize

Returns the number of elements in the Deque.

source

pub fn is_empty(&self) -> bool

Returns whether the Deque is empty.

source

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

Returns the last element of the Deque.

§Examples
use fixed_deque::Deque;

let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.back(), Some(&2));
source

pub fn get(&self, index: usize) -> Option<&T>

Returns an immutable reference to the element at the given index.

§Examples
use fixed_deque::Deque;
let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.get(1), Some(&2));
source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Returns a mutable reference to the element at the given index.

§Examples
use fixed_deque::Deque;

let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
if let Some(value) = deque.get_mut(1) {
    *value = 42;
}
assert_eq!(deque.get(1), Some(&42));
source

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

Returns an iterator over all elements except the last one.

§Examples
use fixed_deque::Deque;

let mut deque: Deque<i32> = Deque::new(3);
deque.push_back(1);
deque.push_back(2);
deque.push_back(3);
let mut iter = deque.iter_except_last();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), None);
source

pub fn iter(&self) -> Iter<'_, T>

Returns a front-to-back iterator.

§Examples
use fixed_deque::Deque;

let mut deque = Deque::new(3);
deque.push_back(5);
deque.push_back(3);
deque.push_back(4);
let b: &[_] = &[&5, &3, &4];
let c: Vec<&i32> = deque.iter().collect();
assert_eq!(&c[..], b);
source

pub fn capacity(&self) -> usize

Returns the number of elements the deque can hold without reallocating.

§Examples
use fixed_deque::Deque;

let deque: Deque<i32> = Deque::new(10);
assert!(deque.capacity() >= 10);

Trait Implementations§

source§

impl<T: Clone> Clone for Deque<T>

source§

fn clone(&self) -> Deque<T>

Returns a copy 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> Debug for Deque<T>

source§

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

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

impl<T: Default> Default for Deque<T>

source§

fn default() -> Deque<T>

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

impl<T> FromIterator<T> for Deque<T>

source§

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

Creates a value from an iterator. Read more
source§

impl<T> Index<usize> for Deque<T>

source§

type Output = T

The returned type after indexing.
source§

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

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<usize> for Deque<T>

source§

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

Performs the mutable indexing (container[index]) operation. Read more
source§

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

source§

type Item = &'a T

The type of the elements being iterated over.
source§

type IntoIter = Iter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T> IntoIterator for &'a mut Deque<T>

source§

type Item = &'a mut T

The type of the elements being iterated over.
source§

type IntoIter = IterMut<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for Deque<T>

source§

type Item = T

The type of the elements being iterated over.
source§

type IntoIter = IntoIter<T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq for Deque<T>

source§

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

Tests for self and other values to be equal, and is used by ==.
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: Eq> Eq for Deque<T>

Auto Trait Implementations§

§

impl<T> Freeze for Deque<T>

§

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

§

impl<T> Send for Deque<T>
where T: Send,

§

impl<T> Sync for Deque<T>
where T: Sync,

§

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

§

impl<T> UnwindSafe for Deque<T>
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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.