pub struct RingBuf<T: Copy, const SIZE: usize> { /* private fields */ }Expand description
A Circular / Ring buffer.
A fixed-capacity FIFO (First-In-First-Out) data structure that wraps around when it reaches the end of its backing array. Elements are added at the head and removed from the tail.
§Capacity
The buffer is considered ‘full’ when it contains SIZE - 1 elements.
This design choice simplifies the empty/full detection logic.
§Type Parameters
T: The type of elements stored (must beCopy)SIZE: The size of the backing array (usable capacity isSIZE - 1)
§Examples
use planck_noalloc::ringbuf::RingBuf;
let mut buf = RingBuf::<i32, 16>::new();
buf.push(10);
buf.push(20);
buf.push(30);
assert_eq!(buf.pop(), Some(10));
assert_eq!(buf.pop(), Some(20));
assert_eq!(buf.len(), 1);Implementations§
Source§impl<T, const N: usize> RingBuf<T, N>where
T: Copy,
impl<T, const N: usize> RingBuf<T, N>where
T: Copy,
Sourcepub const SIZE: usize = N
pub const SIZE: usize = N
The total size of the backing array. The usable capacity is SIZE - 1.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new ringbuf with no data inside
This method does not allocate memory.
§Example
use planck_noalloc::ringbuf::RingBuf;
// Create an empty ringbuf
let ringbuf = RingBuf::<u8, 8>::new();
assert!(ringbuf.is_empty());Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the ring buffer is empty
§Example
use planck_noalloc::ringbuf::RingBuf;
// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
assert!(ringbuf.is_empty());
ringbuf.push(42);
assert!(!ringbuf.is_empty());Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of the used buffer
§Example
use planck_noalloc::ringbuf::RingBuf;
// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
assert_eq!(ringbuf.len(), 0);
ringbuf.push(1);
ringbuf.push(2);
ringbuf.push(3);
assert_eq!(ringbuf.len(), 3);
ringbuf.pop();
assert_eq!(ringbuf.len(), 2);Sourcepub const fn is_full(&self) -> bool
pub const fn is_full(&self) -> bool
Returns true if the buffer is full
§Example
use planck_noalloc::ringbuf::RingBuf;
// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
for i in 0..6 {
ringbuf.push(i);
}
assert!(!ringbuf.is_full());
ringbuf.push(6);
assert!(ringbuf.is_full());Sourcepub const fn max_capacity(&self) -> usize
pub const fn max_capacity(&self) -> usize
Returns the maximum number of elements that can be stored in the ringbuf This is calculated as the size subtracted 1
Sourcepub fn try_push(&mut self, x: T) -> Result<(), T>
pub fn try_push(&mut self, x: T) -> Result<(), T>
Pushes (or enqueues) an element on the ring buffer
§Errors
Returns an error with the pushed value if the ringbuf is full
Sourcepub fn push(&mut self, x: T)
pub fn push(&mut self, x: T)
Pushes (or enqueues) an element on the ring buffer
§Panics
Panics if the ringbuf is full. If this is not what you want, see RingBuf::try_push or
RingBuf::push_unchecked.
Sourcepub unsafe fn push_unchecked(&mut self, x: T)
pub unsafe fn push_unchecked(&mut self, x: T)
Pushes (or enqueues) an element on the ring buffer
§Safety
This does not check if it is out of bounds, which may cause data to be overwritten