Skip to main content

ntex_service/
map_state.rs

1use crate::{Ctx, IntoService, IntoServiceFactory, Service, ServiceFactory};
2
3/// Wraps a service with a fixed state value.
4///
5/// The wrapped service uses `st` instead of the state from its outer pipeline.
6/// This changes the state passed to operations, not the service's readiness
7/// identity. A shared binding keeps one gate across mapped state values.
8pub fn map_state<S, St, Req>(st: St, s: impl IntoService<S, St, Req>) -> MapState<S, St>
9where
10    S: Service<St, Req>,
11{
12    MapState {
13        st,
14        s: s.into_service(),
15    }
16}
17
18/// Wraps a service factory with a fixed state value.
19///
20/// The fixed state is used both to create services and to process their calls.
21pub fn map_state_factory<Sf, St, Req>(
22    st: St,
23    sf: impl IntoServiceFactory<Sf, St, Req>,
24) -> MapStateFactory<Sf, St>
25where
26    Sf: ServiceFactory<St, Req>,
27    St: Clone,
28{
29    MapStateFactory {
30        st,
31        sf: sf.into_factory(),
32    }
33}
34
35#[derive(Clone, Debug)]
36/// A service that substitutes fixed state for the outer pipeline state.
37///
38/// Readiness and lifecycle subscriptions retain the wrapped service's scope.
39/// Cloning follows the wrapped service: a [`crate::ReadyBinding`] clone gets
40/// fresh readiness, while an explicitly shared handle keeps its existing gate.
41pub struct MapState<S, St> {
42    s: S,
43    st: St,
44}
45
46impl<OtSt, S, St, Req> Service<OtSt, Req> for MapState<S, St>
47where
48    S: Service<St, Req>,
49{
50    type Res = S::Res;
51    type Error = S::Error;
52
53    fn bind(&self, ctx: crate::BindCtx<'_>) {
54        ctx.bind(&self.s);
55    }
56
57    #[inline]
58    #[allow(clippy::manual_async_fn)]
59    fn call(
60        &self,
61        req: Req,
62        ctx: Ctx<'_, Self, OtSt>,
63    ) -> impl Future<Output = Result<S::Res, S::Error>> {
64        // See `ready`.
65        async move { self.s.call(req, ctx.map_state(&self.st).cast()).await }
66    }
67
68    #[inline]
69    #[allow(clippy::manual_async_fn)]
70    fn ready(&self, ctx: Ctx<'_, Self, OtSt>) -> impl Future<Output = Result<(), S::Error>> {
71        // An `async fn` would store `ctx` twice; `map_state` borrows `ctx`,
72        // so the mapped context must be created inside the block.
73        async move { self.s.ready(ctx.map_state(&self.st).cast()).await }
74    }
75
76    #[inline]
77    #[allow(clippy::manual_async_fn)]
78    fn shutdown(&self, ctx: Ctx<'_, Self, OtSt>) -> impl Future<Output = ()> {
79        // See `ready`.
80        async move { self.s.shutdown(ctx.map_state(&self.st).cast()).await }
81    }
82}
83
84#[derive(Clone, Debug)]
85/// A factory that creates [`MapState`] services using fixed state.
86pub struct MapStateFactory<Sf, St> {
87    sf: Sf,
88    st: St,
89}
90
91impl<OtSt, Sf, St, Req> ServiceFactory<OtSt, Req> for MapStateFactory<Sf, St>
92where
93    Sf: ServiceFactory<St, Req>,
94    St: Clone,
95{
96    type Res = Sf::Res;
97    type Error = Sf::Error;
98
99    type Service = MapState<Sf::Service, St>;
100    type InitError = Sf::InitError;
101
102    #[inline]
103    async fn create(&self, _: &OtSt) -> Result<Self::Service, Self::InitError> {
104        Ok(MapState {
105            s: self.sf.create(&self.st).await?,
106            st: self.st.clone(),
107        })
108    }
109}
110
111#[cfg(test)]
112#[path = "../tests/unit/map_state.rs"]
113mod tests;