Skip to main content

command

Attribute Macro command 

Source
#[command]
Expand description

Marks a function as a Telepath RPC command.

§What it generates

For every annotated function the macro emits five additional items:

  1. fn __telepath_shim_<name>(input: &[u8], output: &mut [u8], resources: &ResourceRegistry) -> Result<usize, DispatchError> — deserializes input via postcard, resolves #[resource]-annotated arguments from resources, calls the original function, and serializes the result into output.
  2. fn __telepath_args_schema_<name>(out: &mut [u8]) -> Result<usize, ()> — writes a postcard-encoded postcard_schema::schema::NamedType for the argument tuple into out and returns the byte count.
  3. fn __telepath_ret_schema_<name>(out: &mut [u8]) -> Result<usize, ()> — same for the return type.
  4. pub const __TELEPATH_CMD_<NAME>: CommandMetadata — a CommandMetadata const whose id is derived deterministically from the function’s signature via derive_cmd_id at build time.
  5. #[linkme] static __TELEPATH_REG_<NAME> — registers the metadata in [telepath_server::TELEPATH_COMMANDS] at link time.

The original function body is preserved unchanged so it remains directly callable.

§Requirements on the calling crate

The calling crate must declare the following direct dependencies:

  • telepath-server — provides CommandMetadata, DispatchError, and re-exports postcard_schema and linkme for use in generated code.
  • postcard — used in the generated shim for (de)serialization

All argument types and the return type must implement postcard_schema::Schema. Built-in primitives (u8, u32, (), standard tuples, etc.) already implement it. For user-defined types, add #[derive(postcard_schema::Schema)].

§Restrictions

The macro rejects functions that are:

  • async fn (RPC dispatch is synchronous)
  • unsafe fn
  • Generic (<T> / where clauses)
  • Methods (self receiver)
  • Functions with reference arguments or reference return types
  • Functions with pattern-destructured arguments

§Example

use telepath_server::{command, CommandMetadata};

#[command]
fn ping() -> u32 {
    0xDEAD_BEEF
}

static COMMANDS: [CommandMetadata; 1] = [__TELEPATH_CMD_PING];