pub struct RingBuf<T, const SIZE: usize>where
T: Copy,{ /* private fields */ }Expand description
A Circular / Ring buffer ‘Full’ is considered when it is SIZE-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,
pub const SIZE: usize = N
Sourcepub const fn new() -> RingBuf<T, N>
pub const fn new() -> RingBuf<T, N>
Create a new ringbuf with no data inside
This method does not allocate memory.
§Example
use 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 of the ring buffer is empty
§Example
use 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 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 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