Эта страница ещё не переведена
Ниже показан английский оригинал.
insert_typed
HeaderMap::insert_typed writes a header contract into a header map, the way a publish transform stamps every message of a slot.
A field becomes one header named after it, its value written as a string.
A None field writes nothing, and a header of the same name is overwritten.
A field that holds a nested struct, a sequence or a map is an error.
The example's transform is a publish transform on an Out slot, so every message the slot sends carries the contract.
Example
- Macros
- Manual
/// Where the service runs, as a contract stamped on everything the slot sends.
#[derive(Clone, Serialize)]
struct Origin {
region: &'static str,
canary: bool,
}
struct Stamp(Origin);
impl<Options> PublishTransform<ForSlot, Options> for Stamp {
type Destination = Reads;
fn apply(
&self,
out: &mut Outgoing<'_>,
_options: &mut Option<Options>,
_cx: &SlotContext<'_>,
) {
if let Err(error) = out.headers_mut().insert_typed(&self.0) {
tracing::warn!(%error, "origin headers left out");
}
}
}
fn app() -> impl App {
let origin = Origin {
region: "eu-west",
canary: false,
};
RustStream::new(AppInfo::new("transcoder", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(convert)
.out(Events, Publish)
.transform(Stamp(origin))
.build();
})
}
/// Where the service runs, as a contract stamped on everything the slot sends.
#[derive(Clone, Serialize)]
struct Origin {
region: &'static str,
canary: bool,
}
struct Stamp(Origin);
impl<Options> PublishTransform<ForSlot, Options> for Stamp {
type Destination = Reads;
fn apply(
&self,
out: &mut Outgoing<'_>,
_options: &mut Option<Options>,
_cx: &SlotContext<'_>,
) {
if let Err(error) = out.headers_mut().insert_typed(&self.0) {
tracing::warn!(%error, "origin headers left out");
}
}
}
fn app() -> impl App {
let origin = Origin {
region: "eu-west",
canary: false,
};
RustStream::new(AppInfo::new("transcoder", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(subscriber("chunks", Convert).build())
.out(Events, Publish)
.transform(Stamp(origin))
.build();
})
}