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.
T
: Any type that implements the Clone
trait.
let mut cbuf = CircularBuffer::<isize>::new(3);
let mut cbuf_def = CircularBuffer::with_default(3, 0isize);
assert_eq!(cbuf.size(), 0);
assert_eq!(cbuf_def.size(), 3);
cbuf.add(6);
cbuf_def.add(7);
assert_eq!(cbuf.peek().unwrap(), 6);
assert_eq!(cbuf_def.peek().unwrap(), 0);
Default CircularBuffer<T>
initializer
A new, empty CircularBuffer<T>
let cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
assert_eq!(cbuf.size(), 0);
assert_eq!(cbuf.capacity(), 3);
Create a CircularBuffer<T>
with default values
A new CircularBuffer<T>
filled with default values
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));
Gets the capacity of the CircularBuffer<T>
The number of allowed elements in the buffer
let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
assert_eq!(cbuf.capacity(), 3);
Adds an element to a circular buffer
val
: Value to add to the buffer
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
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)));
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.
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 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));
Peek at the head of the circular 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 cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
cbuf.add(42);
assert_eq!(cbuf.peek(), Ok(42));
Gets the size of the circular buffer
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
let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
assert_eq!(cbuf.size(), 0);
cbuf.add(42);
assert_eq!(cbuf.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