xitca_web/handler/
sync.rs

1#![allow(non_snake_case)]
2
3use core::{convert::Infallible, marker::PhantomData};
4
5use super::{FromRequest, Responder};
6
7use xitca_service::{FnService, Service, fn_build};
8
9/// synchronous version of [handler_service]
10///
11/// `handler_sync_service` run given function on a separate thread pool from the event loop and async
12/// logic of xitca-web itself. Therefore it comes with different requirement of function argument
13/// compared to [handler_service] where the arguments must be types that impl [FromRequest] trait,
14/// being thread safe with `Send` trait bound and with `'static` lifetime.
15///
16/// # Examples:
17/// ```rust
18/// # use xitca_web::{
19/// #     handler::{
20/// #         {handler_service, handler_sync_service},
21/// #         uri::{UriOwn, UriRef},
22/// #     },
23/// #     App,
24/// #     WebContext
25/// # };
26///
27/// App::new()
28///     .at("/valid", handler_sync_service(|_: UriOwn| "uri is thread safe and owned value"))
29///     // uncomment the line below would result in compile error.
30///     // .at("/invalid1", handler_sync_service(|_: UriRef<'_>| { "uri ref is borrowed value" }))
31///     // uncomment the line below would result in compile error.
32///     // .at("/invalid2", handler_sync_service(|_: &WebContext<'_>| { "web request is borrowed value and not thread safe" }))
33///     # .at("/nah", handler_service(async |_: &WebContext<'_>| { "" }));
34/// ```
35///
36/// [handler_service]: super::handler_service
37pub fn handler_sync_service<Arg, F, T>(func: F) -> FnService<impl AsyncFn(Arg) -> FnServiceOutput<F, T>>
38where
39    F: Closure<T> + Send + Clone,
40{
41    fn_build(async move |_| {
42        Ok(HandlerServiceSync {
43            func: func.clone(),
44            _p: PhantomData,
45        })
46    })
47}
48
49type FnServiceOutput<F, T> = Result<HandlerServiceSync<F, T>, Infallible>;
50
51pub struct HandlerServiceSync<F, T> {
52    func: F,
53    _p: PhantomData<T>,
54}
55
56impl<F, Req, T, O> Service<Req> for HandlerServiceSync<F, T>
57where
58    // for borrowed extractors, `T` is the `'static` version of the extractors
59    T: FromRequest<'static, Req>,
60    // just to assist type inference to pinpoint `T`
61    F: Closure<T> + Send + Clone + 'static,
62    F: for<'a> Closure<T::Type<'a>, Output = O>,
63    O: Responder<Req> + Send + 'static,
64    for<'a> T::Type<'a>: Send + 'static,
65    T::Error: From<O::Error>,
66{
67    type Response = O::Response;
68    type Error = T::Error;
69
70    async fn call(&self, req: Req) -> Result<Self::Response, Self::Error> {
71        let extract = T::Type::<'_>::from_request(&req).await?;
72        let func = self.func.clone();
73        let res = tokio::task::spawn_blocking(move || func.call(extract)).await.unwrap();
74        res.respond(req).await.map_err(Into::into)
75    }
76}
77
78#[doc(hidden)]
79/// sync version of xitca_service::AsyncClosure trait.
80pub trait Closure<Arg> {
81    type Output;
82
83    fn call(&self, arg: Arg) -> Self::Output;
84}
85
86macro_rules! closure_impl {
87    ($($arg: ident),*) => {
88        impl<Func, O, $($arg,)*> Closure<($($arg,)*)> for Func
89        where
90            Func: Fn($($arg),*) -> O,
91        {
92            type Output = O;
93
94            #[inline]
95            fn call(&self, ($($arg,)*): ($($arg,)*)) -> Self::Output {
96                self($($arg,)*)
97            }
98        }
99    }
100}
101
102closure_impl! {}
103closure_impl! { A }
104closure_impl! { A, B }
105closure_impl! { A, B, C }
106closure_impl! { A, B, C, D }
107closure_impl! { A, B, C, D, E }
108closure_impl! { A, B, C, D, E, F }
109closure_impl! { A, B, C, D, E, F, G }
110closure_impl! { A, B, C, D, E, F, G, H }
111closure_impl! { A, B, C, D, E, F, G, H, I }