xitca_service/service/
function.rs1use core::{convert::Infallible, future::Future};
2
3use super::Service;
4
5pub fn fn_build<F>(f: F) -> FnService<F> {
7 FnService(f)
8}
9
10pub fn fn_service<F>(f: F) -> FnService<impl AsyncFn(()) -> Result<FnService<F>, Infallible> + Clone>
12where
13 F: Clone,
14{
15 fn_build(async move |_| Ok(FnService(f.clone())))
16}
17
18#[derive(Clone)]
20pub struct FnService<F>(F);
21
22impl<F, Req, Res, Err> Service<Req> for FnService<F>
23where
24 F: AsyncFn(Req) -> Result<Res, Err>,
25{
26 type Response = Res;
27 type Error = Err;
28
29 #[inline]
30 fn call(&self, req: Req) -> impl Future<Output = Result<Self::Response, Self::Error>> {
31 (self.0)(req)
32 }
33}