1 use std::error::Error as StdError; 2 3 use futures::{Future, IntoFuture}; 4 5 use body::Payload; 6 use super::{MakeService, Service}; 7 8 /// An asynchronous constructor of `Service`s. 9 pub trait NewService { 10 /// The `Payload` body of the `http::Request`. 11 type ReqBody: Payload; 12 13 /// The `Payload` body of the `http::Response`. 14 type ResBody: Payload; 15 16 /// The error type that can be returned by `Service`s. 17 type Error: Into<Box<StdError + Send + Sync>>; 18 19 /// The resolved `Service` from `new_service()`. 20 type Service: Service< 21 ReqBody=Self::ReqBody, 22 ResBody=Self::ResBody, 23 Error=Self::Error, 24 >; 25 26 /// The future returned from `new_service` of a `Service`. 27 type Future: Future<Item=Self::Service, Error=Self::InitError>; 28 29 /// The error type that can be returned when creating a new `Service`. 30 type InitError: Into<Box<StdError + Send + Sync>>; 31 32 /// Create a new `Service`. new_service(&self) -> Self::Future33 fn new_service(&self) -> Self::Future; 34 } 35 36 impl<F, R, S> NewService for F 37 where 38 F: Fn() -> R, 39 R: IntoFuture<Item=S>, 40 R::Error: Into<Box<StdError + Send + Sync>>, 41 S: Service, 42 { 43 type ReqBody = S::ReqBody; 44 type ResBody = S::ResBody; 45 type Error = S::Error; 46 type Service = S; 47 type Future = R::Future; 48 type InitError = R::Error; 49 new_service(&self) -> Self::Future50 fn new_service(&self) -> Self::Future { 51 (*self)().into_future() 52 } 53 } 54 55 impl<N, Ctx> MakeService<Ctx> for N 56 where 57 N: NewService, 58 { 59 type ReqBody = N::ReqBody; 60 type ResBody = N::ResBody; 61 type Error = N::Error; 62 type Service = N::Service; 63 type Future = N::Future; 64 type MakeError = N::InitError; 65 make_service(&mut self, _: Ctx) -> Self::Future66 fn make_service(&mut self, _: Ctx) -> Self::Future { 67 self.new_service() 68 } 69 } 70 71