Skip to main content

Command

Trait Command 

Source
pub trait Command:
    Serialize
    + DeserializeOwned
    + Send
    + Sync {
    type Reply: Serialize + DeserializeOwned + Send;

    const SUBJECT: &'static str;
    const SUBJECT_FILTER: &'static str;
    const DELIVERY: Delivery = Delivery::AtLeastOnce;

    // Provided methods
    fn subject(&self) -> Cow<'static, str> { ... }
    fn into_payload(self) -> Result<Vec<u8>, CommandError> { ... }
    fn from_payload(subject: &str, payload: &[u8]) -> Result<Self, CommandError> { ... }
}
Expand description

Typed command endpoint for request-reply over a message bus.

Each struct represents one command endpoint.

Required Associated Constants§

Source

const SUBJECT: &'static str

NATS subject pattern, e.g. "rooms.*.create".

Source

const SUBJECT_FILTER: &'static str

JetStream stream filter (auto-inferred by derive), e.g. "rooms.>".

Provided Associated Constants§

Source

const DELIVERY: Delivery = Delivery::AtLeastOnce

How often this command may reach its handler.

Defaults to Delivery::AtLeastOnce, which suits any handler that can run twice without harm.

Required Associated Types§

Source

type Reply: Serialize + DeserializeOwned + Send

Wire response type — Requester::request returns Self::Reply.

Provided Methods§

Source

fn subject(&self) -> Cow<'static, str>

The concrete subject this instance is sent to.

Encodes any wildcard fields of Command::SUBJECT. Defaults to the static SUBJECT for commands without wildcard fields.

Source

fn into_payload(self) -> Result<Vec<u8>, CommandError>

Serialize this command into the transport payload.

The wire format is owned here, not by the transport, so the encoding can change (e.g. JSON to protobuf) without the bus implementation knowing. The full struct is encoded, including any fields also carried in the subject (so the two copies cannot drift); the reply address travels out-of-band (e.g. a NATS header), not in these bytes.

Source

fn from_payload(subject: &str, payload: &[u8]) -> Result<Self, CommandError>

Reconstruct Self from a received command’s payload.

The full struct is serialized on the wire (including any fields also encoded in the subject, so the two copies cannot drift), and the reply address is carried out-of-band by the transport (e.g. a NATS header). The payload therefore suffices to recover every field; subject is retained for transports that wish to validate routing.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§