nri::rpc! [-] [+] [src]

macro_rules! rpc {
    ($tx:expr, CmdFrom::$name:ident, $($param:expr),*) => {{
        let (msg_tx, msg_rx) = ::std::sync::mpsc::channel();
        $tx.send($crate::comms::CmdFrom::$name($($param),*, msg_tx));
        msg_rx.recv()
    }}
}

Convenience macro for making an "RPC" call from a service up to the main thread. This is done by generating a nonce channel, stuffing the sending end into a message that gets sent to the main thread, and then waiting for the main thread to send back a reply.

This is a macro instead of a function because you need to pass in the name of a CmdFrom variant without constructing it (because a Sender is needed to construct it, but the macro is creating the channel for you). Possibly a better design would be structs and a trait/generic.