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> UnwindSafe for RingBuffer<T, N>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
Source§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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§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.
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Returns whether the given value has been niched. Read more
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
Writes data to
out indicating that a T is niched.