ntex_service/
boxed.rs

1use std::{fmt, future::Future, pin::Pin, task::Context};
2
3use crate::ctx::{ServiceCtx, WaitersRef};
4
5type BoxFuture<'a, I, E> = Pin<Box<dyn Future<Output = Result<I, E>> + 'a>>;
6pub struct BoxService<Req, Res, Err>(Box<dyn ServiceObj<Req, Response = Res, Error = Err>>);
7pub struct BoxServiceFactory<Cfg, Req, Res, Err, InitErr>(
8    Box<dyn ServiceFactoryObj<Req, Cfg, Response = Res, Error = Err, InitError = InitErr>>,
9);
10
11/// Create boxed service factory
12pub fn factory<F, R, C>(
13    factory: F,
14) -> BoxServiceFactory<C, R, F::Response, F::Error, F::InitError>
15where
16    R: 'static,
17    C: 'static,
18    F: crate::ServiceFactory<R, C> + 'static,
19    F::Service: 'static,
20{
21    BoxServiceFactory(Box::new(factory))
22}
23
24/// Create boxed service
25pub fn service<S, R>(service: S) -> BoxService<R, S::Response, S::Error>
26where
27    R: 'static,
28    S: crate::Service<R> + 'static,
29{
30    BoxService(Box::new(service))
31}
32
33impl<Req, Res, Err> fmt::Debug for BoxService<Req, Res, Err> {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        f.debug_struct("BoxService").finish()
36    }
37}
38
39impl<Cfg, Req, Res, Err, InitErr> fmt::Debug
40    for BoxServiceFactory<Cfg, Req, Res, Err, InitErr>
41{
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.debug_struct("BoxServiceFactory").finish()
44    }
45}
46
47trait ServiceObj<Req> {
48    type Response;
49    type Error;
50
51    fn ready<'a>(
52        &'a self,
53        idx: u32,
54        waiters: &'a WaitersRef,
55    ) -> BoxFuture<'a, (), Self::Error>;
56
57    fn call<'a>(
58        &'a self,
59        req: Req,
60        idx: u32,
61        waiters: &'a WaitersRef,
62    ) -> BoxFuture<'a, Self::Response, Self::Error>;
63
64    fn shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>>;
65
66    fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error>;
67}
68
69impl<S, Req> ServiceObj<Req> for S
70where
71    S: crate::Service<Req>,
72    Req: 'static,
73{
74    type Response = S::Response;
75    type Error = S::Error;
76
77    #[inline]
78    fn ready<'a>(
79        &'a self,
80        idx: u32,
81        waiters: &'a WaitersRef,
82    ) -> BoxFuture<'a, (), Self::Error> {
83        Box::pin(async move { ServiceCtx::<'a, S>::new(idx, waiters).ready(self).await })
84    }
85
86    #[inline]
87    fn shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
88        Box::pin(crate::Service::shutdown(self))
89    }
90
91    #[inline]
92    fn call<'a>(
93        &'a self,
94        req: Req,
95        idx: u32,
96        waiters: &'a WaitersRef,
97    ) -> BoxFuture<'a, Self::Response, Self::Error> {
98        Box::pin(async move {
99            ServiceCtx::<'a, S>::new(idx, waiters)
100                .call_nowait(self, req)
101                .await
102        })
103    }
104
105    #[inline]
106    fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
107        crate::Service::poll(self, cx)
108    }
109}
110
111trait ServiceFactoryObj<Req, Cfg> {
112    type Response;
113    type Error;
114    type InitError;
115
116    fn create<'a>(
117        &'a self,
118        cfg: Cfg,
119    ) -> BoxFuture<'a, BoxService<Req, Self::Response, Self::Error>, Self::InitError>
120    where
121        Cfg: 'a;
122}
123
124impl<F, Req, Cfg> ServiceFactoryObj<Req, Cfg> for F
125where
126    Cfg: 'static,
127    Req: 'static,
128    F: crate::ServiceFactory<Req, Cfg>,
129    F::Service: 'static,
130{
131    type Response = F::Response;
132    type Error = F::Error;
133    type InitError = F::InitError;
134
135    #[inline]
136    fn create<'a>(
137        &'a self,
138        cfg: Cfg,
139    ) -> BoxFuture<'a, BoxService<Req, Self::Response, Self::Error>, Self::InitError>
140    where
141        Cfg: 'a,
142    {
143        let fut = crate::ServiceFactory::create(self, cfg);
144        Box::pin(async move { fut.await.map(service) })
145    }
146}
147
148impl<Req, Res, Err> crate::Service<Req> for BoxService<Req, Res, Err>
149where
150    Req: 'static,
151{
152    type Response = Res;
153    type Error = Err;
154
155    #[inline]
156    async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
157        let (idx, waiters) = ctx.inner();
158        self.0.ready(idx, waiters).await
159    }
160
161    #[inline]
162    async fn shutdown(&self) {
163        self.0.shutdown().await
164    }
165
166    #[inline]
167    async fn call(&self, req: Req, ctx: ServiceCtx<'_, Self>) -> Result<Res, Err> {
168        let (idx, waiters) = ctx.inner();
169        self.0.call(req, idx, waiters).await
170    }
171
172    #[inline]
173    fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
174        self.0.poll(cx)
175    }
176}
177
178impl<C, Req, Res, Err, InitErr> crate::ServiceFactory<Req, C>
179    for BoxServiceFactory<C, Req, Res, Err, InitErr>
180where
181    Req: 'static,
182{
183    type Response = Res;
184    type Error = Err;
185
186    type Service = BoxService<Req, Res, Err>;
187    type InitError = InitErr;
188
189    #[inline]
190    async fn create(&self, cfg: C) -> Result<Self::Service, Self::InitError> {
191        self.0.create(cfg).await
192    }
193}