pub struct RingBuffer<T, const N: usize> { /* private fields */ }Expand description
Fixed-capacity ring buffer.
A circular buffer that stores up to N-1 elements (one slot reserved
for distinguishing full from empty). All operations are O(1) and
allocation-free after construction.
§Type Parameters
T- The element typeN- Buffer capacity (must be > 1; usable capacity is N-1)
§Example
use laminar_core::alloc::RingBuffer;
let mut buffer: RingBuffer<u64, 4> = RingBuffer::new();
buffer.push(1).unwrap();
buffer.push(2).unwrap();
buffer.push(3).unwrap();
// buffer.push(4) would fail - only 3 slots usable
assert_eq!(buffer.pop(), Some(1));
assert_eq!(buffer.pop(), Some(2));§Thread Safety
This buffer is NOT thread-safe. For multi-threaded scenarios, use the
SPSC queue in tpc::spsc.
Implementations§
Source§impl<T, const N: usize> RingBuffer<T, N>
impl<T, const N: usize> RingBuffer<T, N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty ring buffer.
§Panics
Panics if N < 2 (need at least 2 slots for ring buffer semantics).
Sourcepub fn push(&mut self, item: T) -> Result<(), T>
pub fn push(&mut self, item: T) -> Result<(), T>
Push an item onto the buffer.
§Performance
O(1), no allocation.
§Errors
Returns Err(item) if the buffer is full, giving back ownership
of the item that couldn’t be inserted.
§Example
use laminar_core::alloc::RingBuffer;
let mut buf: RingBuffer<i32, 4> = RingBuffer::new();
buf.push(1).unwrap();
buf.push(2).unwrap();
buf.push(3).unwrap();
assert!(buf.push(4).is_err()); // FullSourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Peek at the front item without removing it.
Returns None if the buffer is empty.
§Performance
O(1), no allocation.
Sourcepub fn peek_mut(&mut self) -> Option<&mut T>
pub fn peek_mut(&mut self) -> Option<&mut T>
Get mutable reference to the front item without removing it.
Trait Implementations§
Source§impl<T, const N: usize> Default for RingBuffer<T, N>
impl<T, const N: usize> Default for RingBuffer<T, N>
Source§impl<T, const N: usize> Drop for RingBuffer<T, N>
impl<T, const N: usize> Drop for RingBuffer<T, N>
Source§impl<'a, T, const N: usize> IntoIterator for &'a RingBuffer<T, N>
impl<'a, T, const N: usize> IntoIterator for &'a RingBuffer<T, N>
Auto Trait Implementations§
impl<T, const N: usize> Freeze for RingBuffer<T, N>where
T: Freeze,
impl<T, const N: usize> RefUnwindSafe for RingBuffer<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for RingBuffer<T, N>where
T: Send,
impl<T, const N: usize> Sync for RingBuffer<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for RingBuffer<T, N>where
T: Unpin,
impl<T, const N: usize> UnsafeUnpin for RingBuffer<T, N>where
T: UnsafeUnpin,
impl<T, const N: usize> UnwindSafe for RingBuffer<T, N>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Returns the layout of the type.