queue

Struct Queue

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

A first in, first out queue built around Vec.

An optional capacity can be set (or changed) to ensure the Queue never grows past a certain size. If the capacity is not specified (ie set to None) then the Queue will grow as needed. If you’re worried about memory allocation, set a capacity and the necessary memory will be allocated at that time. Otherwise memory could be allocated, deallocated and reallocated as the Queue changes size.

The only requirement of the type used is that it implements the Clone trait.

§Example

use queue::Queue;

let mut q = Queue::with_capacity(5);

for i in 0..5 {
	q.queue(i).unwrap();
}

for i in 0..5 {
	assert_eq!(q.dequeue(), Some(i));
}

Implementations§

Source§

impl<T: Clone> Queue<T>

Source

pub fn new() -> Queue<T>

Constructs a new Queue<T>.

§Example
let mut q: Queue<String> = Queue::new();
Source

pub fn with_capacity(cap: usize) -> Queue<T>

Constructs a new Queue<T> with a specified capacity.

§Example
let mut q: Queue<String> = Queue::with_capacity(20);
Source

pub fn queue(&mut self, item: T) -> Result<usize, ()>

Add an item to the end of the Queue. Returns Ok(usize) with the new length of the Queue, or Err(()) if there is no more room.

§Example
let mut q = Queue::new();
q.queue("hello").unwrap();
assert_eq!(q.peek(), Some("hello"));
Source

pub fn force_queue(&mut self, item: T) -> usize

Forcefully add an item to the end of the Queue. If the Queue is at capacity, the first item will be removed to make room. Returns usize with the new length of the Queue.

§Example
let mut q = Queue::with_capacity(1);
q.queue("hello").unwrap();
let _ = q.force_queue("world");
assert_eq!(q.peek(), Some("world"));
Source

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

Remove the next item from the Queue. Returns Option<T> so it will return either Some(T) or None depending on if there’s anything in the Queue to get.

§Example
let mut q = Queue::new();
q.queue("hello").unwrap();
q.queue("world").unwrap();
assert_eq!(q.dequeue(), Some("hello"));
Source

pub fn vec(&self) -> &Vec<T>

Return a &Vec<T> for the Queue<T>.

§Example
let mut q = Queue::new();
q.queue(1).unwrap();
q.queue(2).unwrap();
q.queue(3).unwrap();
assert_eq!(&vec![1, 2, 3], q.vec());
Source

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

Peek at the next item in the Queue, if there’s something there.

§Example
let mut q = Queue::new();
q.queue(12).unwrap();
assert_eq!(q.peek(), Some(12));
Source

pub fn len(&self) -> usize

The number of items currently in the Queue.

§Example
let mut q = Queue::with_capacity(8);
q.queue(1).unwrap();
q.queue(2).unwrap();
assert_eq!(q.len(), 2);
Source

pub fn is_empty(&self) -> bool

Check if the Queue is empty.

§Example
let mut q = Queue::new();
assert_eq!(q.is_empty(), true);
q.queue(1).unwrap();
assert_eq!(q.is_empty(), false);
Source

pub fn capacity(&self) -> Option<usize>

Query the capacity for a Queue. If there is no capacity set (the Queue can grow as needed) then None will be returned, otherwise it will be Some(usize).

§Example
let q: Queue<u8> = Queue::with_capacity(12);
assert_eq!(q.capacity(), Some(12));
Source

pub fn set_capacity<C: Into<Option<usize>>>(&mut self, cap: C) -> Result<(), ()>

Modify the capacity of a Queue. If set to None, the Queue will grow automatically, as needed. Otherwise, it will be limited to the specified number of items. If there are more items in the Queue than the requested capacity, Err(()) will be returned, otherwise the operation will succeed and Ok(()) will be returned. If the capacity is shrunk, the underlying Vec will be shrunk also, which would free up whatever extra memory was allocated for the Queue.

§Example
let mut q: Queue<u8> = Queue::new();
q.set_capacity(12).unwrap();
q.set_capacity(None).unwrap();

Trait Implementations§

Source§

impl<T: Clone> Clone for Queue<T>

Source§

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

Source§

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

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

impl<T: Default> Default for Queue<T>

Source§

fn default() -> Queue<T>

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

impl<T: Clone> From<Vec<T>> for Queue<T>

Source§

fn from(v: Vec<T>) -> Queue<T>

Constructs a new Queue<T> from a Vec<T>.

§Example
let q = Queue::from(vec![1, 2, 3]);

Auto Trait Implementations§

§

impl<T> Freeze for Queue<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Queue<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.