yog_event/call.rs
1//! Calling the bus from this binary: the one way it's reached is set once
2//! (by `yog-api` in a mod, by the runtime in itself), and every action's
3//! function and [`call`] go through it.
4
5use yog_sync::OnceLock;
6
7use crate::{Message, Service};
8
9/// Sends `payload` to `address`'s provider: its reply's bytes, `None` when
10/// no one answers.
11pub type Caller = fn(address: crate::Address, payload: &[u8]) -> Option<Vec<u8>>;
12
13static CALLER: OnceLock<Caller> = OnceLock::new();
14
15/// How this binary reaches the bus; the first one set stays.
16pub fn set_caller(caller: Caller) {
17 let _ = CALLER.set(caller);
18}
19
20/// Asks `request`'s provider: its reply, `None` when no one answers (or the
21/// bus isn't reached yet).
22pub fn call<S: Service>(request: &S) -> Option<S::Reply> {
23 let caller = CALLER.get()?;
24 let reply = request.with_bytes(|payload| caller(request.address(), payload))?;
25 S::Reply::from_bytes(&reply)
26}