Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Jun 9, 2023
2 parents e16be31 + 758ba10 commit 9d0cfac
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
# Ringbuffer
![Github Workflows](https://img.shields.io/github/actions/workflow/status/NULLx76/ringbuffer/rust.yml?branch=master&logo=github&style=for-the-badge)
![Github Workflows](https://img.shields.io/github/actions/workflow/status/NULLx76/ringbuffer/rust.yml?style=for-the-badge)
[![Docs.rs](https://img.shields.io/badge/docs.rs-ringbuffer-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/ringbuffer)
[![Crates.io](https://img.shields.io/crates/v/ringbuffer?logo=rust&style=for-the-badge)](https://crates.io/crates/ringbuffer)

The ringbuffer crate provides safe fixed size circular buffers (ringbuffers) in rust.

Implementations for three kinds of ringbuffers, with a mostly similar API are provided:

| type | description |
|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `AllocRingBuffer` | Ringbuffer allocated on the heap at runtime. This ringbuffer is still fixed size. This requires the alloc feature. |
| `GrowableAllocRingBuffer` | Ringbuffer allocated on the heap at runtime. This ringbuffer can grow in size, and is implemented as an `alloc::VecDeque` internally. This requires alloc and the alloc feature. |
| `ConstGenericRingBuffer` | Ringbuffer which uses const generics to allocate on the stack. |
| type | description |
|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`AllocRingBuffer`][1] | Ringbuffer allocated on the heap at runtime. This ringbuffer is still fixed size. This requires the alloc feature. |
| [`GrowableAllocRingBuffer`][2] | Ringbuffer allocated on the heap at runtime. This ringbuffer can grow in size, and is implemented as an `alloc::VecDeque` internally. This requires the alloc feature. |
| [`ConstGenericRingBuffer`][3] | Ringbuffer which uses const generics to allocate on the stack. |

All of these ringbuffers also implement the RingBuffer trait for their shared API surface.
All of these ringbuffers also implement the [RingBuffer][4] trait for their shared API surface.

[1]: https://docs.rs/ringbuffer/latest/ringbuffer/struct.AllocRingBuffer.html
[2]: https://docs.rs/ringbuffer/latest/ringbuffer/struct.GrowableAllocRingBuffer.html
[3]: https://docs.rs/ringbuffer/latest/ringbuffer/struct.ConstGenericRingBuffer.html
[4]: https://docs.rs/ringbuffer/latest/ringbuffer/trait.RingBuffer.html

MSRV: Rust 1.59

# Usage

```rust
use ringbuffer::{AllocRingBuffer, RingBuffer};

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

Expand Down
16 changes: 4 additions & 12 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ pub unsafe trait RingBuffer<T>:

/// dequeues the top item off the queue, but does not return it. Instead it is dropped.
/// If the ringbuffer is empty, this function is a nop.
fn skip(&mut self);
#[inline]
fn skip(&mut self) {
let _ = self.dequeue();
}

/// Returns an iterator over the elements in the ringbuffer,
/// dequeueing elements as they are iterated over.
Expand Down Expand Up @@ -403,17 +406,6 @@ pub use iter::{
RingBufferDrainingIterator, RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator,
};

/// Implement various functions on implementors of [`RingBuffer`].
/// This is to avoid duplicate code.
macro_rules! impl_ringbuffer_read {
() => {
#[inline]
fn skip(&mut self) {
let _ = self.dequeue().map(drop);
}
};
}

/// Implement various functions on implementors of [`RingBuffer`].
/// This is to avoid duplicate code.
macro_rules! impl_ringbuffer {
Expand Down
2 changes: 0 additions & 2 deletions src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ unsafe impl<T, SIZE: RingbufferSize> RingBuffer<T> for AllocRingBuffer<T, SIZE>
self.writeptr += 1;
}

impl_ringbuffer_read!();

fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
None
Expand Down
4 changes: 1 addition & 3 deletions src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::collections::VecDeque;
use core::ops::{Deref, DerefMut, Index, IndexMut};

/// A growable ringbuffer. Once capacity is reached, the size is doubled.
/// Wrapper of the built-in [`VecDeque`](std::collections::VecDeque) struct
/// Wrapper of the built-in [`VecDeque`] struct.
///
/// The reason this is a wrapper, is that we want `RingBuffers` to implement `Index<isize>`,
/// which we cannot do for remote types like `VecDeque`
Expand Down Expand Up @@ -164,8 +164,6 @@ unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
self.pop_front()
}

impl_ringbuffer_read!();

fn push(&mut self, value: T) {
self.push_back(value);
}
Expand Down
4 changes: 1 addition & 3 deletions src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ unsafe impl<T, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP
self.writeptr += 1;
}

impl_ringbuffer_read!();

fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
None
Expand Down Expand Up @@ -299,7 +297,7 @@ unsafe impl<T, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP
impl<T, const CAP: usize> Default for ConstGenericRingBuffer<T, CAP> {
/// Creates a buffer with a capacity specified through the Cap type parameter.
/// # Panics
/// Panics if `CAP` is 0 or not a power of two
/// Panics if `CAP` is 0
#[inline]
fn default() -> Self {
Self::new()
Expand Down

1 comment on commit 9d0cfac

@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.