跳到主要内容
本页尚未翻译

下面显示的是英文原文。

Slice codecs

A SliceCodec encodes into a slice the runtime lends and decodes from a slice, so it needs no allocator. It is the codec of the embedded runtime built without alloc, in place of Codec, which hands out a growing buffer.

PostcardCodec is on with the postcard feature. It is a compact binary format, application/octet-stream, and both ends need the same Rust types. JsonCoreCodec is on with the json-core feature. It writes the JSON that serde_json writes, application/json, over serde-json-core, and its decoder does not unescape strings, so a message type holds numbers, booleans, arrays and fixed-capacity strings. A value that does not fit the buffer it is encoded into is CodecFailure::BufferFull, and the publish fails like any other: the handler's .error hears of it.

A mount that names no codec uses the default: JsonCoreCodec when json-core is on, PostcardCodec otherwise. Enabling json-core beside postcard therefore makes JSON the default. A mount names its codec with b.with_codec(PostcardCodec) for the handlers of a scope, .out_reply(policy).codec(PostcardCodec) for a reply, and .out(marker, policy).codec(PostcardCodec) for an Out slot. A reply is encoded into a buffer that .encode_capacity::<N>() sizes, 256 bytes unless the mount names more. A message that carries its own bytes needs no codec, with the byte lanes. A codec of your own implements SliceCodec.

Example​

The mount of a device service built with postcard, whose default codec decodes the readings and encodes the replies:

fn app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(on_report)
.include(on_reading)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
})
}