Ring buffer that uses a boxed array as backing storage. This guarantees that the buffers size is
always exactly at the compile-time constant value. There is no way to get fewer or more entries.
In addition, by having the array boxed, conversion from [Vec][std::vec::Vec] and to
[VecDeque][std::collections::VecDeque] are nearly free.
This crate does not use any unsafe code (however the IntoIterator implementation does so
indirectly, as it internally uses a [VecDeque][std::collections::VecDeque]).
Since the ring buffer has always a guaranteed number of elements, it must be initialized, either
from an iterator, a boxed slice (most collections can convert into a boxed slice at very low
cost), a [Vec], or at a value that implements [Copy][std::marker::Copy].
Examples
A new ring buffer can only be created from a [Vec] or a [boxed][std::boxed::Box] [slice]
of the correct size (using [TryFrom][std::convert::TryFrom]/[TryInto][std::convert::TryInto]),
by taking the correct size from an iterator (using [new()][RingBuffer::new]),
or by initializing all entries in the ring buffer to the same [Copy][core::marker::Copy]able
value (using [new_copy()][RingBuffer::new_copy]).
# use RingBuffer;
let buf : = vec!.try_into.expect;
let wrong_size : = vec!.try_into;
assert!;
let buf2 : = new
.expect;
let not_enough : = new;
assert_eq!;
let buf3 : = new_copy;
assert_eq!;
As you have seen above, accessing upcoming elements can be done with the [Index] or
[IndexMut] syntax. Howver, if you are not absolutely certain that your index is within the
size of the ring buffer, you can also use the [get()][RingBuffer::get] or
[get_mut()][RingBuffer::get_mut] methods. The indices are always relevant to the current
position within the RingBuffer.
# use RingBuffer;
let buf : = vec!.try_into.expect;
assert_eq!;
assert_eq!;
let mut buf = buf;
assert_eq!;
let = buf.push_pop;
assert_eq!;
In order to add a new element to the buffer and remove the oldest element from the buffer (after
all, the size of the RingBuffer has to be constant), you can use the
[push_pop()][RingBuffer::push_pop()] method:
# use RingBuffer;
let buf : = vec!.try_into.expect;
let = buf.push_pop;
assert_eq!;
assert_eq!;
assert_eq!;
To iterate over the upcoming elements, you can either use the [iter()][RingBuffer::iter] or
the [`into_iter()][RingBuffer::into_iter] methods, the latter if you want the iterator to take
ownership of the data of the ring buffer.
# use RingBuffer;
let buf : = vec!.try_into.expect;
assert!;
assert!;
And last, but not least, once you are done utilizing the ring buffer's guarantee that the amount
of items is constant, you can always convert it to a [VecDeque][std::collections::VecDeque]
without needing a reallocation.
# use RingBuffer;
let buf : = vec!.try_into.expect;
let = buf.push_pop;
let deque : VecDeque = buf.into;
assert!;