#ring-buffer #ring #buffer #vec

ringvec

A simple ring buffer implementation based on a vector

1 unstable release

0.1.0 Aug 26, 2021

#2316 in Data structures


Used in inditech

Custom license

8KB
199 lines

ringvec

License

A simple ring buffer implementation based on a vector. No dependencies except criterion for benchmarking.

Usage

use ringvec::RingVec;

fn main() {
    let mut v = RingVec::new(3);

    assert!(v.is_empty());
    assert!(!v.is_full());

    v.push(1);
    v.push(2);
    v.push(3);

    v.push_force(4);
    v.push_force(5);

    assert!(!v.is_empty());
    assert!(v.is_full());

    assert_eq!(v.peek_oldest(), Some(&3));
    assert_eq!(v.peek_newest(), Some(&5));

    assert_eq!(v.pop(), Some(3));

    assert!(!v.is_empty());
    assert!(!v.is_full());

    assert_eq!(v.pop(), Some(4));
    assert_eq!(v.pop(), Some(5));

    assert!(v.is_empty());
    assert!(!v.is_full());
}

Performance

It's optimized for peeking, but not slow when pushing too. I might've messed something up when it comes to benchmarking peeking.

  • push 7.4ns
  • peek oldest 0ns
  • peek newest 0ns

No runtime deps