embassy-sync

Crates

git

Versions

default

Flavors

RpcService

Struct RpcService 

Source
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>

Source

pub const fn new() -> Self

Create a new RpcService.

Source

pub fn call<R, F>(&self, f: F) -> CallFuture<'_, M, T, R, F, S>
where F: FnOnce(&mut T) -> R + Send + 'static, R: Send + 'static,

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.

Source

pub fn try_call_immediate<F>(&self, f: F) -> bool
where F: FnOnce(&mut T) + Send + 'static,

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.

Source

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.

Trait Implementations§

Source§

impl<M: RawMutex, T, const S: usize> Sync for RpcService<M, T, S>
where Mutex<M, RefCell<SlotState>>: Sync, Mutex<M, Cell<RunnerState>>: Sync, Signal<M, unsafe fn(&Storage<S>, &mut T)>: Sync, Signal<M, ()>: Sync,

Auto Trait Implementations§

§

impl<M, T, const S: usize> !Freeze for RpcService<M, T, S>

§

impl<M, T, const S: usize> !RefUnwindSafe for RpcService<M, T, S>

§

impl<M, T, const S: usize> Send for RpcService<M, T, S>
where M: Send,

§

impl<M, T, const S: usize> Unpin for RpcService<M, T, S>
where M: Unpin,

§

impl<M, T, const S: usize> UnwindSafe for RpcService<M, T, S>
where M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.