Skip to main content

MethodTable

Trait MethodTable 

Source
pub trait MethodTable {
    type MethodId: Copy + Debug + PartialEq;
    type RequestParams<'a>;
    type NotificationParams<'a>;

    // Required methods
    fn lookup(&self, name: &str) -> Option<Self::MethodId>;
    fn decode_request_params<'a>(
        &self,
        method: Self::MethodId,
        params: Option<&'a RawValue>,
    ) -> Result<Self::RequestParams<'a>, RouteError<'a>>;
    fn decode_notification_params<'a>(
        &self,
        method: Self::MethodId,
        params: Option<&'a RawValue>,
    ) -> Result<Self::NotificationParams<'a>, RouteError<'a>>;
}
Expand description

The set of methods one protocol speaks, and how to decode each one’s params.

§Implementing this

A protocol module implements it once. lookup is the dispatch table and should be a match on the method name — the compiler turns a match on string literals into a length switch followed by a memcmp chain, which beats a hash table for a small, fixed vocabulary and allocates nothing.

The two decode_*_params methods are separate rather than one method with an is_request flag because a request and a notification of the same method legitimately have different params, and a flag would push that decision out to every call site.

use protocol_droid::application::jsonrpc::{MethodTable, RouteError};
use serde_json::value::RawValue;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MyMethod {
    Ping,
}

struct MyTable;

impl MethodTable for MyTable {
    type MethodId = MyMethod;
    type RequestParams<'a> = ();
    type NotificationParams<'a> = ();

    fn lookup(&self, name: &str) -> Option<MyMethod> {
        match name {
            "ping" => Some(MyMethod::Ping),
            _ => None,
        }
    }

    fn decode_request_params<'a>(
        &self,
        _method: MyMethod,
        _params: Option<&'a RawValue>,
    ) -> Result<(), RouteError<'a>> {
        Ok(())
    }

    fn decode_notification_params<'a>(
        &self,
        _method: MyMethod,
        _params: Option<&'a RawValue>,
    ) -> Result<(), RouteError<'a>> {
        Ok(())
    }
}

Required Associated Types§

Source

type MethodId: Copy + Debug + PartialEq

How this table names the methods it supports.

Source

type RequestParams<'a>

The decoded params of a request, which are method- and revision-specific.

A GAT rather than a plain associated type because the decoded value borrows the frame buffer. A protocol whose request params reference the id, or slice the raw JSON, would otherwise be forced to own a copy of data that is still alive.

Source

type NotificationParams<'a>

The decoded params of a notification.

Required Methods§

Source

fn lookup(&self, name: &str) -> Option<Self::MethodId>

The method a name denotes, or None if this protocol has no such method — which the router answers with -32601.

Source

fn decode_request_params<'a>( &self, method: Self::MethodId, params: Option<&'a RawValue>, ) -> Result<Self::RequestParams<'a>, RouteError<'a>>

Decodes a request’s params.

§Errors

RouteError::InvalidParams if params does not fit the method.

Source

fn decode_notification_params<'a>( &self, method: Self::MethodId, params: Option<&'a RawValue>, ) -> Result<Self::NotificationParams<'a>, RouteError<'a>>

Decodes a notification’s params.

§Errors

RouteError::InvalidParams if params does not fit the method.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§