boluo_core/service/
service_fn.rs1use super::Service;
2
3pub fn service_fn<F>(f: F) -> ServiceFn<F> {
19 ServiceFn { f }
20}
21
22#[derive(Clone, Copy)]
24
25pub struct ServiceFn<F> {
26 f: F,
27}
28
29impl<F, Fut, Req, Res, Err> Service<Req> for ServiceFn<F>
30where
31 F: Fn(Req) -> Fut + Send + Sync,
32 Fut: Future<Output = Result<Res, Err>> + Send,
33{
34 type Response = Res;
35 type Error = Err;
36
37 fn call(&self, req: Req) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send {
38 (self.f)(req)
39 }
40}
41
42impl<F> std::fmt::Debug for ServiceFn<F> {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("ServiceFn")
45 .field("f", &std::any::type_name::<F>())
46 .finish()
47 }
48}