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

下面显示的是英文原文。

Naming

A naming transform sets the destination of each message, on a position where nothing else declared it: a reply to the inbox a request named.

A transform that declares type Destination = Names may call set_name on the outgoing message. A position offers that right only where nothing declared the destination: a reply type with no #[outgoing(name = ..)], or a slot whose whole #[publishes(..)] list leaves it open. It offers it once, so a second naming transform on one position does not compile, and neither does one on a declared reply or on a batch reply. DefaultSlot and a slot that lists a declared destination never offer it. The name at the mount site stays the declared fallback: the generated document reports it, and a delivery the transform leaves alone is answered there. A name read off the delivery costs no copy: get_shared hands over the header's own buffer, and Str checks it is UTF-8. The reply side of RequestReply is the usual case. A naming transform is a publish transform in every other way: it has no macro form, and only the mounting differs.

Example​

/// Answers on the inbox the question named in its `reply-to` header. The header's bytes become
/// the destination as they are: one UTF-8 check, no copy.
struct ToInbox;

impl<C, Options> PublishTransform<ForReply<C>, Options> for ToInbox {
type Destination = Names;

fn apply(
&self,
out: &mut Outgoing<'_>,
_options: &mut Option<Options>,
cx: &PublishContext<'_, C>,
) {
if let Some(inbox) = cx.headers().get_shared("reply-to")
&& let Ok(name) = Str::try_from(inbox)
{
out.set_name(name);
}
}
}
/// `"answers"` is the fallback, and what the generated document reports.
#[subscriber("questions", reply("answers"))]
async fn answer(question: &Question) -> Answer {
Answer { id: question.id }
}

fn app() -> impl App {
RustStream::new(AppInfo::new("answers", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(answer).out_reply(Publish).transform(ToInbox);
})
}