Trait RequestContext

Source
pub trait RequestContext: AsRef<Self> + AsRef<NoContext> { }
Expand description

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 Guards 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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§