[−][src]Trait hyperdrive::RequestContext
Trait for context types passed to FromRequest
, FromBody
and
Guard
.
#[derive(RequestContext)]
This trait can be derived automatically. This will also implement
AsRef<Self>
and AsRef<NoContext>
.
On structs, fields can also be annotated using #[as_ref]
, which generates
an additional implementation of AsRef
for that field (note that all
#[as_ref]
fields must have distinct types). This will automatically use
the field's type as a context when required by a FromRequest
impl.
Examples
Create your own context that allows running database queries in Guard
s
and elsewhere:
#[derive(RequestContext)] struct MyContext { db: ConnectionPool, }
Create a context that contains the above context and additional data:
#[derive(RequestContext)] struct BigContext { #[as_ref] inner: MyContext, logger: Logger, }
This context can be used in the same places where MyContext
is accepted,
but provides additional data that may be used only by a few Guard
,
FromRequest
or FromBody
implementations.