18 stable releases

3.0.1 Dec 31, 2024
2.1.1 Dec 30, 2024
1.3.2 Nov 14, 2024
1.2.10 Jul 8, 2024
1.1.2 Sep 30, 2023

#236 in Procedural macros

Download history 43/week @ 2024-09-24 17/week @ 2024-10-01 189/week @ 2024-10-29 40/week @ 2024-11-05 114/week @ 2024-11-12 10/week @ 2024-11-19 1/week @ 2024-11-26 10/week @ 2024-12-03 33/week @ 2024-12-10 265/week @ 2024-12-24 354/week @ 2024-12-31

653 downloads per month

MIT license

31KB
539 lines

Actor

Caution

This version has breaking changes from the previous version. The previous version is not compatible with this version.

Usage

  1. add dependencies
$ cargo add xan-actor bincode log
$ cargo add serde --features=serde_derive
$ cargo add tokio --features="rt-multi-thread macros sync"
  1. declare an actor_system in your lib.rs or main.rs with declare the message types that will be sent to the actors.
xan_actor::actor_system!(
    struct Message {
        pub message: String,
    },
    ...
);
  1. declare Actor to register
xan_actor::actor!(
    Actor, // actor name
    Message, // message type
    struct ActorResource {
        pub data: String,
    },
    fn handle_message(&self, message: Message) -> String {
        message.message
    },
    fn pre_start(&mut self) {},
    fn post_stop(&mut self) {},
    fn pre_restart(&mut self) {},
    fn post_restart(&mut self) {},
    true
);
  1. create ActorSystem & declared actor
#[tokio::main]
async fn main() {
    let (mut actor_system, register_tx) = ActorSystem::new();
    let actor = Actor::new(
        address!("test".to_string(), "actor".to_string(), "*".to_string()),
        ActorResource {
            data: "test".to_string(),
        },
        register_tx,
    );
    ...
}
  1. run actor
#[tokio::main]
async fn main() {
   ...
    let (_handle, ready_rx) = actor.run();
    ready_rx.await.unwrap();
    ...
}
  1. send and receive messages
#[tokio::main]
async fn main() {
    ...
    let response_rxs = send_msg!(
        &mut actor_system,
        address!("test".to_string(), "actor".to_string(), "*".to_string()), // actor address
        &"test".to_string()                                                 // message
    );

    for response in recv_res!(String /* return type */, response_rxs) {
        println!("{}", response);
    }
    ...
}

Dependencies

~215–660KB
~16K SLoC