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

下面显示的是英文原文。

Bytes

A handler that reads the delivery's bytes as they arrived, and a reply that leaves as the bytes it holds, skip the codec.

A newtype over &'a [u8] deriving Deserialized is a view of the payload, borrowed from the broker's buffer. A handler over &Frame<'_> receives it with no copy and no decode step, and &[Frame<'_>] is the batch form. A bare &[u8] parameter does not compile: the newtype names what the bytes are. A newtype over Vec<u8> deriving Serialized publishes its bytes as they are, as a reply, an Out slot or a publish from the builder. The two lanes mix freely with codecs: a handler can decode its input with the scope codec and reply in bytes, or the other way round. A service whose messages are all on the lanes builds with no codec feature. A type that holds fields and writes its own format uses the same derives with #[wire(..)], as Protobuf does. In the manual form the impls the derives write are spelled out.

Example​

The example builds with no codec feature:

cargo test --example byte_lanes --no-default-features --features testing,macros,memory
/// The delivery's bytes, borrowed from the broker's buffer.
#[derive(Deserialized)]
struct Frame<'a>(&'a [u8]);

/// Bytes that leave as they are.
#[derive(Outgoing, Serialized)]
struct Export(Vec<u8>);

#[subscriber("frames", reply("exports"))]
async fn reverse(frame: &Frame<'_>) -> Export {
tracing::info!(len = frame.0.len(), "reversed");
let mut bytes = frame.0.to_vec();
bytes.reverse();
Export(bytes)
}

fn app() -> impl App {
RustStream::new(AppInfo::new("frames", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(reverse);
})
}