Skip to main content

ActiveEntitySet

Trait ActiveEntitySet 

Source
pub trait ActiveEntitySet<E>: ReadableDataSet<E> + WritableDataSet<E>
where E: Entity<Self::Value> + IntoRecord<Self::Value> + TryFromRecord<Self::Value> + Send + Sync + Clone,
{ // Provided methods fn get_entity<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 Self::Id, ) -> Pin<Box<dyn Future<Output = Result<Option<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn list_entities<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } fn get_some_entity<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Option<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } fn new_entity(&self, id: Self::Id, entity: E) -> ActiveEntity<'_, Self, E> { ... } }
Expand description

Change tracking for typed entities with automatic persistence.

This trait extends readable and writable datasets with a “entity” pattern that tracks entity modifications and enables deferred persistence. entities act as smart wrappers around entities that know how to save themselves back to storage.

§entity Pattern Benefits

  • Change tracking: Only modified fields are serialized and persisted
  • Type safety: Work with native Rust entities, not raw values
  • Optimistic locking: Conflict detection in concurrent scenarios
  • Deferred persistence: Batch multiple changes before saving
  • Interactive editing: Perfect for UI scenarios with undo/redo

§Example

use vantage_dataset::dataset::entityDataSet;

// Get entity for interactive editing
let mut user_entity = users.get_entity(&user_id).await?.unwrap();

// Modify through standard field access
user_entity.name = "Alice Smith".to_string();
user_entity.age = 31;

// Changes are automatically tracked and persisted
user_entity.save().await?;

// Or work with multiple entities
let mut entities = users.list_entities().await?;
for mut entity in entities {
    entity.status = Status::Processed;
    entity.save().await?; // Each saves independently
}

Provided Methods§

Source

fn get_entity<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 Self::Id, ) -> Pin<Box<dyn Future<Output = Result<Option<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieve an entity wrapped for change tracking and deferred persistence.

The returned entity can be modified in-place and will track all changes for efficient persistence when save() is called.

§Example
let mut user = table.get_entity(&"user123".to_string()).await?
    .unwrap_or_else(|| table.new_entity("user123".to_string(), User {
        id: Some("user123".to_string()),
        name: "Default User".to_string(),
        active: false,
    }));

user.active = true;
user.save().await?;
§Returns
  • Ok(Some(entity)): Entity wrapper with change tracking
  • Ok(None): entity doesn’t exist
  • Err: Storage or deserialization error
Source

fn list_entities<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Retrieve all entities wrapped for change tracking.

Each returned entity operates independently - modifications to one entity don’t affect others, and each must be saved separately.

§Performance Note

This loads and deserializes all entities into memory. Consider pagination or streaming approaches for large datasets.

Source

fn get_some_entity<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Option<ActiveEntity<'_, Self, E>>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Retrieve some entity wrapped for change tracking and deferred persistence.

This is equivalent to get_some() but returns an ActiveEntity wrapper.

§Returns
  • Ok(Some(entity)): Entity wrapper with change tracking
  • Ok(None): no entities exist in the dataset
  • Err: Storage or deserialization error
Source

fn new_entity(&self, id: Self::Id, entity: E) -> ActiveEntity<'_, Self, E>

Create a new entity with the provided data.

This method creates a new entity and returns it wrapped as an ActiveEntity. The entity is not automatically saved - call .save() to persist it.

§Parameters
  • id: The ID for the new entity
  • entity: The entity data
§Returns
  • ActiveEntity: New entity wrapper ready for modification and saving
§Example
let mut user = table.get_entity(&"user123".to_string()).await?
    .unwrap_or_else(|| table.new_entity("user123".to_string(), User {
        id: Some("user123".to_string()),
        name: "Default User".to_string(),
        active: false,
    }));

user.active = true;
user.save().await?;
§Note

This method does not check if an entity with the given ID already exists. Use in combination with get_entity() for get-or-create patterns.

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§

Source§

impl<T, E> ActiveEntitySet<E> for T