pub struct Entity { /* private fields */ }Implementations§
Source§impl Entity
impl Entity
pub fn new() -> Self
pub fn with_id(id: impl Into<String>) -> Self
pub fn id(&self) -> &str
pub fn set_id(&mut self, id: impl Into<String>)
pub fn version(&self) -> u64
pub fn snapshot_version(&self) -> u64
pub fn set_snapshot_version(&mut self, snapshot_version: u64)
pub fn committed_version(&self) -> u64
pub fn events(&self) -> &[EventRecord]
Sourcepub fn take_events(&mut self) -> Vec<EventRecord>
pub fn take_events(&mut self) -> Vec<EventRecord>
Take the in-memory events out of the entity, leaving it empty.
Used by replay paths that need to iterate events while also holding a
mutable borrow of the owning aggregate (the borrow checker forbids
borrowing events immutably and the aggregate mutably at once). The
caller is responsible for restoring history afterward — e.g. via
load_from_history — so the entity’s
version/committed_version invariants hold.
Sourcepub fn restore_history(&mut self, events: Vec<EventRecord>)
pub fn restore_history(&mut self, events: Vec<EventRecord>)
Put a previously taken event history back without
recomputing version/prefix_version/committed_version. The caller
must restore the same events the entity already accounted for, so the
existing invariants continue to hold (used by replay paths that only
borrowed the events out to satisfy the borrow checker).
Sourcepub fn new_events(&self) -> &[EventRecord]
pub fn new_events(&self) -> &[EventRecord]
Returns events added since the entity was loaded (not yet persisted).
Slices relative to prefix_version so it is correct whether the entity
holds the full history (prefix_version == 0) or only a snapshot tail.
Sourcepub fn mark_committed(&mut self)
pub fn mark_committed(&mut self)
Mark all current events as committed. Called by repository after successful commit.
Sourcepub fn set_metadata(&mut self, metadata: HashMap<String, String>)
pub fn set_metadata(&mut self, metadata: HashMap<String, String>)
Set metadata that will be attached to every subsequent event.
Call this before invoking command methods to propagate context (correlation IDs, user info, trace spans) into the event stream.
Sourcepub fn set_meta(&mut self, key: impl Into<String>, value: impl Into<String>)
pub fn set_meta(&mut self, key: impl Into<String>, value: impl Into<String>)
Set a single metadata key-value pair.
Sourcepub fn set_correlation_id(&mut self, id: impl Into<String>)
pub fn set_correlation_id(&mut self, id: impl Into<String>)
Set the correlation ID for subsequent events.
Sourcepub fn set_causation_id(&mut self, id: impl Into<String>)
pub fn set_causation_id(&mut self, id: impl Into<String>)
Set the causation ID for subsequent events.
Sourcepub fn clear_metadata(&mut self)
pub fn clear_metadata(&mut self)
Clear all metadata.
Sourcepub fn digest<T: Serialize>(
&mut self,
name: impl Into<String>,
payload: &T,
) -> SourcedResult
pub fn digest<T: Serialize>( &mut self, name: impl Into<String>, payload: &T, ) -> SourcedResult
Record an event with a serializable payload.
If serialization fails, the entity is left unchanged. Any metadata set on the entity is attached to the event.
Sourcepub fn digest_v<T: Serialize>(
&mut self,
name: impl Into<String>,
version: u64,
payload: &T,
) -> SourcedResult
pub fn digest_v<T: Serialize>( &mut self, name: impl Into<String>, version: u64, payload: &T, ) -> SourcedResult
Record a versioned event.
If serialization fails, the entity is left unchanged.
Sourcepub fn digest_empty(&mut self, name: impl Into<String>) -> SourcedResult
pub fn digest_empty(&mut self, name: impl Into<String>) -> SourcedResult
Record an event with no payload.
pub fn load_from_history(&mut self, history: Vec<EventRecord>)
Sourcepub fn load_tail_from_history(
&mut self,
tail: Vec<EventRecord>,
prefix_version: u64,
)
pub fn load_tail_from_history( &mut self, tail: Vec<EventRecord>, prefix_version: u64, )
Load only the tail of a stream — the events after a known prefix that is not held in memory (e.g. events already folded into a snapshot).
Unlike load_from_history, the in-memory
events here are a suffix of the durable stream, so version and
committed_version must be supplied explicitly rather than derived from
events.len(). They reflect the true persisted stream position:
committed_version is the optimistic-concurrency expected_version for
the next commit, and new_events() slices relative to it.
§Invariant
prefix_version is the count of events covered by the omitted prefix.
Callers must guarantee that every event in tail has
sequence > prefix_version and that sequences are contiguous, so the
resulting version == prefix_version + tail.len() equals the true
max(sequence). Snapshots satisfy this: prefix_version is the snapshot
version and the tail is exactly sequence > snapshot.version.
pub fn rehydrate<F, E>(&mut self, apply: F) -> Result<(), E>
pub fn is_replaying(&self) -> bool
pub fn set_replaying(&mut self, replaying: bool)
Trait Implementations§
Source§impl Committable for Entity
impl Committable for Entity
Source§fn entities_mut(&mut self) -> Vec<&mut Entity>
fn entities_mut(&mut self) -> Vec<&mut Entity>
Source§impl<'de> Deserialize<'de> for Entity
impl<'de> Deserialize<'de> for Entity
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Entity
impl RefUnwindSafe for Entity
impl Send for Entity
impl Sync for Entity
impl Unpin for Entity
impl UnsafeUnpin for Entity
impl UnwindSafe for Entity
Blanket Implementations§
Source§impl<T> AggregateBuilder for T
impl<T> AggregateBuilder for T
fn aggregate<A: Aggregate>(self) -> AggregateRepository<Self, A>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Queueable for T
impl<T> Queueable for T
Source§fn queued(self) -> QueuedRepository<Self, InMemoryLockManager>
fn queued(self) -> QueuedRepository<Self, InMemoryLockManager>
.aggregate::<T>() for per-aggregate serialization over the async
repository surface.