Skip to main content

Command

Trait Command 

Source
pub trait Command:
    Send
    + Sync
    + 'static {
    type Request: DeserializeOwned + Send + 'static;
    type Response: Serialize + Send + 'static;

    const ID: &'static str;
    const DESCRIPTION: Option<&'static str> = None;

    // Required method
    fn handle(
        &self,
        request: Self::Request,
    ) -> impl Future<Output = Result<Self::Response, CommandError>> + Send;

    // Provided methods
    fn id(&self) -> &str { ... }
    fn description(&self) -> Option<&str> { ... }
    fn schema(&self) -> Option<CommandSchema> { ... }
}
Expand description

A typed command handler registered with a CommandRegistry.

Implementations are zero-cost at registration time: the registry wraps the typed handler in a dynamically-dispatched closure that decodes the incoming request JSON into Request, runs handle, and re-encodes the result.

§Compile-time vs runtime commands

  • Compile-time: const ID / const DESCRIPTION and the #[command] macro. The defaults for id and description read these constants.
  • Runtime: use DynCommand to supply an owned String id, description, and schema. DynCommand implements Command by overriding the instance-level methods.

Both paths register through the same register_command entry point.

Required Associated Constants§

Source

const ID: &'static str

Compile-time identifier for typed commands. Ignored when a command overrides id to return a runtime string (as DynCommand does).

Identifiers prefixed with _ are treated as private: they are never escalated to a router channel and never advertised to peers via list.commands.response.

Provided Associated Constants§

Source

const DESCRIPTION: Option<&'static str> = None

Optional compile-time description. Ignored when description is overridden.

Required Associated Types§

Source

type Request: DeserializeOwned + Send + 'static

Source

type Response: Serialize + Send + 'static

Required Methods§

Source

fn handle( &self, request: Self::Request, ) -> impl Future<Output = Result<Self::Response, CommandError>> + Send

Handles a single invocation.

Provided Methods§

Source

fn id(&self) -> &str

Instance-level identifier. Defaults to ID. DynCommand overrides this to return a runtime-owned id.

Source

fn description(&self) -> Option<&str>

Instance-level description. Defaults to DESCRIPTION.

Source

fn schema(&self) -> Option<CommandSchema>

Wire-level JSON Schema for this command. Defaults to None. The #[command] macro overrides this with a schema generated from Request and Response via schemars.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Req, Res, F, Fut> Command for DynCommand<Req, Res, F>
where Req: DeserializeOwned + Send + Sync + 'static, Res: Serialize + Send + Sync + 'static, F: Fn(Req) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Res, CommandError>> + Send + 'static,

Source§

const ID: &'static str = ""

Source§

type Request = Req

Source§

type Response = Res