Workload

Trait Workload 

Source
pub trait Workload:
    Send
    + Sync
    + 'static {
    type Dispatcher: MessageDispatcher<Workload = Self>;

    // Provided methods
    fn on_start<'life0, 'life1, 'async_trait, C>(
        &'life0 self,
        _ctx: &'life1 C,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             C: 'async_trait + Context,
             Self: 'async_trait { ... }
    fn on_stop<'life0, 'life1, 'async_trait, C>(
        &'life0 self,
        _ctx: &'life1 C,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             C: 'async_trait + Context,
             Self: 'async_trait { ... }
}
Expand description

Workload - Executable Actor workload

Represents a complete Actor instance, including:

  • Associated dispatcher type (Dispatcher)
  • Lifecycle hooks (on_start, on_stop)

§Design Characteristics

  • Bidirectional association: Workload::Dispatcher and MessageDispatcher::Workload reference each other
  • Default implementations: Lifecycle hooks have default no-op implementations, users can optionally override
  • Auto-implementation: Implemented for wrapper types by code generator

§Code Generation Example

// User-implemented Handler
pub struct MyEchoService { /* ... */ }

impl EchoServiceHandler for MyEchoService {
    async fn echo<C: Context>(
        &self,
        req: EchoRequest,
        ctx: &C,
    ) -> ActorResult<EchoResponse> {
        // Business logic
        Ok(EchoResponse { reply: format!("Echo: {}", req.message) })
    }
}

// Code-generated Workload wrapper
pub struct EchoServiceWorkload<T: EchoServiceHandler>(pub T);

impl<T: EchoServiceHandler> Workload for EchoServiceWorkload<T> {
    type Dispatcher = EchoServiceRouter<T>;
}

Required Associated Types§

Source

type Dispatcher: MessageDispatcher<Workload = Self>

Associated dispatcher type

Provided Methods§

Source

fn on_start<'life0, 'life1, 'async_trait, C>( &'life0 self, _ctx: &'life1 C, ) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, C: 'async_trait + Context, Self: 'async_trait,

Lifecycle hook: Called when Actor starts

§Default Implementation

Default is a no-op, users can optionally override to perform initialization logic.

§Example
async fn on_start<C: Context>(&self, ctx: &C) -> ActorResult<()> {
    tracing::info!("Actor {} started", ctx.self_id());
    // Initialize resources
    Ok(())
}
Source

fn on_stop<'life0, 'life1, 'async_trait, C>( &'life0 self, _ctx: &'life1 C, ) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, C: 'async_trait + Context, Self: 'async_trait,

Lifecycle hook: Called when Actor stops

§Default Implementation

Default is a no-op, users can optionally override to perform cleanup logic.

§Example
async fn on_stop<C: Context>(&self, ctx: &C) -> ActorResult<()> {
    tracing::info!("Actor {} stopping", ctx.self_id());
    // Cleanup resources
    Ok(())
}

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§