Trait IsQueue

Source
pub trait IsQueue<T: Clone> {
    // Required methods
    fn add(&mut self, val: T) -> Result<Option<T>, &str>;
    fn remove(&mut self) -> Result<T, &str>;
    fn peek(&self) -> Result<T, &str>;
    fn size(&self) -> usize;
}
Expand description

Defines methods that would be expected on a queue data structure

Required Methods§

Source

fn add(&mut self, val: T) -> Result<Option<T>, &str>

Adds a new value to a queue

§Parameters
  • val: Value to add to the queue
§Returns
  • Ok(_): If the element add was successful.
    • Some(T): If adding an element resulted in the removal of an existing one (in the case of a circular buffer, for instance)
    • None: Adding an element did not return any value
  • Error: If the element add was unsuccessful
§Errors

Attempting to add an element to a full queue that does not allow for overflow will return an error.

Source

fn remove(&mut self) -> Result<T, &str>

Removes an element from the queue and returns it

For queues with default values, removing an element will add a new default value into the queue.

§Returns
  • Ok(T): The oldest element in the queue
  • Error
§Errors

Returns an error if an attempt is made to remove an element from an empty queue

Source

fn peek(&self) -> Result<T, &str>

Peek at the head of the queue

§Returns
  • Ok(T): The next element scheduled for removal from the queue
  • Error
§Errors

Returns an error if an attempt is made to peek into an empty queue

Source

fn size(&self) -> usize

Gets the size of the queue

§Returns

The number of elements in the queue. Note, this includes default values when specified, which means that the size of a queue with default values should always be equal to its capacity

Implementors§

Source§

impl<T: Clone> IsQueue<T> for Buffer<T>

Source§

impl<T: Clone> IsQueue<T> for CircularBuffer<T>

Source§

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