Basic circular buffer library for Rust.
use circbuf::CircBuf;
fn main() {
// Create a new circular buffer that can hold 16 elements
let mut buf = CircBuf::<i32, 16>::new();
// Fill the buffer completely
for i in 0..16 {
buf.push(i);
}
assert!(buf.is_full());
// Iterate over values
for n in buf.iter() {
println!("{}", n);
}
// Adding values when the buffer is full overwrites the oldest value
for i in 16..19 {
buf.push(i);
}
// Iterate over values again
for n in buf.iter() {
println!("{}", n);
}
// Index specific values
println!("buf[0] = {}", buf[0]);
// println!("buf[20] = {}", buf[20]); // panic when index invalid
// Delete values while the buffer is not empty
while !buf.is_empty() {
// Popped values are returned in Option
println!("{}", buf.pop().unwrap());
}
// Check number of elements in a buffer
assert_eq!(buf.len(), 0);
}
Licensed under the MIT License