6 releases

0.10.0 Apr 15, 2024
0.9.0 Mar 4, 2024
0.8.3 Mar 3, 2022
0.8.2 Feb 28, 2022
0.8.0 Jan 30, 2022

#174 in Asynchronous

MIT license

50KB
926 lines

Hannibal

build version downloads docs.rs docs contributors maintenance license

a small actor library

Why

Credit where credit is due: Hannibal is a fork of the excellent Xactor which unfortunately received very little interest by its original maintainer recently. As of now there is no breaking change here, we could still merge xactor and hannibal again. Please reach out via an issue if you are interested.

Documentation

Features

  • Async actors.
  • Actor communication in a local context.
  • Using Futures for asynchronous message handling.
  • Typed messages (No Any type). Generic messages are allowed.

Examples

use hannibal::*;

#[message(result = String)]
struct ToUppercase(String);

struct MyActor;

impl Actor for MyActor {}

impl Handler<ToUppercase> for MyActor {
    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: ToUppercase) -> String {
        msg.0.to_uppercase()
    }
}

#[hannibal::main]
async fn main() -> Result<()> {
    // Start actor and get its address
    let mut addr = MyActor.start().await?;

    // Send message `ToUppercase` to actor via addr
    let res = addr.call(ToUppercase("lowercase".to_string())).await?;
    assert_eq!(res, "LOWERCASE");
    Ok(())
}

Installation

Hannibal requires async-trait on userland.

With cargo add installed, run:

$ cargo add hannibal
$ cargo add async-trait

We also provide the tokio runtime instead of async-std. To use it, you need to activate runtime-tokio and disable default features.

You can edit your Cargo.toml as follows:

hannibal = { version = "x.x.x", features = ["runtime-tokio"], default-features = false }

References

Dependencies

~1–12MB
~145K SLoC