Skip to main content

MethodHandler

Trait MethodHandler 

Source
pub trait MethodHandler: Send + Sync {
    // Required method
    fn call(
        &self,
        params: Value,
        ctx: ConnectionContext,
    ) -> Pin<Box<dyn Future<Output = Result<Value, RpcError>> + Send>>;
}
Expand description

Object-safe async handler for one JSON-RPC method.

Why: Router stores handlers as trait objects (Arc<dyn MethodHandler>) so methods of different concrete closure/fn types can share one registry. Async fns are not directly object-safe, hence the call -> boxed-future indirection. What: implemented for Self by the blanket impl below whenever Self is Fn(Value, ConnectionContext) -> Fut with Fut: Future<Output = Result<Value, RpcError>> — callers never need to implement this trait by hand. Test: exercised indirectly by every router::tests case and by crate::serve::methods::tests.

Required Methods§

Source

fn call( &self, params: Value, ctx: ConnectionContext, ) -> Pin<Box<dyn Future<Output = Result<Value, RpcError>> + Send>>

Invoke the handler with the request’s params (defaulted to Null when the wire request omitted them) and the calling connection’s context.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<F, Fut> MethodHandler for F
where F: Fn(Value, ConnectionContext) -> Fut + Send + Sync, Fut: Future<Output = Result<Value, RpcError>> + Send + 'static,