pub struct ContextKey<T: 'static> { /* private fields */ }Expand description
Opaque, typed, unique context key. Created once per logical
slot (module-scope static via [create_context!] / the
deprecated [inject_key!], or runtime via ContextKey::new).
The T type parameter pins the value type so inject returns
Option<T> with no turbofish at the callsite.
PhantomData<fn() -> T> (contravariant in T) keeps the type
parameter in the signature without requiring T: 'static in
unrelated positions; T: 'static is enforced on use via
provide / inject.
Implementations§
Source§impl<T: 'static> ContextKey<T>
impl<T: 'static> ContextKey<T>
Sourcepub fn new(name: &'static str) -> Self
pub fn new(name: &'static str) -> Self
Mint a fresh unique key. Two calls — even with the same
name — yield keys that never collide. name is a debug
label only.
Sourcepub fn id(&self) -> u64
pub fn id(&self) -> u64
Unique process-local id; stable for the key’s lifetime. Used as the HashMap key inside the provides table.
Sourcepub fn provide(&self, value: T)where
T: Any + 'static,
pub fn provide(&self, value: T)where
T: Any + 'static,
Method-style provide. Equivalent to provide(&self, value)
but reads naturally on a typed key declared with
[create_context!] (RFC 056 §6.4):
ROOT.provide(this::<Self>());