Struct texcraft_stdext::collections::circularbuffer::CircularBuffer
source · pub struct CircularBuffer<T> { /* private fields */ }
Expand description
A circular buffer.
A circular buffer is an ordered buffer with a fixed capacity and the property that if it is full and a new element is pushed to the front, the oldest element is deleted.
The buffer is created using the new method, elements are pushed to the front using push, and retrieved using std::ops::Index::index trait method.
let mut buf = CircularBuffer::new(3);
buf.push(0);
buf.push(1);
assert_eq![buf.index(0), &1];
assert_eq![buf.index(1), &0];
buf.push(2);
buf.push(3);
assert_eq![buf.index(0), &3];
assert_eq![buf.index(1), &2];
assert_eq![buf.index(2), &1];
Implementations§
source§impl<T> CircularBuffer<T>
impl<T> CircularBuffer<T>
source§impl<T: Clone> CircularBuffer<T>
impl<T: Clone> CircularBuffer<T>
sourcepub fn clone_to_front(&mut self, i: usize) -> &mut T
pub fn clone_to_front(&mut self, i: usize) -> &mut T
Clone the element at the provided index to the front of the buffer.
The buffer contains the following optimization: if the buffer is full and the referenced element is the tail element, this method is a no-op. If the method’s clone function has side effects, these will not be seen, as in the following example.
/// A struct whose clone function has a side effect; i.e., is not pure.
/// The side effect is that a shared counter owned by all clones is incremented.
/// The shared counter thus records the number of times a clone has occured.
struct ImpureClone {
counter: Rc<RefCell<i64>>,
}
impl Clone for ImpureClone {
fn clone(&self) -> Self {
let old_count = *self.counter.borrow();
*self.counter.borrow_mut() = old_count + 1;
ImpureClone {
counter: self.counter.clone()
}
}
}
let mut buf = CircularBuffer::new(2);
let counter = Rc::new(RefCell::new(0));
buf.push(ImpureClone{counter: counter.clone()});
assert_eq![*counter.borrow(), 0];
buf.clone_to_front(0);
assert_eq![*counter.borrow(), 1];
// Clone from the tail - no clone occurs!
buf.clone_to_front(1);
assert_eq![*counter.borrow(), 1];
Trait Implementations§
Auto Trait Implementations§
impl<T> RefUnwindSafe for CircularBuffer<T>where T: RefUnwindSafe,
impl<T> Send for CircularBuffer<T>where T: Send,
impl<T> Sync for CircularBuffer<T>where T: Sync,
impl<T> Unpin for CircularBuffer<T>where T: Unpin,
impl<T> UnwindSafe for CircularBuffer<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more