diff --git a/src/lib.rs b/src/lib.rs index f36b96c..6d0f112 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ extern crate futures; -use futures::Future; +use futures::{Future, IntoFuture}; use std::io; use std::rc::Rc; @@ -171,21 +171,26 @@ pub trait NewService { /// The `Service` value created by this factory type Instance: Service; - /// Create and return a new service value. - fn new_service(&self) -> io::Result; + /// The future of the `Service` instance. + type Future: Future; + + /// Create and return a new service value asynchronously. + fn new_service(&self) -> Self::Future; } -impl NewService for F - where F: Fn() -> io::Result, - R: Service, +impl NewService for F + where F: Fn() -> R, + R: IntoFuture, + S: Service, { - type Request = R::Request; - type Response = R::Response; - type Error = R::Error; - type Instance = R; + type Request = S::Request; + type Response = S::Response; + type Error = S::Error; + type Instance = S; + type Future = R::Future; - fn new_service(&self) -> io::Result { - (*self)() + fn new_service(&self) -> Self::Future { + (*self)().into_future() } } @@ -194,8 +199,9 @@ impl NewService for Arc { type Response = S::Response; type Error = S::Error; type Instance = S::Instance; + type Future = S::Future; - fn new_service(&self) -> io::Result { + fn new_service(&self) -> Self::Future { (**self).new_service() } } @@ -205,8 +211,9 @@ impl NewService for Rc { type Response = S::Response; type Error = S::Error; type Instance = S::Instance; + type Future = S::Future; - fn new_service(&self) -> io::Result { + fn new_service(&self) -> Self::Future { (**self).new_service() } }