ringvec

Crates.ioringvec
lib.rsringvec
version0.1.0
sourcesrc
created_at2021-08-26 07:43:00.514607
updated_at2021-08-26 07:43:00.514607
descriptionA simple ring buffer implementation based on a vector
homepagehttps://github.com/stjepangolemac/ringvec
repositoryhttps://github.com/stjepangolemac/ringvec
max_upload_size
id442478
size9,492
Stjepan Golemac (stjepangolemac)

documentation

https://docs.rs/ringvec

README

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
Commit count: 4

cargo fmt