Skip to main content

RpcService

Struct RpcService 

Source
pub struct RpcService<M, Req, Resp>
where M: RawMutex,
{ /* private fields */ }
Expand description

In-memory request/response synchronization for async tasks.

This type is not a wire protocol. It connects a client async task that calls RpcService::request with a server task that calls RpcService::serve, using a embassy_sync::blocking_mutex::Mutex and async wakers. It is suitable for no_std use when paired with a mutex implementation appropriate for your platform (RawMutex).

§Concurrency

  • Single in-flight RPC: Internal state holds at most one queued request and one response slot. Design for one active request/response pair at a time.
  • Multiple clients: Several tasks may call request; additional callers wait until the current RPC completes (including delivery of RequestDroppedError).
  • Client cancellation: If the async task awaiting Self::request is dropped (executor cancellation), the service releases the client slot once the in-flight RPC is fully finished (including after the server responds or drops ServedRequest). Dropping the client future after the request was queued but before the server takes it removes the queued request and frees the slot immediately.

§Type parameters

  • M: Mutex raw type (RawMutex), e.g. CriticalSectionRawMutex on many MCUs.
  • Req / Resp: Your message types. Req may borrow from the client for the duration of the call; then the RpcService must not outlive those borrows (often modeled with a stack-scoped service—see the crate README).

Implementations§

Source§

impl<M, Req, Resp> RpcService<M, Req, Resp>
where M: RawMutex,

Source

pub const fn new() -> Self

Creates an empty service. Safe to call in const contexts (e.g. static initializer).

Source

pub async fn request(&self, req: Req) -> Result<Resp, RequestDroppedError>

Sends req and waits until the server responds or drops the ServedRequest.

If another client is already in an RPC, this call waits until that RPC fully completes (including waking the next waiter for the client slot) before sending req.

§Errors

Returns Err(RequestDroppedError) if the server drops ServedRequest without calling ServedRequest::respond.

§Cancellation

If this future is dropped while waiting for a response, the queued request is removed if the server has not taken it yet; otherwise the server continues with ServedRequest and the response is discarded when the server completes. The client slot becomes available again after that completion (or immediately when the queued request is dropped).

Source

pub async fn serve(&self) -> (Req, ServedRequest<'_, M, Req, Resp>)

Waits until a client submits a request via Self::request, then returns the request value together with a ServedRequest used to complete or abandon the RPC.

The server must eventually call ServedRequest::respond on the handle or drop it; dropping notifies the client with RequestDroppedError.

Trait Implementations§

Source§

impl<M, Req, Resp> Default for RpcService<M, Req, Resp>
where M: RawMutex,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<M, Req, Resp> !Freeze for RpcService<M, Req, Resp>

§

impl<M, Req, Resp> !RefUnwindSafe for RpcService<M, Req, Resp>

§

impl<M, Req, Resp> Send for RpcService<M, Req, Resp>
where M: Send, Req: Send, Resp: Send,

§

impl<M, Req, Resp> Sync for RpcService<M, Req, Resp>
where M: Sync, Req: Send, Resp: Send,

§

impl<M, Req, Resp> Unpin for RpcService<M, Req, Resp>
where M: Unpin, Req: Unpin, Resp: Unpin,

§

impl<M, Req, Resp> UnsafeUnpin for RpcService<M, Req, Resp>
where M: UnsafeUnpin, Req: UnsafeUnpin, Resp: UnsafeUnpin,

§

impl<M, Req, Resp> UnwindSafe for RpcService<M, Req, Resp>
where M: UnwindSafe, Req: UnwindSafe, Resp: 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.