Перейти к основному содержимому
Эта страница ещё не переведена

Ниже показан английский оригинал.

Protobuf

A message generated by prost carries its own bytes with #[wire(prost)]: no serde, no codec feature, encoded once into the buffer that leaves.

The message derives Serialized and Deserialized next to prost::Message, and #[wire(prost)] tells both derives to call prost's encoder and decoder. The type then rides the byte lanes: a handler reads &Order, decoded by prost before the call, and a publish or a reply writes the value with prost into the publish path's own buffer. No codec is resolved for the type anywhere, so the service builds with no codec feature. In a real service prost-build generates the type: its message_attribute setting adds the derives and the #[wire(prost)] line to every message it emits. The example writes the generated struct out by hand. Another format uses the general form, #[wire(encode = <path>, decode = <path>)], which names the format's own writer and reader: Cap'n Proto, FlatBuffers and a hand-rolled frame fit it. A payload that prost cannot decode is a decode failure, settled by the subscriber's failure policy. In the manual form the impls the derives write are spelled out, and wire_bytes and from_payload call prost directly.

Example​

The example builds with no codec feature:

cargo test --example protobuf --no-default-features --features testing,macros,memory
#[derive(Clone, PartialEq, prost::Message, Outgoing, Serialized, Deserialized)]
#[outgoing(name = "orders")]
#[wire(prost)]
struct Order {
#[prost(uint64, tag = "1")]
id: u64,
#[prost(string, tag = "2")]
sku: String,
}

The handler reads the message type, and the publish names no codec.

fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(handle);
// The outgoing half of the same declaration: the value encodes itself on the way out
// too, so this publish names no codec either.
b.after_startup(Publish, async move |publisher| {
publisher
.message(&Order {
id: 7,
sku: "widget".to_owned(),
})
.publish()
.await
});
})
}