Skip to content

Commit c1f7854

Browse files
authored
Merge pull request #130 from NULLx76/swap_enqueue_push
swap dequeue and push
2 parents 728c9ab + bef917a commit c1f7854

File tree

9 files changed

+300
-284
lines changed

9 files changed

+300
-284
lines changed

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@ MSRV: Rust 1.59
2727
```rust
2828
use ringbuffer::{AllocRingBuffer, RingBuffer};
2929

30-
fn main() {
31-
let mut buffer = AllocRingBuffer::with_capacity(2);
30+
let mut buffer = AllocRingBuffer::with_capacity(2);
3231

33-
// First entry of the buffer is now 5.
34-
buffer.push(5);
32+
// First entry of the buffer is now 5.
33+
buffer.push(5);
3534

36-
// The last item we pushed is 5
37-
assert_eq!(buffer.back(), Some(&5));
35+
// The last item we pushed is 5
36+
assert_eq!(buffer.back(), Some(&5));
3837

39-
// Second entry is now 42.
40-
buffer.push(42);
41-
assert_eq!(buffer.peek(), Some(&5));
42-
assert!(buffer.is_full());
43-
44-
// Because capacity is reached the next push will be the first item of the buffer.
45-
buffer.push(1);
46-
assert_eq!(buffer.to_vec(), vec![42, 1]);
47-
}
38+
// Second entry is now 42.
39+
buffer.push(42);
40+
assert_eq!(buffer.peek(), Some(&5));
41+
assert!(buffer.is_full());
4842

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

5148
# Features

benches/bench.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn benchmark_push<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new: F) {
77
let mut rb = new();
88

99
for i in 0..1_000_000 {
10-
rb.push(i);
10+
rb.enqueue(i);
1111
black_box(());
1212
}
1313

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

2222
for _i in 0..100_000 {
23-
rb.push(1);
23+
rb.enqueue(1);
2424
black_box(());
25-
rb.push(2);
25+
rb.enqueue(2);
2626
black_box(());
2727

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

31-
rb.push(1);
31+
rb.enqueue(1);
3232
black_box(());
33-
rb.push(2);
33+
rb.enqueue(2);
3434
black_box(());
3535

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

39-
rb.push(1);
39+
rb.enqueue(1);
4040
black_box(());
41-
rb.push(2);
41+
rb.enqueue(2);
4242
black_box(());
4343

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

5656
for i in 0..100_000 {
57-
rb.push(i);
57+
rb.enqueue(i);
5858
black_box(());
5959
black_box(rb.back());
6060
}

0 commit comments

Comments
 (0)