use ringbuf::{traits::*, HeapRb};
let mut rb = HeapRb::<i32>::new(2);
assert_eq!(rb.push_overwrite(0), None);
assert_eq!(rb.push_overwrite(1), None);
assert_eq!(rb.push_overwrite(2), Some(0));
assert_eq!(rb.try_pop(), Some(1));
assert_eq!(rb.try_pop(), Some(2));
assert_eq!(rb.try_pop(), None);
In this example, the capacity of the ring buffer is 2, and the operation rb.push_overwrite(1) pushed the latest element such that the ring buffer is full. As said in the document
Pushes an item to the ring buffer overwriting the latest item if the buffer is full.
Returns overwritten item if overwriting took place.
Doesn't the latest item have value 1? rb.push_overwrite(2) should overwrite 1, and the subsequent pop operations should return Some(0) and Some(2).
The current example looks like rb.push_overwrite(2) overwrites the first element, and the subsequent pop returns the value in reverse order.
If comment rb.push_overwrite(2)
let mut rb = HeapRb::<i32>::new(2);
assert_eq!(rb.push_overwrite(0), None);
assert_eq!(rb.push_overwrite(1), None);
//assert_eq!(rb.push_overwrite(2), Some(0));
assert_eq!(rb.try_pop(), Some(0));
assert_eq!(rb.try_pop(), Some(1));
assert_eq!(rb.try_pop(), None);
The subsequent pop instead behaves as the normal order.
In this example, the capacity of the ring buffer is
2, and the operationrb.push_overwrite(1)pushed the latest element such that the ring buffer is full. As said in the documentDoesn't the latest item have value
1?rb.push_overwrite(2)should overwrite1, and the subsequentpopoperations should returnSome(0)andSome(2).The current example looks like
rb.push_overwrite(2)overwrites the first element, and the subsequentpopreturns the value in reverse order.If comment
rb.push_overwrite(2)The subsequent
popinstead behaves as the normal order.