Struct fundamental::Queue[][src]

pub struct Queue<T, const C: usize> { /* fields omitted */ }

A fixed-size queue. The const-parameter C denotes the capacity.

Implementations

impl<T: Copy, const C: usize> Queue<T, C>[src]

pub fn new() -> Self[src]

Constructs a new, empty Queue<T, C>.

impl<T, const C: usize> Queue<T, C>[src]

pub const fn capacity(&self) -> usize[src]

Returns the number of elements the queue can hold.

Example

use fundamental::Queue;
 
let queue = Queue::<i32, 4>::new();
assert_eq!(queue.capacity(), 4);

pub const fn len(&self) -> usize[src]

Returns the number of elements in the queue.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 1>::new();
assert_eq!(queue.len(), 0);
queue.enqueue(1);
assert_eq!(queue.len(), 1);

pub const fn is_empty(&self) -> bool[src]

Returns true if the queue contains no elements.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 1>::new();
assert_eq!(queue.is_empty(), true);
queue.enqueue(1);
assert_eq!(queue.is_empty(), false);

pub const fn is_full(&self) -> bool[src]

Returns true if the queue cannot contain any more elements.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 1>::new();
assert_eq!(queue.is_full(), false);
queue.enqueue(1);
assert_eq!(queue.is_full(), true);

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

Returns a reference to the element at the given index relative to the start of the queue. Returns None if there is no element at the position.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 1>::new();
assert_eq!(queue.get(0), None);
queue.enqueue(1);
assert_eq!(queue.get(0), Some(&1));

pub const fn as_slice(&self) -> &[Option<T>][src]

Returns a reference to the underlying storage of the queue.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 3>::new();
assert_eq!(queue.as_slice(), &[None, None, None]);

pub fn enqueue(&mut self, element: T) -> Result<(), T>[src]

Insert an element at the back of the queue. Returns Err(element) if the queue is full.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 3>::new();
assert_eq!(queue.as_slice(), &[None, None, None]);
let _ = queue.enqueue(1);
assert_eq!(queue.as_slice(), &[Some(1), None, None]);
let _ = queue.enqueue(2);
assert_eq!(queue.as_slice(), &[Some(1), Some(2), None]);

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

Take an element out of the front of the queue. Returns None if the queue is empty.

Example

use fundamental::Queue;
 
let mut queue = Queue::<i32, 3>::new();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
assert_eq!(queue.dequeue(), Some(1));
assert_eq!(queue.dequeue(), Some(2));
assert_eq!(queue.dequeue(), Some(3));
assert_eq!(queue.dequeue(), None);

Trait Implementations

impl<T: Clone, const C: usize> Clone for Queue<T, C>[src]

impl<T: Copy, const C: usize> Copy for Queue<T, C>[src]

impl<T: Debug, const C: usize> Debug for Queue<T, C>[src]

Auto Trait Implementations

impl<T, const C: usize> RefUnwindSafe for Queue<T, C> where
    T: RefUnwindSafe

impl<T, const C: usize> Send for Queue<T, C> where
    T: Send

impl<T, const C: usize> Sync for Queue<T, C> where
    T: Sync

impl<T, const C: usize> Unpin for Queue<T, C> where
    T: Unpin

impl<T, const C: usize> UnwindSafe for Queue<T, C> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.