pub struct Cx { /* private fields */ }Expand description
The request context.
Add cx: &Cx to a handler or component to access context. Topcoat supplies
it automatically. Read shared application values with app_context and
values scoped to the request with request_context.
Clones share the request’s state. Use with or
with_many to add context values in a child scope
without changing the parent. Move an owned clone into work that outlives
the handler, such as a streaming response.
Implementations§
Source§impl Cx
impl Cx
Sourcepub fn new(app_context: Arc<AppContext>) -> Cx
pub fn new(app_context: Arc<AppContext>) -> Cx
Creates the context for one request over the shared app context, with an empty request context.
Sourcepub fn keyed(&self, key: impl IdentityKey) -> Cx
pub fn keyed(&self, key: impl IdentityKey) -> Cx
Returns a child context with a key derived from the parent’s key,
this call’s source location, and key.
Use () to distinguish separate call locations, or an item key to
distinguish repeated calls at one location:
let first = cx.keyed(());
let second = cx.keyed(());
for id in [1, 2, 3] {
let child = cx.keyed(id);
}The same inputs produce the same key. Moving the call in source changes it. The child shares the parent’s request state and context values.
Sourcepub fn with<T>(&self, value: T) -> Cx
pub fn with<T>(&self, value: T) -> Cx
Returns a child handle whose request context also holds value.
The child inherits other context values and shares the request’s state.
If this type is already registered, the child sees value while the
parent still sees the original.
Sourcepub fn with_many<V>(&self, values: V) -> Cxwhere
V: ContextValues,
pub fn with_many<V>(&self, values: V) -> Cxwhere
V: ContextValues,
Returns a child handle whose request context also holds every value in
values, a tuple of context values or a RequestContext.
Behaves like chained with calls, but builds the child’s
request context in one step.