pub struct EventBuffer { /* private fields */ }Expand description
A bounded, FIFO event buffer.
When the buffer is at capacity and a new event is pushed, the oldest
event is evicted and returned. This mirrors the JS _eventBuffer
behavior in SyncEngine where old events are shifted when the
buffer exceeds _eventBufferSize.
§Examples
use stateset_sync::{EventBuffer, SyncEvent};
use serde_json::json;
let mut buffer = EventBuffer::new(2);
buffer.push(SyncEvent::new("a", "x", "1", json!({})));
buffer.push(SyncEvent::new("b", "x", "2", json!({})));
// Third push evicts the oldest event
let evicted = buffer.push(SyncEvent::new("c", "x", "3", json!({})));
assert!(evicted.is_some());
assert_eq!(evicted.unwrap().event_type, "a");
assert_eq!(buffer.len(), 2);Implementations§
Source§impl EventBuffer
impl EventBuffer
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new EventBuffer with the given capacity.
A capacity of 0 means the buffer will evict every event immediately.
Sourcepub fn push(&mut self, event: SyncEvent) -> Option<SyncEvent>
pub fn push(&mut self, event: SyncEvent) -> Option<SyncEvent>
Push an event into the buffer.
If the buffer is at capacity, the oldest event is evicted and returned.
Returns None if no eviction was needed.
Sourcepub fn drain_all(&mut self) -> Vec<SyncEvent>
pub fn drain_all(&mut self) -> Vec<SyncEvent>
Drain all events from the buffer, returning them in FIFO order.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EventBuffer
impl RefUnwindSafe for EventBuffer
impl Send for EventBuffer
impl Sync for EventBuffer
impl Unpin for EventBuffer
impl UnsafeUnpin for EventBuffer
impl UnwindSafe for EventBuffer
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