use std::marker::PhantomData; use std::sync::Arc; use futures::IntoFuture; use tokio_service::{NewService, Service}; /// Create a `Service` from a function. pub fn service_fn(f: F) -> ServiceFn where F: Fn(R) -> S, S: IntoFuture, { ServiceFn { f: f, _req: PhantomData, } } /// Create a `NewService` by sharing references of `service. pub fn const_service(service: S) -> ConstService { ConstService { svc: Arc::new(service), } } #[derive(Debug)] pub struct ServiceFn { f: F, _req: PhantomData R>, } impl Service for ServiceFn where F: Fn(R) -> S, S: IntoFuture, { type Request = R; type Response = S::Item; type Error = S::Error; type Future = S::Future; fn call(&self, req: Self::Request) -> Self::Future { (self.f)(req).into_future() } } #[derive(Debug)] pub struct ConstService { svc: Arc, } impl NewService for ConstService where S: Service, { type Request = S::Request; type Response = S::Response; type Error = S::Error; type Instance = Arc; fn new_service(&self) -> ::std::io::Result { Ok(self.svc.clone()) } }