EsEntity

Trait EsEntity 

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

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

    // 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§

Required Methods§

Source

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

Returns an immutable reference to the entity’s events

Source

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

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", so this trait is not object safe.

Implementors§