kameo 0.3.0

Simple tokio actors - mpsc channels & no macros
Documentation

Kameo

Simple tokio actors

  • ✅ Async Support
  • ✅ Links Between Actors (spawn_link/spawn_child)
  • ✅ MPSC Unbounded Channel for Messaging
  • ✅ Concurrent Queries
  • ✅ Panic Safe

Installing

Stable

[dependencies]
kameo = "0.1"

Nightly

[dependencies]
kameo = { version = "0.1", features = ["nightly"] }

Defining an Actor without Macros

// Define the actor state
struct Counter {
    count: i64,
}

impl Actor for Counter {}

// Define messages
struct Inc { amount: u32 }

impl Message<Counter> for Inc {
    type Reply = Result<i64, Infallible>;

    async fn handle(self, state: &mut Counter) -> Self::Reply {
        state.count += self.0 as i64;
        Ok(state.count)
    }
}

Defining an Actor with Macros

// Define the actor state
#[derive(Actor)]
struct Counter {
    count: i64,
}

// Define messages
#[actor]
impl Counter {
    #[message]
    fn inc(&mut self, amount: u32) -> Result<i64, Infallible> {
        self.count += amount as i64;
        Ok(self.count)
    }
}
// Derive Actor
impl kameo::Actor for Counter {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Counter")
    }
}

// Messages
struct Inc { amount: u32 }

impl kameo::Message<Counter> for Inc {
    type Reply = Result<i64, Infallible>;

    async fn handle(self, state: &mut Counter) -> Self::Reply {
        state.inc(self.amount)
    }
}

Spawning an Actor & Messaging

use kameo::{Spawn, ActorRef};

let counter_ref: ActorRef<Counter> = Counter { count: 0 }.spawn();

let count = counter_ref.send(Inc(42)).await?;
println!("Count is {count}");

Contributing

Contributions are welcome! Feel free to submit pull requests, create issues, or suggest improvements.

License

kameo is dual-licensed under either:

at your option.