ActorClient

Trait ActorClient 

Source
pub trait ActorClient<T: ActorEntity>: Send + Sync {
    type Error: From<String> + Send + Sync;

    // Required methods
    fn inner(&self) -> &ResourceClient<T>;
    fn map_error(e: FrameworkError) -> Self::Error;

    // Provided methods
    fn get<'life0, 'async_trait>(
        &'life0 self,
        id: T::Id,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>, Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn delete<'life0, 'async_trait>(
        &'life0 self,
        id: T::Id,
    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Trait for resource-specific clients to inherit standard CRUD operations.

This trait reduces boilerplate by providing default implementations for common operations like get and delete.

§Example

use actor_framework::{ActorClient, ActorEntity, FrameworkError, ResourceClient};
use async_trait::async_trait;

// 1. Define Entity
#[derive(Clone, Debug)]
struct User { id: u32 }
#[derive(Debug)] struct UserCreate;
#[derive(Debug)] struct UserUpdate;
#[derive(Debug)] enum UserAction {}
#[derive(Debug)] struct UserError(String);

// Error must implement Display + Error + From<String> + Send + Sync
impl std::fmt::Display for UserError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::error::Error for UserError {}

impl From<String> for UserError {
    fn from(s: String) -> Self { UserError(s) }
}

#[async_trait]
impl ActorEntity for User {
    type Id = u32;
    type Create = UserCreate;
    type Update = UserUpdate;
    type Action = UserAction;
    type ActionResult = ();
    type Context = ();
    type Error = UserError;

    fn from_create_params(id: u32, _: UserCreate) -> Result<Self, Self::Error> {
        Ok(Self { id })
    }
    async fn on_update(&mut self, _: UserUpdate, _: &()) -> Result<(), Self::Error> { Ok(()) }
    async fn handle_action(&mut self, _: UserAction, _: &()) -> Result<(), Self::Error> { Ok(()) }
}

// 2. Define Client Wrapper
struct UserClient {
    inner: ResourceClient<User>,
}

// 3. Implement ActorClient
#[async_trait]
impl ActorClient<User> for UserClient {
    type Error = UserError;

    fn inner(&self) -> &ResourceClient<User> {
        &self.inner
    }

    fn map_error(e: FrameworkError) -> Self::Error {
        UserError(e.to_string())
    }
}

// 4. Usage
async fn usage(client: UserClient) {
    // get() and delete() are provided automatically!
    let _ = client.get(1).await;
    let _ = client.delete(1).await;
}

Required Associated Types§

Source

type Error: From<String> + Send + Sync

The resource-specific error type.

Required Methods§

Source

fn inner(&self) -> &ResourceClient<T>

Access the inner generic ResourceClient.

Source

fn map_error(e: FrameworkError) -> Self::Error

Map framework errors to the specific resource error type.

Provided Methods§

Source

fn get<'life0, 'async_trait>( &'life0 self, id: T::Id, ) -> Pin<Box<dyn Future<Output = Result<Option<T>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Fetch an entity by ID.

Source

fn delete<'life0, 'async_trait>( &'life0 self, id: T::Id, ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Delete an entity by ID.

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§