Skip to main content

rama_http/service/web/endpoint/extract/
state.rs

1use std::ops::{Deref, DerefMut};
2
3use rama_core::conversion::FromRef;
4use rama_http_types::request::Parts;
5
6use crate::service::web::extract::FromPartsStateRefPair;
7
8#[derive(Debug, Default, Clone, Copy)]
9/// Extractor for static State provided by the caller of [`IntoEndpointServiceWithState`]
10///
11/// `S` can be the exact provided `State` or any type that implements [`FromRef<State>`]
12///
13/// [`IntoEndpointServiceWithState`]: crate::service::web::IntoEndpointServiceWithState
14pub struct State<S>(pub S);
15
16impl<OuterState, InnerState> FromPartsStateRefPair<OuterState> for State<InnerState>
17where
18    OuterState: Send + Sync,
19    InnerState: FromRef<OuterState> + Send + Sync + 'static,
20{
21    type Rejection = std::convert::Infallible;
22
23    async fn from_parts_state_ref_pair(
24        _parts: &Parts,
25        state: &OuterState,
26    ) -> Result<Self, Self::Rejection> {
27        let inner_state = InnerState::from_ref(state);
28        Ok(Self(inner_state))
29    }
30}
31
32impl<S> Deref for State<S> {
33    type Target = S;
34
35    fn deref(&self) -> &Self::Target {
36        &self.0
37    }
38}
39
40impl<S> DerefMut for State<S> {
41    fn deref_mut(&mut self) -> &mut Self::Target {
42        &mut self.0
43    }
44}