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) on purpose: 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.
§Examples
use ruststream::ContextField;
struct Delivery {
sequence: u64,
}
#[derive(Clone, Copy, Default)]
struct Sequence;
impl ContextField for Sequence {
type Context = Delivery;
type Value = u64;
fn read(self, src: &Delivery) -> u64 {
src.sequence
}
}
let delivery = Delivery { sequence: 7 };
assert_eq!(Sequence.read(&delivery), 7);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".