xan_actor/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
extern crate proc_macro;
#[macro_use]
extern crate quote;
mod actor;
mod actor_system;
mod messenger;
#[proc_macro]
/// Generate actor system struct and inner methods
/// It contains LifeCycle, WhoisResponse struct too
pub fn actor_system(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
actor_system::actor_system(input)
}
#[proc_macro]
/// Genereate actor struct and inner methods
/// # Arguments
/// * `actor_struct_name` - Name of the actor struct
/// * `message_struct` - Message struct that the actor will receive
/// * `resource_struct` - Resource struct that the actor will use
/// * `handle_message` - Method that will handle the message
/// * `pre_start` - Method that will be called before the actor starts
/// * `post_stop` - Method that will be called after the actor stops
/// * `pre_restart` - Method that will be called before the actor restarts
/// * `post_restart` - Method that will be called after the actor restarts
/// * `kill_in_error` - kill the actor if an error occurs - TODO: not supported yet
///
/// # Example
/// use xan_actor::{actor, actor_system, recv_res, send_msg};
///
/// actor_system!(); // It always needs to be called once in lib.rs or main.rs
///
/// actor!(
/// TestActor,
/// struct Message {
/// pub message: String,
/// },
/// struct TestActorResource {
/// pub name: 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
/// );
pub fn actor(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
actor::actor(input)
}
#[proc_macro]
/// Send a message to an actor
/// # Arguments
/// * `actor_system` - Actor system that the actor belongs to
/// * `address` - Address of the actor
/// * `message` - Message to send
///
/// # Example
/// use xan_actor::send_msg;
/// send_msg!(
/// &mut actor_system,
/// "test-actor".to_string(),
/// &"test".to_string()
/// );
pub fn send_msg(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
messenger::send_msg(input)
}
#[proc_macro]
/// Receive a response from an actor
/// # Arguments
/// * `res_type` - Type of the response
/// * `response_rx` - Response receiver
///
/// # Example
/// use xan_actor::{recv_res, send_msg};
/// let response_rx = send_msg!(
/// &mut actor_system,
/// "test-actor".to_string(),
/// &"test".to_string()
/// );
/// let response = recv_res!(
/// String,
/// response_rx
/// );
pub fn recv_res(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
messenger::recv_res(input)
}