-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, `ServiceExt` and `ServiceBuilder` provide combinators for mapping successful responses to other responses, and mapping errors to other errors, but don't provide a way to map between `Ok` and `Err` results. For completeness, this branch adds a new `then` combinator, which takes a function from `Result` to `Result` and applies it when the service's future completes. This can be used for recovering from some errors or for rejecting some `Ok` responses. It can also be used for behaviors that should be run when a service's future completes regardless of whether it completed successfully or not. Depends on #499
- Loading branch information
Showing
3 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use futures_util::FutureExt; | ||
use std::task::{Context, Poll}; | ||
use tower_layer::Layer; | ||
use tower_service::Service; | ||
|
||
pub use futures_util::future::Map as ThenFuture; | ||
|
||
/// Service returned by the [`then`] combinator. | ||
/// | ||
/// [`then`]: crate::util::ServiceExt::then | ||
#[derive(Clone, Debug)] | ||
pub struct Then<S, F> { | ||
inner: S, | ||
f: F, | ||
} | ||
|
||
/// A [`Layer`] that produces a [`Then`] service. | ||
/// | ||
/// [`Layer`]: tower_layer::Layer | ||
#[derive(Debug, Clone)] | ||
pub struct ThenLayer<F> { | ||
f: F, | ||
} | ||
|
||
impl<S, F> Then<S, F> { | ||
/// Creates a new `Then` service. | ||
pub fn new(inner: S, f: F) -> Self { | ||
Then { f, inner } | ||
} | ||
} | ||
|
||
impl<S, F, Request, Response, Error> Service<Request> for Then<S, F> | ||
where | ||
S: Service<Request>, | ||
Error: From<S::Error>, | ||
F: FnOnce(Result<S::Response, S::Error>) -> Result<Response, Error> + Clone, | ||
{ | ||
type Response = Response; | ||
type Error = Error; | ||
type Future = ThenFuture<S::Future, F>; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
#[inline] | ||
fn call(&mut self, request: Request) -> Self::Future { | ||
self.inner.call(request).map(self.f.clone()) | ||
} | ||
} | ||
|
||
impl<F> ThenLayer<F> { | ||
/// Creates a new [`ThenLayer`] layer. | ||
pub fn new(f: F) -> Self { | ||
ThenLayer { f } | ||
} | ||
} | ||
|
||
impl<S, F> Layer<S> for ThenLayer<F> | ||
where | ||
F: Clone, | ||
{ | ||
type Service = Then<S, F>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
Then { | ||
f: self.f.clone(), | ||
inner, | ||
} | ||
} | ||
} |