Loading RustViz Engine...
Implement a circular ring buffer that reuses allocated memory slots without resizing.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
use std::collections::VecDeque;
pub struct RingBuffer<T> {
capacity: usize,
buf: VecDeque<T>,
}
impl<T> RingBuffer<T> {
pub fn new(capacity: usize) -> Self {
Self { capacity, buf: VecDeque::with_capacity(capacity) }
}
pub fn push(&mut self, item: T) {
if self.buf.len() == self.capacity {
self.buf.pop_front();
}
self.buf.push_back(item);
}
pub fn pop(&mut self) -> Option<T> {
self.buf.pop_front()
}
}