pub struct Buffer<T: Clone> { /* fields omitted */ }
A FIFO buffer with a growable size and a capacity limit
T
: Any type that implements the Clone
trait.
let mut buf = Buffer::new(3);
assert_eq!(buf.add(6), Ok(None));
assert_eq!(buf.add(7), Ok(None));
assert_eq!(buf.remove(), Ok(6));
assert_eq!(buf.peek(), Ok(7));
assert_eq!(buf.size(), 1);
Create a new buffer
A new, empty Buffer<T>
let buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.size(), 0);
Gets the capacity of the buffer
The number of allowed elements in the buffer
let mut buf: Buffer<isize> = Buffer::new(5);
assert_eq!(buf.capacity(), 5);
Adds an element to a buffer
val
: Value to add to the buffer
Ok(None)
: Element addition was successful
Error
Returns an error if an attempt is made to add an element to a full
buffer
use queues::*;
let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.add(42), Ok(None));
Removes an element from the buffer and returns it.
Ok(T)
: The oldest element in the buffer
Error
Returns an error if an attempt is made to remove an element from
an empty buffer
let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.remove(), Ok(42));
assert_eq!(buf.size(), 0);
Peek at the head of the buffer
Ok(T)
: The next element scheduled for removal from the buffer
Error
Returns an error if an attempt is made to peek into an empty buffer
let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.peek(), Ok(42));
Gets the size of the buffer
The number of elements in the buffer.
let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.size(), 0);
buf.add(42);
assert_eq!(buf.size(), 1);
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static