https://github.com/nullx76/ringbuffer
A fixed-size circular buffer written in Rust.
https://github.com/nullx76/ringbuffer
circular-buffer hacktoberfest no-std ringbuffer rust
Last synced: 8 months ago
JSON representation
A fixed-size circular buffer written in Rust.
- Host: GitHub
- URL: https://github.com/nullx76/ringbuffer
- Owner: NULLx76
- License: mit
- Created: 2020-06-11T16:01:31.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-31T09:53:04.000Z (about 1 year ago)
- Last Synced: 2025-03-27T03:10:26.196Z (10 months ago)
- Topics: circular-buffer, hacktoberfest, no-std, ringbuffer, rust
- Language: Rust
- Homepage: https://crates.io/crates/ringbuffer
- Size: 469 KB
- Stars: 113
- Watchers: 2
- Forks: 23
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ringbuffer

[](https://docs.rs/ringbuffer)
[](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`][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][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};
let mut buffer = AllocRingBuffer::with_capacity(2);
// First entry of the buffer is now 5.
buffer.push(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]);
```
# Features
| name | default | description |
|-------|---------|--------------------------------------------------------------------------------------------------------------|
| alloc | ✓ | Disable this feature to remove the dependency on alloc. Disabling this feature makes `ringbuffer` `no_std`. |
# License
Licensed under MIT License