Skip to main content

Outbox

Struct Outbox 

Source
pub struct Outbox { /* private fields */ }
Expand description

An event outbox.

Events are appended and assigned monotonically increasing sequence numbers. The outbox can be drained (consumed) in order for push operations, or peeked without consuming.

By default this is in-memory, but it can be backed by a durable JSON snapshot via Outbox::with_persistence.

§Examples

use stateset_sync::{Outbox, SyncEvent};
use serde_json::json;

let mut outbox = Outbox::new(100);
let seq = outbox.append(SyncEvent::new("order.created", "order", "ORD-1", json!({}))).unwrap();
assert_eq!(seq, 1);
assert_eq!(outbox.count(), 1);

Implementations§

Source§

impl Outbox

Source

pub const fn new(max_capacity: usize) -> Self

Create a new in-memory Outbox with the given maximum capacity.

Source

pub const fn with_default_capacity() -> Self

Create a new Outbox with the default maximum capacity (10,000).

Source

pub fn with_persistence( max_capacity: usize, path: impl AsRef<Path>, ) -> Result<Self, SyncError>

Create a durable outbox persisted to path.

If the snapshot already exists, it is loaded and reused.

§Errors

Returns SyncError::Storage if snapshot I/O fails.

Source

pub fn append(&mut self, event: SyncEvent) -> Result<u64, SyncError>

Append an event to the outbox, assigning it the next sequence number.

Returns the assigned sequence number.

§Errors

Returns SyncError::OutboxFull if the outbox is at capacity or SyncError::Storage if durable persistence fails.

Source

pub fn drain(&mut self, count: usize) -> Result<Vec<SyncEvent>, SyncError>

Drain up to count events from the front of the outbox (FIFO order).

Drained events are removed from the outbox.

§Errors

Returns SyncError::Storage if durable persistence cannot be updated. In that case, the original in-memory ordering is restored.

Source

pub fn peek(&self, count: usize) -> Vec<&SyncEvent>

Peek at up to count events from the front of the outbox without consuming them.

Source

pub fn retain<F>(&mut self, predicate: F)
where F: FnMut(&SyncEvent) -> bool,

Retain only events matching the given predicate.

Preserves FIFO order and does not modify sequence allocation.

Source

pub fn try_retain<F>(&mut self, predicate: F) -> Result<(), SyncError>
where F: FnMut(&SyncEvent) -> bool,

Retain only events matching the given predicate and persist the result.

§Errors

Returns SyncError::Storage if durable persistence fails. In that case, the original in-memory ordering is restored.

Source

pub fn count(&self) -> usize

Return the number of events currently in the outbox.

Source

pub fn is_empty(&self) -> bool

Whether the outbox is empty.

Source

pub fn is_full(&self) -> bool

Whether the outbox is at maximum capacity.

Source

pub fn clear(&mut self)

Remove all events from the outbox.

Source

pub const fn max_capacity(&self) -> usize

Return the maximum capacity of the outbox.

Source

pub const fn next_sequence(&self) -> u64

Return the next sequence number that will be assigned.

Trait Implementations§

Source§

impl Debug for Outbox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Outbox

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.