Crate rsactor_derive

Source
Expand description

Derive macros for rsActor framework

This crate provides derive macros for the rsActor framework, allowing users to automatically implement common traits with sensible defaults.

§Actor Derive Macro

The #[derive(Actor)] macro provides a convenient way to implement the Actor trait for simple structs and enums that don’t require complex initialization logic.

§Generated Implementation

When you use #[derive(Actor)], it generates:

  • Args type set to Self (the struct or enum itself)
  • Error type set to std::convert::Infallible (never fails)
  • on_start method that simply returns the provided args

§Usage

§With Structs
use rsactor::Actor;

#[derive(Actor)]
struct MyActor {
    name: String,
    count: u32,
}
§With Enums
use rsactor::Actor;

#[derive(Actor)]
enum StateActor {
    Idle,
    Processing(String),
    Completed(i32),
}

This is equivalent to manually writing:

use rsactor::{Actor, ActorRef};
use std::convert::Infallible;

impl Actor for MyActor {
    type Args = Self;
    type Error = Infallible;

    async fn on_start(
        args: Self::Args,
        _actor_ref: &ActorRef<Self>,
    ) -> std::result::Result<Self, Self::Error> {
        Ok(args)
    }
}

§When to Use

Use the derive macro when:

  • Your actor doesn’t need complex initialization
  • You want to pass a fully constructed instance to spawn()
  • You don’t need custom error handling during initialization

For complex initialization (async resource setup, validation, etc.), implement the Actor trait manually.

Derive Macros§

Actor
Derive macro for automatically implementing the Actor trait.