Macro ractor::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 PRECEED 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};
struct TestActor;
enum MessageFormat {
    TestRpc(String, RpcReplyPort<String>),
}

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

    type State = ();

    async fn pre_start(&self, _this_actor: ActorRef<Self>) -> Self::State {}

    async fn handle(
        &self,
        _this_actor: ActorRef<Self>,
        message: Self::Msg,
        _state: &mut Self::State,
    ) {
        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);
                }
            }
        }
    }
}

async fn test() {
    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())
}