roa_core/
state.rs

1/// The `State` trait, should be replace with trait alias.
2/// The `App::state` will be cloned when a request inbounds.
3///
4/// `State` is designed to share data or handler between middlewares.
5///
6/// ### Example
7/// ```rust
8/// use roa_core::{App, Context, Next, Result};
9/// use roa_core::http::StatusCode;
10///
11/// #[derive(Clone)]
12/// struct State {
13///     id: u64,
14/// }
15///
16/// let app = App::state(State { id: 0 }).gate(gate).end(end);
17/// async fn gate(ctx: &mut Context<State>, next: Next<'_>) -> Result {
18///     ctx.id = 1;
19///     next.await
20/// }
21///
22/// async fn end(ctx: &mut Context<State>) -> Result {
23///     let id = ctx.id;
24///     assert_eq!(1, id);
25///     Ok(())
26/// }
27/// ```
28pub trait State: 'static + Clone + Send + Sync + Sized {}
29
30impl<T: 'static + Clone + Send + Sync + Sized> State for T {}