-
Notifications
You must be signed in to change notification settings - Fork 49
The semantic of push_overwrite is a bit strange #55
Copy link
Copy link
Open
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels