|
3 | 3 | use alloc::{boxed::Box, rc::Rc}; |
4 | 4 | use core::{future::Future, pin::Pin}; |
5 | 5 |
|
6 | | -use paste::paste; |
7 | | - |
8 | 6 | use crate::{Service, ServiceFactory}; |
9 | 7 |
|
10 | 8 | /// A boxed future with no send bound or lifetime parameters. |
11 | 9 | pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T>>>; |
12 | 10 |
|
13 | | -macro_rules! service_object { |
14 | | - ($name: ident, $type: tt, $fn_name: ident) => { |
15 | | - paste! { |
16 | | - #[doc = "Type alias for service trait object using `" $type "`."] |
17 | | - pub type $name<Req, Res, Err> = $type< |
18 | | - dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>, |
19 | | - >; |
20 | | - |
21 | | - #[doc = "Wraps service as a trait object using [`" $name "`]."] |
22 | | - pub fn $fn_name<S, Req>(service: S) -> $name<Req, S::Response, S::Error> |
23 | | - where |
24 | | - S: Service<Req> + 'static, |
25 | | - Req: 'static, |
26 | | - S::Future: 'static, |
27 | | - { |
28 | | - $type::new(ServiceWrapper::new(service)) |
29 | | - } |
30 | | - } |
31 | | - }; |
| 11 | +/// Type alias for service trait object using [`Box`]. |
| 12 | +pub type BoxService<Req, Res, Err> = |
| 13 | + Box<dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>>; |
| 14 | + |
| 15 | +/// Wraps service as a trait object using [`BoxService`]. |
| 16 | +pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error> |
| 17 | +where |
| 18 | + S: Service<Req> + 'static, |
| 19 | + Req: 'static, |
| 20 | + S::Future: 'static, |
| 21 | +{ |
| 22 | + Box::new(ServiceWrapper::new(service)) |
32 | 23 | } |
33 | 24 |
|
34 | | -service_object!(BoxService, Box, service); |
35 | | -service_object!(RcService, Rc, rc_service); |
| 25 | +/// Type alias for service trait object using [`Rc`]. |
| 26 | +pub type RcService<Req, Res, Err> = |
| 27 | + Rc<dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>>; |
| 28 | + |
| 29 | +/// Wraps service as a trait object using [`RcService`]. |
| 30 | +pub fn rc_service<S, Req>(service: S) -> RcService<Req, S::Response, S::Error> |
| 31 | +where |
| 32 | + S: Service<Req> + 'static, |
| 33 | + Req: 'static, |
| 34 | + S::Future: 'static, |
| 35 | +{ |
| 36 | + Rc::new(ServiceWrapper::new(service)) |
| 37 | +} |
36 | 38 |
|
37 | 39 | struct ServiceWrapper<S> { |
38 | 40 | inner: S, |
|
0 commit comments