pub trait ContextField: Default {
type Context;
type Value: Send + 'static;
// Required method
fn read(self, src: &Self::Context) -> Self::Value;
}Expand description
A Field-style key that also names its context type and yields an owned value.
It powers the Ctx extractor: because the key carries its context
as an associated type, a handler taking Ctx(value): Ctx<Key> needs no &mut Context
parameter at all - the #[subscriber] macro projects the subscription’s context type from
the first Ctx key it sees. The value is owned ('static): extractor values bind before the
handler body runs, so borrowing from the context is not an option. Borrowed
keys keep working through Field and ctx.context(KEY).
Broker crates implement it next to Field on the same zero-sized keys; a key typically
implements both, so it works as a context read and as an extractor.
A broker with native delivery metadata (a partition, an offset, a stream sequence) exposes it
as a #[non_exhaustive] context struct its subscriber names, plus unit-struct keys like this
one; the delivery path carries no type map and no heap allocation. A value that is not Copy
(a Pulsar message id, a Kinesis shard and sequence) is read by borrowing through
Field::Value<'a>, so a body reading it with ctx.context(..) copies nothing,
and this key clones what the borrowing one returns. A broker with no per-delivery fields uses
(). Subscription-scoped values for batch bodies go in a second struct built through
BuildBatchContext. The in-memory broker’s MemoryContext and MemoryBatchContext, which
publish the Position and SeekHandle keys, are the model.
§Examples
use ruststream::ContextField;
use ruststream::prelude::*;
use serde::Deserialize;
/// A broker's per-delivery context.
struct Delivery {
sequence: u64,
}
/// The broker's key for the sequence number a delivery carries.
#[derive(Clone, Copy, Default)]
struct Sequence;
impl ContextField for Sequence {
type Context = Delivery;
type Value = u64;
fn read(self, src: &Delivery) -> u64 {
src.sequence
}
}
#[derive(Deserialize)]
struct Order {
id: u64,
}
#[subscriber("orders")]
async fn audit(order: &Order, Ctx(sequence): Ctx<Sequence>) -> HandlerOutcome {
tracing::info!(order.id, sequence, "audited");
HandlerOutcome::ack()
}Required Associated Types§
Required Methods§
Sourcefn read(self, src: &Self::Context) -> Self::Value
fn read(self, src: &Self::Context) -> Self::Value
Reads this key’s field out of src. Named apart from Field::get so a key
implementing both traits stays unambiguous to call.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".