diff --git a/actix-tower/src/lib.rs b/actix-tower/src/lib.rs index a57dad0f..78311a90 100644 --- a/actix-tower/src/lib.rs +++ b/actix-tower/src/lib.rs @@ -23,22 +23,29 @@ impl ActixCompat { /// Extension trait for wrapping a `tower_service::Service` instance for use as /// an `actix_service::Service`. -pub trait TowerServiceExt { +pub trait TowerServiceExt : TowerService + Sized { /// Wraps a `tower_service::Service` in a compatibility wrapper. - fn into_actix_service(self) -> ActixCompat - where - Self: TowerService + Sized; -} - -impl TowerServiceExt for S { - fn into_actix_service(self) -> ActixCompat - where - Self: TowerService + Sized - { + fn into_actix_service(self) -> ActixCompat { ActixCompat::new(self) } + + /// Takes a function that, when provided with an `actix_service::Service` wraps it + /// and returns a new service. Useful for wrapping a `tower_service::Service` with + /// middleware built for `actix_service`. + fn wrap_with_actix_middleware(self, f: F) -> TowerCompat + where + F: FnOnce(ActixCompat) -> U, + U: ActixService + { + f(self.into_actix_service()).into_tower_service() + } } +impl TowerServiceExt for S +where + S: TowerService + Sized +{} + impl ActixService for ActixCompat where S: TowerService, @@ -76,18 +83,26 @@ impl TowerCompat { /// a `tower_service::Service`. pub trait ActixServiceExt: ActixService + Sized { /// Wraps a `tower_service::Service` in a compatibility wrapper. - fn into_tower_service(self) -> TowerCompat; + fn into_tower_service(self) -> TowerCompat { + TowerCompat::new(self) + } + + /// Takes a function that, when provided with a `tower_service::Service` wraps it + /// and returns a new service. Useful for wrapping an `actix_service::Service` with + /// middleware built for `tower_service`. + fn wrap_with_tower_middleware(self, f: F) -> ActixCompat + where + F: FnOnce(TowerCompat) -> U, + U: TowerService + { + f(self.into_tower_service()).into_actix_service() + } } impl ActixServiceExt for S where S: ActixService + Sized -{ - fn into_tower_service(self) -> TowerCompat - { - TowerCompat::new(self) - } -} +{} impl TowerService for TowerCompat where @@ -133,6 +148,15 @@ mod tests { assert_eq!(Ok(Async::Ready(5)), s.call(()).poll()); } + #[test] + fn random_service_can_use_actix_middleware() { + let mut s = RandomService.wrap_with_actix_middleware(DoMathTransform); + + assert_eq!(Ok(Async::Ready(())), s.poll_ready()); + + assert_eq!(Ok(Async::Ready(68)), s.call(()).poll()); + } + #[test] fn random_service_and_add_service_chained() { let s1 = RandomService.into_actix_service(); @@ -277,8 +301,8 @@ mod tests { #[test] fn random_service_and_add_service_and_ignoring_service_chained() { - let s1 = AddOneService::wrap(RandomService.into_tower_service()).into_actix_service(); - let s2 = AddOneService::wrap(DoMathService.into_tower_service()).into_actix_service(); + let s1 = RandomService.wrap_with_tower_middleware(AddOneService::wrap); + let s2 = DoMathService.wrap_with_tower_middleware(AddOneService::wrap); let mut s = s1.and_then(s2);