pub struct RpcService<M: RawMutex, T, const S: usize> { /* private fields */ }Expand description
Dispatch closures for execution on a dedicated runner task.
Callers submit an FnOnce(&mut T) -> R via call.
The runner, started with run, executes closures one at a
time with exclusive &mut T access and sends results back. Each call
can return a different type.
Closures and return values are stored inline in a fixed-size slot of S bytes.
Both the closure and the return type must fit within S bytes and require no
more alignment than the slot. This is checked at compile time.
§Example
static FS: RpcService<CriticalSectionRawMutex, Filesystem, 64> =
RpcService::new();
// runner task
FS.run(&mut filesystem).await;
// caller task
let size = FS.call(|fs| fs.read_blocking(path).len()).await;Implementations§
Source§impl<M: RawMutex, T, const S: usize> RpcService<M, T, S>
impl<M: RawMutex, T, const S: usize> RpcService<M, T, S>
Sourcepub fn call<R, F>(&self, f: F) -> CallFuture<'_, M, T, R, F, S> ⓘ
pub fn call<R, F>(&self, f: F) -> CallFuture<'_, M, T, R, F, S> ⓘ
Submit a closure for execution on the runner.
Fails at compile time if F or R exceeds the slot capacity S.
Note: Under panic=unwind, if the closure panics, the returned future will not complete; it must be dropped for the service to recover.
§Cancellation
The returned future is cancel-safe: dropping it at any point is
sound and leaves the service in a usable state. When dropped, the
closure is either dropped without being called, or executed to
completion by the runner (with the return value discarded).
Execution requires that run() is eventually polled
and the service is not dropped.
Sourcepub fn try_call_immediate<F>(&self, f: F) -> bool
pub fn try_call_immediate<F>(&self, f: F) -> bool
Try to submit a fire-and-forget closure without blocking.
Returns true if the closure was submitted, false if the slot is busy.
A submitted closure will be executed during the next run()
call. If no run() is currently active, it remains pending until one
starts. There is no way to retrieve a return value.
Sourcepub async fn run(&self, state: &mut T) -> !
pub async fn run(&self, state: &mut T) -> !
Run the service loop, executing closures submitted via call
with exclusive &mut T access. This future must be polled (f.ex spawned as
a task) for callers to make progress.
§Panics
Panics if called while another run() is still active.
Sequential calls after a previous run() was dropped are fine.
§Cancellation
This future is cancel-safe. A subsequent call to run() will recover the
previous state and resume processing any in-flight call.