Struct queues::Buffer
[−]
[src]
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 theClone
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]
Trait Implementations
impl<T: Debug + Clone> Debug for Buffer<T>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
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 successfulError
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 bufferError
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 bufferError
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));