#model

oricalchum

A lightweight actor model

2 releases

0.1.1 Oct 14, 2024
0.1.0 Oct 13, 2024

#143 in #model

Download history 89/week @ 2024-10-07 234/week @ 2024-10-14 5/week @ 2024-10-21 2/week @ 2024-11-04

330 downloads per month
Used in oricalchum_derive

MIT license

11KB
137 lines

oricalchum

A lightweight, high-performance actor library

Usage

use async_trait::async_trait;
use oricalchum::{Actor, ActorSystem, Context};

pub enum Test {
    PrintOk(String),
    PrintErr(String, i32),
}

pub struct TestActor {
    pub name: String,
    pub value: String
}

#[async_trait]
impl Actor for TestActor {
    type Msg = Test;

    async fn handle(&mut self, msg: Self::Msg, ctx: &mut Context<Self>) {
        match msg {
            Test::PrintOk(text) => {
                println!("{} {}", self.name, text);
            }
            Test::PrintErr(text, b) => {
                println!("{} {}", text, b);
            }
        }
    }
}

#[tokio::main]
async fn main() {
    let actor1 = TestActor { name: String::from("actor1"), value: String::from("test1") };

    let addr1 = ActorSystem::spawn_actor(actor1, 16).await;

    addr1.send(Test::PrintOk(String::from("Ok"))).await;
}

Dependencies

~2.6–8.5MB
~74K SLoC