Skip to main content

request_context

Function request_context 

Source
pub fn request_context<T>(cx: &Cx) -> &T
where T: Any + Send + Sync,
Expand description

Returns a reference to the request context value of type T registered on the current request’s Cx.

The lookup is keyed by T’s TypeId, so each type may have at most one registered value per request. Request context lives only for the duration of the request that owns it; once the request completes, every value is dropped.

§Panics

Panics if no value of type T has been registered on this request’s Cx.

§Examples

use topcoat::context::{Cx, request_context};

struct RequestId(String);

async fn current_request_id(cx: &Cx) -> &str {
    let id: &RequestId = request_context(cx);
    &id.0
}