Skip to main content

EsEntity

Trait EsEntity 

Source
pub trait EsEntity: TryFromEvents<Self::Event, Self::Snapshot> + Send {
    type Event: EsEvent;
    type New: IntoEvents<Self::Event>;
    type Snapshot: EsSnapshot;

    // Required methods
    fn events(&self) -> &EntityEvents<Self::Event, Self::Snapshot>;
    fn events_mut(&mut self) -> &mut EntityEvents<Self::Event, Self::Snapshot>;

    // Provided method
    fn last_persisted(&self, n: usize) -> LastPersisted<'_, Self::Event> { ... }
}
Expand description

Required trait for all entities to be compatible and recognised by es-entity.

All Entity types implement this trait to satisfy the basic requirements for event sourcing. The trait ensures the entity implements traits like IntoEvents and has the required components like EntityEvent, with helper methods to access the events sequence. Implemented by the EsEntity derive macro.

§Example

ⓘ
use es_entity::*;
use serde::{Serialize, Deserialize};

entity_id!{ UserId }

#[derive(EsEvent, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[es_event(id = "UserId")]
pub enum UserEvent {
    Initialized { id: UserId, name: String },
}

// Compile-time error: Missing required trait implementations
// - TryFromEvents<UserEvent> for User
// - IntoEvents<UserEvent> for NewUser (associated type New)
// - NewUser type definition
#[derive(EsEntity)]
pub struct User {
    pub id: UserId,
    pub name: String,
    events: EntityEvents<UserEvent>,
}

Required Associated Types§

Source

type Event: EsEvent

Source

type New: IntoEvents<Self::Event>

Source

type Snapshot: EsSnapshot

The entity’s snapshot state, or NoSnapshot for an entity whose repo does not enable snapshot. No default: associated type defaults are unstable, so every hand-written impl adds one line.

Required Methods§

Source

fn events(&self) -> &EntityEvents<Self::Event, Self::Snapshot>

Returns an immutable reference to the entity’s events

Source

fn events_mut(&mut self) -> &mut EntityEvents<Self::Event, Self::Snapshot>

Returns mutable reference to the entity’s events

Provided Methods§

Source

fn last_persisted(&self, n: usize) -> LastPersisted<'_, Self::Event>

Returns the last n persisted events

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§