use std::cell::RefCell; use std::marker; use std::rc::Rc; use futures::{future, future::FutureResult, Async, Future, IntoFuture, Poll}; use tower_service::Service; /// Creates new `Service` values. /// /// Acts as a service factory. This is useful for cases where new `Service` /// values must be produced. One case is a TCP servier listener. The listner /// accepts new TCP streams, obtains a new `Service` value using the /// `NewService` trait, and uses that new `Service` value to process inbound /// requests on that new TCP stream. pub trait NewService { /// Requests handled by the service type Request; /// Responses given by the service type Response; /// Errors produced by the service type Error; /// The `Service` value created by this factory type Service: Service< Request = Self::Request, Response = Self::Response, Error = Self::Error, >; /// Pipeline configuration type Config: Clone; /// Errors produced while building a service. type InitError; /// The future of the `Service` instance. type Future: Future; /// Create and return a new service value asynchronously. fn new_service(&self, Self::Config) -> Self::Future; fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, F: IntoNewService, B: NewService< Request = Self::Response, Error = Self::Error, Config = Self::Config, InitError = Self::InitError, >, { AndThenNewService::new(self, new_service) } fn map_err(self, f: F) -> MapErrNewService where Self: Sized, F: Fn(Self::Error) -> E, { MapErrNewService::new(self, f) } fn map_init_err(self, f: F) -> MapInitErr where Self: Sized, F: Fn(Self::InitError) -> E, { MapInitErr::new(self, f) } } /// Trait for types that can be converted to a Service pub trait IntoService where T: Service, { /// Create service fn into(self) -> T; } /// Trait for types that can be converted to a Service pub trait IntoNewService where T: NewService, { /// Create service fn into_new_service(self) -> T; } impl IntoService for T where T: Service, { fn into(self) -> T { self } } impl IntoNewService for T where T: NewService, { fn into_new_service(self) -> T { self } } impl IntoService> for F where F: Fn(Req) -> Fut + 'static, Fut: IntoFuture, { fn into(self) -> FnService { FnService::new(self) } } pub struct FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } impl FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { pub fn new(f: F) -> Self { FnService { f, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } } } impl Service for FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { type Request = Req; type Response = Resp; type Error = E; type Future = Fut::Future; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, req: Req) -> Self::Future { (self.f)(req).into_future() } } pub struct FnNewService where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, cfg: marker::PhantomData, } impl FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { fn new(f: F) -> Self { FnNewService { f, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, cfg: marker::PhantomData, } } } impl NewService for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, Cfg: Clone, { type Request = Req; type Response = Resp; type Error = Err; type Service = FnService; type Config = Cfg; type InitError = (); type Future = FutureResult; fn new_service(&self, cfg: Cfg) -> Self::Future { future::ok(FnService::new(self.f.clone())) } } impl IntoNewService> for F where F: Fn(Req) -> Fut + Clone + 'static, Fut: IntoFuture, Cfg: Clone, { fn into_new_service(self) -> FnNewService { FnNewService::new(self) } } impl Clone for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } pub struct FnStateService where F: Fn(&mut S, Req) -> Fut, Fut: IntoFuture, { f: F, state: S, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } impl FnStateService where F: Fn(&mut S, Req) -> Fut, Fut: IntoFuture, { pub fn new(state: S, f: F) -> Self { FnStateService { f, state, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } } } impl Service for FnStateService where F: Fn(&mut S, Req) -> Fut, Fut: IntoFuture, { type Request = Req; type Response = Resp; type Error = Err; type Future = Fut::Future; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, req: Req) -> Self::Future { (self.f)(&mut self.state, req).into_future() } } /// `NewService` for state and handler functions pub struct FnStateNewService { f: F1, state: F2, s: marker::PhantomData, req: marker::PhantomData, resp: marker::PhantomData, err1: marker::PhantomData, err2: marker::PhantomData, fut1: marker::PhantomData, fut2: marker::PhantomData, cfg: marker::PhantomData, } impl FnStateNewService { fn new(f: F1, state: F2) -> Self { FnStateNewService { f, state, s: marker::PhantomData, req: marker::PhantomData, resp: marker::PhantomData, err1: marker::PhantomData, err2: marker::PhantomData, fut1: marker::PhantomData, fut2: marker::PhantomData, cfg: marker::PhantomData, } } } impl NewService for FnStateNewService where S: 'static, F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, F2: Fn() -> Fut2, Fut1: IntoFuture + 'static, Fut2: IntoFuture + 'static, Req: 'static, Resp: 'static, Err1: 'static, Err2: 'static, Cfg: Clone, { type Request = Req; type Response = Resp; type Error = Err1; type Service = FnStateService; type Config = Cfg; type InitError = Err2; type Future = Box>; fn new_service(&self, cfg: Cfg) -> Self::Future { let f = self.f.clone(); Box::new( (self.state)() .into_future() .and_then(move |state| Ok(FnStateService::new(state, f))), ) } } impl IntoNewService> for (F1, F2) where S: 'static, F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, F2: Fn() -> Fut2, Fut1: IntoFuture + 'static, Fut2: IntoFuture + 'static, Req: 'static, Resp: 'static, Err1: 'static, Err2: 'static, Cfg: Clone, { fn into_new_service( self, ) -> FnStateNewService { FnStateNewService::new(self.0, self.1) } } impl Clone for FnStateNewService where F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, F2: Fn() -> Fut2 + Clone, Fut1: IntoFuture, Fut2: IntoFuture, { fn clone(&self) -> Self { Self::new(self.f.clone(), self.state.clone()) } } /// `AndThen` service combinator pub struct AndThen { a: A, b: Rc>, } impl AndThen where A: Service, B: Service, { /// Create new `AndThen` combinator pub fn new(a: A, b: B) -> Self { Self { a, b: Rc::new(RefCell::new(b)), } } } impl Service for AndThen where A: Service, B: Service, { type Request = A::Request; type Response = B::Response; type Error = B::Error; type Future = AndThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { let res = self.a.poll_ready(); if let Ok(Async::Ready(_)) = res { self.b.borrow_mut().poll_ready() } else { res } } fn call(&mut self, req: Self::Request) -> Self::Future { AndThenFuture::new(self.a.call(req), self.b.clone()) } } pub struct AndThenFuture where A: Service, B: Service, { b: Rc>, fut_b: Option, fut_a: A::Future, } impl AndThenFuture where A: Service, B: Service, { fn new(fut_a: A::Future, b: Rc>) -> Self { AndThenFuture { b, fut_a, fut_b: None, } } } impl Future for AndThenFuture where A: Service, B: Service, { type Item = B::Response; type Error = B::Error; fn poll(&mut self) -> Poll { if let Some(ref mut fut) = self.fut_b { return fut.poll(); } match self.fut_a.poll()? { Async::Ready(resp) => { self.fut_b = Some(self.b.borrow_mut().call(resp)); self.poll() } Async::NotReady => Ok(Async::NotReady), } } } /// `AndThenNewService` new service combinator pub struct AndThenNewService { a: A, b: B, } impl AndThenNewService where A: NewService, B: NewService, { /// Create new `AndThen` combinator pub fn new>(a: A, f: F) -> Self { Self { a, b: f.into_new_service(), } } } impl NewService for AndThenNewService where A: NewService< Response = B::Request, Error = B::Error, Config = B::Config, InitError = B::InitError, >, B: NewService, { type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = AndThen; type Config = A::Config; type InitError = A::InitError; type Future = AndThenNewServiceFuture; fn new_service(&self, cfg: A::Config) -> Self::Future { AndThenNewServiceFuture::new(self.a.new_service(cfg.clone()), self.b.new_service(cfg)) } } impl Clone for AndThenNewService where A: NewService + Clone, B: NewService + Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), b: self.b.clone(), } } } pub struct AndThenNewServiceFuture where A: NewService, B: NewService, { fut_b: B::Future, fut_a: A::Future, a: Option, b: Option, } impl AndThenNewServiceFuture where A: NewService, B: NewService, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { fut_a, fut_b, a: None, b: None, } } } impl Future for AndThenNewServiceFuture where A: NewService, B: NewService, { type Item = AndThen; type Error = B::InitError; fn poll(&mut self) -> Poll { if let Async::Ready(service) = self.fut_a.poll()? { self.a = Some(service); } if let Async::Ready(service) = self.fut_b.poll()? { self.b = Some(service); } if self.a.is_some() && self.b.is_some() { Ok(Async::Ready(AndThen::new( self.a.take().unwrap(), self.b.take().unwrap(), ))) } else { Ok(Async::NotReady) } } } /// `MapErr` service combinator pub struct MapErr { a: A, f: F, e: marker::PhantomData, } impl MapErr where A: Service, F: Fn(A::Error) -> E, { /// Create new `MapErr` combinator pub fn new(a: A, f: F) -> Self { Self { a, f, e: marker::PhantomData, } } } impl Service for MapErr where A: Service, F: Fn(A::Error) -> E + Clone, { type Request = A::Request; type Response = A::Response; type Error = E; type Future = MapErrFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.a.poll_ready().map_err(&self.f) } fn call(&mut self, req: Self::Request) -> Self::Future { MapErrFuture::new(self.a.call(req), self.f.clone()) } } pub struct MapErrFuture where A: Service, F: Fn(A::Error) -> E, { f: F, fut: A::Future, } impl MapErrFuture where A: Service, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { MapErrFuture { f, fut } } } impl Future for MapErrFuture where A: Service, F: Fn(A::Error) -> E, { type Item = A::Response; type Error = E; fn poll(&mut self) -> Poll { self.fut.poll().map_err(&self.f) } } /// `MapErrNewService` new service combinator pub struct MapErrNewService { a: A, f: F, e: marker::PhantomData, } impl MapErrNewService where A: NewService, F: Fn(A::Error) -> E, { /// Create new `MapErr` new service instance pub fn new(a: A, f: F) -> Self { Self { a, f, e: marker::PhantomData, } } } impl Clone for MapErrNewService where A: NewService + Clone, F: Fn(A::InitError) -> E + Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), e: marker::PhantomData, } } } impl NewService for MapErrNewService where A: NewService + Clone, F: Fn(A::Error) -> E + Clone, { type Request = A::Request; type Response = A::Response; type Error = E; type Service = MapErr; type Config = A::Config; type InitError = A::InitError; type Future = MapErrNewServiceFuture; fn new_service(&self, cfg: Self::Config) -> Self::Future { MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } pub struct MapErrNewServiceFuture where A: NewService, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } impl MapErrNewServiceFuture where A: NewService, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { MapErrNewServiceFuture { f, fut } } } impl Future for MapErrNewServiceFuture where A: NewService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; type Error = A::InitError; fn poll(&mut self) -> Poll { if let Async::Ready(service) = self.fut.poll()? { Ok(Async::Ready(MapErr::new(service, self.f.clone()))) } else { Ok(Async::NotReady) } } } /// `MapInitErr` service combinator pub struct MapInitErr { a: A, f: F, e: marker::PhantomData, } impl MapInitErr where A: NewService, F: Fn(A::InitError) -> E, { /// Create new `MapInitErr` combinator pub fn new(a: A, f: F) -> Self { Self { a, f, e: marker::PhantomData, } } } impl Clone for MapInitErr where A: NewService + Clone, F: Fn(A::InitError) -> E + Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), e: marker::PhantomData, } } } impl NewService for MapInitErr where A: NewService, F: Fn(A::InitError) -> E + Clone, { type Request = A::Request; type Response = A::Response; type Error = A::Error; type Service = A::Service; type Config = A::Config; type InitError = E; type Future = MapInitErrFuture; fn new_service(&self, cfg: Self::Config) -> Self::Future { MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) } } pub struct MapInitErrFuture where A: NewService, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } impl MapInitErrFuture where A: NewService, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { MapInitErrFuture { f, fut } } } impl Future for MapInitErrFuture where A: NewService, F: Fn(A::InitError) -> E, { type Item = A::Service; type Error = E; fn poll(&mut self) -> Poll { self.fut.poll().map_err(&self.f) } }