Struct CircularQueue

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

A circular buffer-like queue.

Implementations§

Source§

impl<T> CircularQueue<T>

Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty CircularQueue<T> with the requested capacity.

§Examples
use circular_queue::CircularQueue;

let mut queue: CircularQueue<i32> = CircularQueue::with_capacity(5);
Source

pub fn len(&self) -> usize

Returns the current number of elements in the queue.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(5);
queue.push(1);
queue.push(2);
queue.push(3);

assert_eq!(queue.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the queue contains no elements.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(5);
assert!(queue.is_empty());

queue.push(1);
assert!(!queue.is_empty());
Source

pub fn is_full(&self) -> bool

Returns true if the queue is full.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(5);

assert!(!queue.is_full());

queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);

assert!(queue.is_full());
Source

pub fn capacity(&self) -> usize

Returns the capacity of the queue.

§Examples
use circular_queue::CircularQueue;

let queue: CircularQueue<i32> = CircularQueue::with_capacity(5);
assert_eq!(queue.capacity(), 5);
Source

pub fn clear(&mut self)

Clears the queue.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(5);
queue.push(1);
queue.push(2);
queue.push(3);

queue.clear();
assert_eq!(queue.len(), 0);
Source

pub fn push(&mut self, x: T) -> Popped<T>

Pushes a new element into the queue.

Once the capacity is reached, pushing new items will overwrite old ones.

In case an old value is overwritten, it will be returned.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);

queue.push(1);
queue.push(2);

assert_eq!(queue.push(3), None);
assert_eq!(queue.push(4), Some(1));

assert_eq!(queue.len(), 3);

let mut iter = queue.iter();

assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
Source

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

Returns an iterator over the queue’s contents.

The iterator goes from the most recently pushed items to the oldest ones.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);

let mut iter = queue.iter();

assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over the queue’s contents.

The iterator goes from the most recently pushed items to the oldest ones.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);

let mut iter = queue.iter_mut();

assert_eq!(iter.next(), Some(&mut 4));
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 2));
Source

pub fn asc_iter(&self) -> AscIter<'_, T>

Returns an ascending iterator over the queue’s contents.

The iterator goes from the least recently pushed items to the newest ones.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);

let mut iter = queue.asc_iter();

assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&4));
Source

pub fn asc_iter_mut(&mut self) -> AscIterMut<'_, T>

Returns a mutable ascending iterator over the queue’s contents.

The iterator goes from the least recently pushed items to the newest ones.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);

let mut iter = queue.asc_iter_mut();

assert_eq!(iter.next(), Some(&mut 2));
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 4));
Source

pub fn into_vec(self) -> Vec<T>

Converts the queue into a Vec<T> going from the most recently pushed items to the oldest ones.

§Examples
use circular_queue::CircularQueue;

let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);

let v = queue.into_vec();

assert_eq!(v, vec![4, 3, 2]);

Trait Implementations§

Source§

impl<T: Clone> Clone for CircularQueue<T>

Source§

fn clone(&self) -> CircularQueue<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 CircularQueue<T>

Source§

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

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

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

Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> From<CircularQueue<T>> for Vec<T>

Source§

fn from(queue: CircularQueue<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq> PartialEq for CircularQueue<T>

Source§

fn eq(&self, other: &CircularQueue<T>) -> 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> Serialize for CircularQueue<T>
where T: Serialize,

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for CircularQueue<T>

Auto Trait Implementations§

§

impl<T> Freeze for CircularQueue<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for CircularQueue<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, 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>,