Skip to content

Commit

Permalink
Merge branch 'main' into enqueue-return
Browse files Browse the repository at this point in the history
  • Loading branch information
NULLx76 committed May 15, 2024
2 parents e817767 + 137f388 commit 088f780
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 309 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "ringbuffer"
version = "0.15.0"
authors = [
"Victor Roest <victor@xirion.net>",
"Vivian Roest <vivian@xirion.net>",
"Jonathan Dönszelmann <[email protected]>",
]
edition = "2021"
Expand Down
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ MSRV: Rust 1.59
```rust
use ringbuffer::{AllocRingBuffer, RingBuffer};

fn main() {
let mut buffer = AllocRingBuffer::with_capacity(2);
let mut buffer = AllocRingBuffer::with_capacity(2);

// First entry of the buffer is now 5.
buffer.push(5);
// First entry of the buffer is now 5.
buffer.push(5);

// The last item we pushed is 5
assert_eq!(buffer.back(), Some(&5));
// The last item we pushed is 5
assert_eq!(buffer.back(), Some(&5));

// Second entry is now 42.
buffer.push(42);
assert_eq!(buffer.peek(), Some(&5));
assert!(buffer.is_full());

// Because capacity is reached the next push will be the first item of the buffer.
buffer.push(1);
assert_eq!(buffer.to_vec(), vec![42, 1]);
}
// Second entry is now 42.
buffer.push(42);
assert_eq!(buffer.peek(), Some(&5));
assert!(buffer.is_full());

// Because capacity is reached the next push will be the first item of the buffer.
buffer.push(1);
assert_eq!(buffer.to_vec(), vec![42, 1]);
```

# Features
Expand Down
16 changes: 8 additions & 8 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn benchmark_push<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new: F) {
let mut rb = new();

for i in 0..1_000_000 {
rb.push(i);
rb.enqueue(i);
black_box(());
}

Expand All @@ -20,25 +20,25 @@ fn benchmark_push_dequeue<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new
let mut rb = new();

for _i in 0..100_000 {
rb.push(1);
let _ = rb.enqueue(1);
black_box(());
rb.push(2);
let _ = rb.enqueue(2);
black_box(());

assert_eq!(black_box(rb.dequeue()), Some(1));
assert_eq!(black_box(rb.dequeue()), Some(2));

rb.push(1);
let _ = rb.enqueue(1);
black_box(());
rb.push(2);
let _ = rb.enqueue(2);
black_box(());

assert_eq!(black_box(rb.dequeue()), Some(1));
assert_eq!(black_box(rb.dequeue()), Some(2));

rb.push(1);
let _ = rb.enqueue(1);
black_box(());
rb.push(2);
let _ = rb.enqueue(2);
black_box(());

assert_eq!(black_box(rb.get_signed(-1)), Some(&2));
Expand All @@ -54,7 +54,7 @@ fn benchmark_various<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new: F)
let mut rb = new();

for i in 0..100_000 {
rb.push(i);
rb.enqueue(i);
black_box(());
black_box(rb.back());
}
Expand Down
Loading

1 comment on commit 088f780

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.