Module context

Expand description

Context passed to and between services as input.

§State

rama supports two kinds of states:

  1. static state: this state can be a part of the service struct or captured by a closure
  2. dynamic state: these can be injected as Extensionss using methods such as Context::insert

Any state that is optional, and especially optional state injected by middleware, can be inserted using extensions. It is however important to try as much as possible to then also consume this state in an approach that deals gracefully with its absence. Good examples of this are header-related inputs. Headers might be set or not, and so absence of Extensionss that might be created as a result of these might reasonably not exist. It might of course still mean the app returns an error response when it is absent, but it should not unwrap/panic.

§Examples

§Example: Extensions

The Context can be extended with additional data using the Extensions type.

use rama_core::Context;

let mut ctx = Context::default();
ctx.insert(5i32);
assert_eq!(ctx.get::<i32>(), Some(&5i32));

Structs§

Context
Context passed to and between services as input.
Extensions
A type map of protocol extensions.
Parts
Component parts of Context.
RequestContextExt
Wrapper type that can be injected into the dynamic extensions of a “Response”, in order to preserve the Context’s extensions of the Request which was used to produce the Response.