catalyzer/request/
state.rs

1use axum::extract::{FromRequestParts, FromRef};
2use axum::http::request::Parts as RequestParts;
3use core::convert::Infallible;
4use std::future::Future;
5
6/// An extractor that extracts the state of the application.
7#[repr(transparent)]
8#[derive(Debug, Default, Clone, Copy)]
9pub struct State<S>(S);
10
11impl<OuterState, InnerState> FromRequestParts<OuterState> for State<InnerState>
12where
13    InnerState: FromRef<OuterState>,
14    OuterState: Send + Sync,
15{
16    type Rejection = Infallible;
17    fn from_request_parts<'a: 'c, 'b: 'c, 'c>(
18        _: &'a mut RequestParts,
19        state: &'b OuterState,
20    ) -> core::pin::Pin<Box<
21        dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'c,
22    >> where Self: 'c {
23        Box::pin(async move {
24            Ok(Self(InnerState::from_ref(state)))
25        })
26    }
27}
28
29impl<S> core::ops::Deref for State<S> {
30    type Target = S;
31    #[inline]
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl<S> core::ops::DerefMut for State<S> {
38    #[inline]
39    fn deref_mut(&mut self) -> &mut Self::Target {
40        &mut self.0
41    }
42}