Skip to main content

Dispatch

Enum Dispatch 

Source
pub enum Dispatch<Req: JsonRpcRequest = UntypedMessage, Notif: JsonRpcMessage = UntypedMessage> {
    Request(Req, Responder<Req::Response>),
    Notification(Notif),
    Response(Result<Req::Response, Error>, ResponseRouter<Req::Response>),
}
Expand description

An enum capturing an in-flight request or notification. In the case of a request, also includes the context used to respond to the request.

Type parameters allow specifying the concrete request and notification types. By default, both are UntypedMessage for dynamic dispatch. The request context’s response type matches the request’s response type.

Variants§

§

Request(Req, Responder<Req::Response>)

Incoming request and the context where the response should be sent.

§

Notification(Notif)

Incoming notification.

§

Response(Result<Req::Response, Error>, ResponseRouter<Req::Response>)

Incoming response to a request we sent.

The first field is the response result (success or error from the remote). The second field is the context for forwarding the response to its destination (typically a waiting oneshot channel).

Implementations§

Source§

impl<Req: JsonRpcRequest, Notif: JsonRpcMessage> Dispatch<Req, Notif>

Source

pub fn map<Req1, Notif1>( self, map_request: impl FnOnce(Req, Responder<Req::Response>) -> (Req1, Responder<Req1::Response>), map_notification: impl FnOnce(Notif) -> Notif1, ) -> Dispatch<Req1, Notif1>
where Req1: JsonRpcRequest<Response = Req::Response>, Notif1: JsonRpcMessage,

Map the request and notification types to new types.

Note: Response variants are passed through unchanged since they don’t contain a parseable message payload.

Source

pub fn respond_with_error<R: Role>( self, error: Error, cx: ConnectionTo<R>, ) -> Result<(), Error>

Respond to the message with an error.

If this message is a request, this error becomes the reply to the request.

If this message is a notification, the error is sent as a notification.

If this message is a response, the error is forwarded to the waiting handler.

Source

pub fn erase_to_json(self) -> Result<Dispatch, Error>

Convert to a Responder that expects a JSON value and which checks (dynamically) that the JSON value it receives can be converted to T.

Note: Response variants cannot be erased since their payload is already parsed. This returns an error for Response variants.

Source

pub fn to_untyped_message(&self) -> Result<UntypedMessage, Error>

Convert the message in self to an untyped message.

Note: Response variants don’t have an untyped message representation. This returns an error for Response variants.

Source

pub fn into_untyped_dispatch(self) -> Result<Dispatch, Error>

Convert self to an untyped message context.

Note: Response variants cannot be converted. This returns an error for Response variants.

Source

pub fn id(&self) -> Option<Value>

Returns the request ID if this is a request or response, None if notification.

Source

pub fn method(&self) -> &str

Returns the method of the message.

For requests and notifications, this is the method from the message payload. For responses, this is the method of the original request.

Source§

impl Dispatch

Source

pub fn has_field(&self, field_name: &str) -> bool

True if this message has a field with the given name.

Returns false for Response variants.

Source

pub fn into_notification<N: JsonRpcNotification>( self, ) -> Result<Result<N, Dispatch>, Error>

Try to parse this as a notification of the given type.

§Returns
  • Ok(Ok(typed)) if this is a request/notification of the given types
  • Ok(Err(self)) if not
  • Err if has the correct method for the given types but parsing fails
Source

pub fn into_request<Req: JsonRpcRequest>( self, ) -> Result<Result<(Req, Responder<Req::Response>), Dispatch>, Error>

Try to parse this as a request of the given type.

§Returns
  • Ok(Ok(typed)) if this is a request/notification of the given types
  • Ok(Err(self)) if not
  • Err if has the correct method for the given types but parsing fails
Source§

impl<M: JsonRpcRequest + JsonRpcNotification> Dispatch<M, M>

Source

pub fn message(&self) -> Option<&M>

Returns the message payload for requests and notifications.

Returns None for Response variants since they don’t contain a message payload.

Trait Implementations§

Source§

impl<Req: Debug + JsonRpcRequest, Notif: Debug + JsonRpcMessage> Debug for Dispatch<Req, Notif>
where Req::Response: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Req, Notif> Freeze for Dispatch<Req, Notif>
where Req: Freeze, Notif: Freeze, <Req as JsonRpcRequest>::Response: Freeze,

§

impl<Req = UntypedMessage, Notif = UntypedMessage> !RefUnwindSafe for Dispatch<Req, Notif>

§

impl<Req, Notif> Send for Dispatch<Req, Notif>

§

impl<Req = UntypedMessage, Notif = UntypedMessage> !Sync for Dispatch<Req, Notif>

§

impl<Req, Notif> Unpin for Dispatch<Req, Notif>
where Req: Unpin, Notif: Unpin, <Req as JsonRpcRequest>::Response: Unpin,

§

impl<Req, Notif> UnsafeUnpin for Dispatch<Req, Notif>

§

impl<Req = UntypedMessage, Notif = UntypedMessage> !UnwindSafe for Dispatch<Req, Notif>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more