[][src]Trait queues::IsQueue

pub trait IsQueue<T: Clone> {
    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; }

Defines methods that would be expected on a queue data structure

Required methods

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.

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

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

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

Loading content...

Implementors

impl<T: Clone> IsQueue<T> for Buffer<T>
[src]

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

Adds an element to a buffer

Parameters

  • val: Value to add to the buffer

Returns

  • Ok(None): Element addition was successful
  • Error

Errors

Returns an error if an attempt is made to add an element to a full buffer

Examples

use queues::*;

let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.add(42), Ok(None));

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

Removes an element from the buffer and returns it.

Returns

  • Ok(T): The oldest element in the buffer
  • Error

Errors

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

Examples

let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.remove(), Ok(42));
assert_eq!(buf.size(), 0);

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

Peek at the head of the buffer

Returns

  • Ok(T): The next element scheduled for removal from the buffer
  • Error

Errors

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

Examples

let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.peek(), Ok(42));

fn size(&self) -> usize
[src]

Gets the size of the buffer

Returns

The number of elements in the buffer.

Examples

let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.size(), 0);
buf.add(42);
assert_eq!(buf.size(), 1);

impl<T: Clone> IsQueue<T> for CircularBuffer<T>
[src]

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

Adds an element to a circular buffer

Parameters

  • val: Value to add to the buffer

Returns

  • Ok(Some(T)): The oldest value in the buffer, in case the addition causes an overflow.
  • Ok(None): Nothing, if the buffer has room for the added element

Examples

let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
let mut cbuf_def = CircularBuffer::with_default(3, 5isize);
assert_eq!(cbuf.add(42), Ok(None));
assert_eq!(cbuf_def.add(42), Ok(Some(5)));

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

Removes an element from the circular buffer and returns it.

For circular buffers with default values, removing an element will add a new default value into the buffer.

Returns

  • Ok(T): The oldest element in the buffer
  • Error

Errors

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

Examples

let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
cbuf.add(42);
assert_eq!(cbuf.remove(), Ok(42));
assert_eq!(cbuf.size(), 0);

let mut cbuf_def = CircularBuffer::with_default(3, 4isize);
cbuf_def.add(42);
assert_eq!(cbuf_def.remove(), Ok(4));

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

Peek at the head of the circular buffer

Returns

  • Ok(T): The next element scheduled for removal from the buffer
  • Error

Errors

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

Examples

let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
cbuf.add(42);
assert_eq!(cbuf.peek(), Ok(42));

fn size(&self) -> usize
[src]

Gets the size of the circular buffer

Returns

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

Examples

let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
assert_eq!(cbuf.size(), 0);
cbuf.add(42);
assert_eq!(cbuf.size(), 1);

impl<T: Clone> IsQueue<T> for Queue<T>
[src]

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

Adds an element to a queue

Parameters

  • val: Value to add to the queue

Returns

Ok(None) as the element addition should always be successful

Examples

let mut q: Queue<isize> = Queue::new();
assert_eq!(q.add(42), Ok(None));
assert_eq!(q.size(), 1);

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

Removes an element from the queue and returns it

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

Examples

let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.remove(), Ok(42));
assert_eq!(q.size(), 0);

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

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

Examples

let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.peek(), Ok(42));

fn size(&self) -> usize
[src]

Gets the size of the queue

Returns

The number of elements in the queue

Examples

let mut q: Queue<isize> = Queue::new();
assert_eq!(q.size(), 0);
let _ = q.add(42);
assert_eq!(q.size(), 1);
Loading content...