ractor

Macro call_t

Source
macro_rules! call_t {
    ($actor:expr, $msg:expr, $timeout_ms:expr) => { ... };
    ($actor:expr, $msg:expr, $timeout_ms:expr, $($args:expr),*) => { ... };
}
Expand description

call_t!: Perform an finite-time remote procedure call to an crate::Actor

  • $actor - The actor to call
  • $msg - The message builder variant
  • $timeout_ms - the timeout in milliseconds for the remote procedure call
  • $args - (optional) Variable length arguments which will PRECEDE the reply channel when constructing the message payload

Returns [Ok(_)] with the result on successful RPC or [Err(crate::RactorErr)] on failure

Example usage

use ractor::{call_t, Actor, ActorRef, RpcReplyPort, ActorProcessingErr};
struct TestActor;
enum MessageFormat {
    TestRpc(String, RpcReplyPort<String>),
}
#[cfg(feature = "cluster")]
impl ractor::Message for MessageFormat {}

#[cfg_attr(feature = "async-trait", ractor::async_trait)]
impl Actor for TestActor {
    type Msg = MessageFormat;
    type Arguments = ();
    type State = ();

    async fn pre_start(&self, _this_actor: ActorRef<Self::Msg>, _: ()) -> Result<Self::State, ActorProcessingErr> {
        Ok(())
    }

    async fn handle(
        &self,
        _this_actor: ActorRef<Self::Msg>,
        message: Self::Msg,
        _state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        match message {
            Self::Msg::TestRpc(arg, reply) => {
                // An error sending means no one is listening anymore (the receiver was dropped),
                // so we should shortcut the processing of this message probably!
                if !reply.is_closed() {
                    let _ = reply.send(arg);
                }
            }
        }
        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let (actor, handle) = Actor::spawn(None, TestActor, ()).await.unwrap();
    let result = call_t!(actor, MessageFormat::TestRpc, 50, "Something".to_string()).unwrap();
    assert_eq!(result, "Something".to_string());
    actor.stop(None);
    handle.await.unwrap();
}