[][src]Crate act_zero

act-zero

An actor system for Rust, designed with several goals in mind:

  • No boilerplate.
  • Ergonomic.
  • Supports standard trait-based static and dynamic polymorphism.
  • Embraces async/await.
  • Executor agnostic.

There are also some basic building blocks to support remoting, but the actual mechanism to transfer messages is left to the user.

Very little code is required to get started:

use std::error::Error;

use futures::executor::LocalPool;
use act_zero::*;

struct SimpleGreeter {
    number_of_greets: i32,
}

impl Actor for SimpleGreeter {
    type Error = ();
}

#[act_zero]
trait Greeter {
    fn greet(&self, name: String, res: Sender<String>);
}

#[act_zero]
impl Greeter for SimpleGreeter {
    async fn greet(&mut self, name: String, res: Sender<String>) {
        self.number_of_greets += 1;
        res.send(format!(
            "Hello, {}. You are number {}!",
            name, self.number_of_greets
        ))
        .ok();
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut pool = LocalPool::new();
    let spawner = pool.spawner();
    pool.run_until(async move {
        let actor_ref = spawn(
            &spawner,
            SimpleGreeter {
                number_of_greets: 0,
            },
        )?;

        let greeting = actor_ref.call_greet("John".into()).await?;
        println!("{}", greeting);

        let greeting = actor_ref.call_greet("Emma".into()).await?;
        println!("{}", greeting);
        Ok(())
    })
}

See #[act_zero] for documentation on the attribute which powers this crate.

Modules

async_fn

This module contains tools for dealing with async functions. These are similar to the FnOnce trait from std, but they allow the lifetime of the returned future to be bound by the lifetime of the argument. The different variants correspond to the different reference types (&T, &mut T) but all variants consume self like FnOnce.

remote

Tools for creating actor proxies. These can be used to communicate with actors in a different process, or on a different machine.

utils

Utility types and traits. These are typically used internally or by macro expansions and you will not normally need to use them directly.

Structs

Addr

Strong reference to an actor. This will not prevent the actor from stopping of its own volition, but the actor will not be automatically stopped as long as a strong reference still exists. If the actor has stopped, messages sent to the actor will be dropped.

Local

Type of an actor running locally.

Receiver

A future for a value that will be provided by another asynchronous task.

Sender

A means of transmitting a single value to another task.

WeakAddr

Weak reference to an actor. If the actor has been dropped, messages sent to the actor will also be dropped.

Traits

Actor

Implement this trait for types representing actors. The only requirement is that you specify an Error type, all other methods are optional.

Handle

This type is automatically implemented for local actors which implement the actor trait corresponding to the message type M.

Functions

channel

Creates a new one-shot channel for sending values across asynchronous tasks.

spawn

Spawn an actor the provided spawner, returning its address or an error.

Attribute Macros

act_zero

The #[act_zero] macro.