pub struct RingBuffer<'a, T: Pod + FixedLayout> { /* private fields */ }Expand description
Fixed-capacity circular buffer overlaid on a byte slice.
pushalways succeeds. When full, overwrites the oldest entry.- Use for event journals, audit logs, price history, etc.
- O(1) push, O(1) read by logical index.
Implementations§
Source§impl<'a, T: Pod + FixedLayout> RingBuffer<'a, T>
impl<'a, T: Pod + FixedLayout> RingBuffer<'a, T>
Sourcepub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
Overlay a RingBuffer on a mutable byte slice.
Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Current number of elements (may be less than capacity if not yet full).
Sourcepub fn push(&mut self, value: T) -> Result<(), ProgramError>
pub fn push(&mut self, value: T) -> Result<(), ProgramError>
Push an element. If the buffer is full, overwrites the oldest entry.
Sourcepub fn get(&self, logical_index: usize) -> Result<T, ProgramError>
pub fn get(&self, logical_index: usize) -> Result<T, ProgramError>
Read the element at logical index (0 = oldest still in buffer).
Sourcepub fn latest(&self) -> Result<T, ProgramError>
pub fn latest(&self) -> Result<T, ProgramError>
Read the most recently pushed element.
Sourcepub fn oldest(&self) -> Result<T, ProgramError>
pub fn oldest(&self) -> Result<T, ProgramError>
Read the oldest element.
Sourcepub const fn required_bytes(capacity: usize) -> usize
pub const fn required_bytes(capacity: usize) -> usize
Compute the byte size needed for a RingBuffer with the given capacity.
Auto Trait Implementations§
impl<'a, T> Freeze for RingBuffer<'a, T>
impl<'a, T> RefUnwindSafe for RingBuffer<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for RingBuffer<'a, T>where
T: Send,
impl<'a, T> Sync for RingBuffer<'a, T>where
T: Sync,
impl<'a, T> Unpin for RingBuffer<'a, T>where
T: Unpin,
impl<'a, T> UnsafeUnpin for RingBuffer<'a, T>
impl<'a, T> !UnwindSafe for RingBuffer<'a, T>
Blanket Implementations§
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