Expand description
§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 the alloc feature. |
ConstGenericRingBuffer | Ringbuffer which uses const generics to allocate on the stack. |
All of these ringbuffers also implement the RingBuffer trait for their shared API surface.
MSRV: Rust 1.79
§Usage
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
Macros§
- impl_
ring_ buffer_ set_ len - Implement
set_lengiven areadptrand awriteptr.
Structs§
- Alloc
Ring Buffer - The
AllocRingBufferis aRingBufferwhich is based on a Vec. This means it allocates at runtime on the heap, and therefore needs thealloccrate. This struct and therefore the dependency on alloc can be disabled by disabling thealloc(default) feature. - Const
Generic Ring Buffer - The
ConstGenericRingBufferstruct is aRingBufferimplementation which does not requireallocbut uses const generics instead. - Growable
Alloc Ring Buffer - A growable ringbuffer. Once capacity is reached, the size is doubled.
Wrapper of the built-in
VecDequestruct.
Traits§
- Ring
Buffer RingBufferis a trait defining the standard interface for allRingBufferimplementations (AllocRingBuffer,ConstGenericRingBuffer)- SetLen
SetLenis a trait defining the unsafeset_lenmethod on ringbuffers that support the operation.