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.
- Inputs:
- tx: Sender<CmdFrom> = Sender that the service uses to send commands to the main thread
- name = name of a CmdFrom variant that has a Sender
as the last parameter - params = any other parameters for the CmdFrom variant
- Outputs:
- Result<T, mpsc::RecvError> = the response received (or not) from the main thread