pub struct EventRingBuffer<E: Event + Copy> { /* private fields */ }Expand description
Lock-free ring buffer for event storage
Optimized for Single-Producer-Single-Consumer (SPSC) pattern with atomic head/tail pointers for wait-free operations.
§Thread Safety
This buffer is designed for SPSC (Single-Producer-Single-Consumer) use.
While it is Send + Sync, concurrent multi-producer or multi-consumer
access may lead to data races or lost events. For MPSC patterns,
use external synchronization or the ShardedEventBus which provides
isolation through sharding.
§Memory Ordering
- Producer writes data before publishing tail (Release)
- Consumer reads head with Acquire before accessing data
- This ensures data visibility across threads in SPSC mode
Implementations§
Source§impl<E: Event + Copy> EventRingBuffer<E>
impl<E: Event + Copy> EventRingBuffer<E>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create new ring buffer with specified capacity
Capacity must be power of 2 for efficient modulo operations.
Sourcepub fn push(&self, event: E) -> Result<(), E>
pub fn push(&self, event: E) -> Result<(), E>
Push event to buffer
Returns Err(event) if buffer is full. Time complexity: O(1), typically <100ns
Sourcepub fn pop(&self) -> Option<E>
pub fn pop(&self) -> Option<E>
Pop event from buffer
Returns None if buffer is empty. Time complexity: O(1), typically <100ns
Sourcepub fn fill_ratio(&self) -> f32
pub fn fill_ratio(&self) -> f32
Get fill percentage (0.0 to 1.0)
Trait Implementations§
impl<E: Event + Copy> Send for EventRingBuffer<E>
impl<E: Event + Copy> Sync for EventRingBuffer<E>
Auto Trait Implementations§
impl<E> !Freeze for EventRingBuffer<E>
impl<E> !RefUnwindSafe for EventRingBuffer<E>
impl<E> Unpin for EventRingBuffer<E>where
E: Unpin,
impl<E> UnwindSafe for EventRingBuffer<E>where
E: UnwindSafe,
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
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>
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>
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