[][src]Struct queues::Buffer

pub struct Buffer<T: Clone> { /* fields omitted */ }

A FIFO buffer with a growable size and a capacity limit

Type parameters

  • T: Any type that implements the Clone trait.

Examples

let mut buf = Buffer::new(3);

// Add some elements
assert_eq!(buf.add(6), Ok(None));
assert_eq!(buf.add(7), Ok(None));

// Remove an element
assert_eq!(buf.remove(), Ok(6));

// Peek at the next element scheduled for removal
assert_eq!(buf.peek(), Ok(7));

// Check the queue size
assert_eq!(buf.size(), 1);

Methods

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

pub fn new(capacity: usize) -> Buffer<T>
[src]

Create a new buffer

Returns

A new, empty Buffer<T>

Examples

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

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

Gets the capacity of the buffer

Returns

The number of allowed elements in the buffer

Examples

let mut buf: Buffer<isize> = Buffer::new(5);
assert_eq!(buf.capacity(), 5);

Trait Implementations

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: Debug + Clone> Debug for Buffer<T>
[src]

Auto Trait Implementations

impl<T> Send for Buffer<T> where
    T: Send

impl<T> Sync for Buffer<T> where
    T: Sync

Blanket Implementations

impl<T> From for T
[src]

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

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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