Struct queues::CircularBuffer
[−]
[src]
pub struct CircularBuffer<T: Clone> { /* fields omitted */ }
Represents a FIFO CircularBuffer<T>
data structure.
This structure is a limited capacity queue, with optional provisions
for default values. Under normal circumstances, the size
of the
queue grows until it reaches its capacity
, at which point any
further additions push out its oldest member.
If default values are specified, then the size
of the queue
is always equal to its capacity
, with empty slots occupied by the
specified default value.
Type parameters
T
: Any type that implements theClone
trait.
Examples
let mut cbuf = CircularBuffer::<isize>::new(3); let mut cbuf_def = CircularBuffer::with_default(3, 0isize); // Check sizes assert_eq!(cbuf.size(), 0); assert_eq!(cbuf_def.size(), 3); // Add elements cbuf.add(6); cbuf_def.add(7); // Peek at the next element scheduled for removal assert_eq!(cbuf.peek().unwrap(), 6); assert_eq!(cbuf_def.peek().unwrap(), 0);
Methods
impl<T: Clone> CircularBuffer<T>
[src]
pub fn new(capacity: usize) -> CircularBuffer<T>
[src]
Default CircularBuffer<T>
initializer
Returns
A new, empty CircularBuffer<T>
Examples
let cbuf: CircularBuffer<isize> = CircularBuffer::new(3); assert_eq!(cbuf.size(), 0); assert_eq!(cbuf.capacity(), 3);
pub fn with_default(capacity: usize, default_value: T) -> CircularBuffer<T>
[src]
Create a CircularBuffer<T>
with default values
Returns
A new CircularBuffer<T>
filled with default values
Examples
let cbuf_def = CircularBuffer::with_default(3, -1isize); assert_eq!(cbuf_def.size(), 3); assert_eq!(cbuf_def.capacity(), 3); assert_eq!(cbuf_def.peek(), Ok(-1));
pub fn capacity(&self) -> usize
[src]
Trait Implementations
impl<T: Debug + Clone> Debug for CircularBuffer<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 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 bufferError
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 bufferError
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);
Auto Trait Implementations
impl<T> Send for CircularBuffer<T> where
T: Send,
T: Send,
impl<T> Sync for CircularBuffer<T> where
T: Sync,
T: Sync,