Skip to content

util: add Wrap #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
util: Allow ServiceExt::wrap to mutate the response value
The `ServiceExt::wrap` combinator was supposed to let the post function
return a different result type than the inner service, but this wasn't
properly expressed in the type system.
  • Loading branch information
lilyball committed Jul 11, 2022
commit 3ad31c118a1d76a4bcf9fbd16787079dd81b0918
5 changes: 3 additions & 2 deletions tower/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,13 @@ impl<L> ServiceBuilder<L> {
/// error the error is output directly without being given to the post function.
#[cfg(feature = "util")]
#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
pub fn wrap<F, F2, Request, Response, E>(
pub fn wrap<F, F2, Request, Response, T, E>(
self,
f: F,
) -> ServiceBuilder<
Stack<
crate::util::WrapLayer<
crate::util::helper::Pre<Request, Response, E, F>,
crate::util::helper::Pre<Request, T, E, F>,
crate::util::helper::Post<F2, E>,
>,
L,
Expand All @@ -866,6 +866,7 @@ impl<L> ServiceBuilder<L> {
where
Self: Sized,
F: FnMut(&Request) -> F2,
F2: FnOnce(Response) -> T,
{
self.layer(crate::util::WrapLayer::new(f))
}
Expand Down
5 changes: 3 additions & 2 deletions tower/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,13 +1080,14 @@ pub trait ServiceExt<Request>: tower_service::Service<Request> {
/// given to the `Wrap` service, `Response` is the response returned from the inner service,
/// and `T` is the response returned from the `Wrap` service. If the inner service returns an
/// error the error is output directly without being given to the post function.
fn wrap<F, F2>(
fn wrap<F, F2, T>(
self,
f: F,
) -> Wrap<Self, wrap::Pre<Request, Self::Response, Self::Error, F>, wrap::Post<F2, Self::Error>>
) -> Wrap<Self, wrap::Pre<Request, T, Self::Error, F>, wrap::Post<F2, Self::Error>>
where
Self: Sized,
F: FnMut(&Request) -> F2,
F2: FnOnce(Self::Response) -> T,
{
Wrap::new(self, f)
}
Expand Down
13 changes: 8 additions & 5 deletions tower/src/util/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ impl<S, F, F2> Wrap<S, F, F2> {
/// given to the `Wrap` service, `Response` is the response returned from the inner service,
/// and `T` is the response returned from the `Wrap` service. If the inner service returns an
/// error the error is output directly without being given to the post function.
pub fn new<Request>(
pub fn new<Request, T>(
inner: S,
f: F,
) -> Wrap<S, Pre<Request, S::Response, S::Error, F>, Post<F2, S::Error>>
) -> Wrap<S, Pre<Request, T, S::Error, F>, Post<F2, S::Error>>
where
F: FnMut(&Request) -> F2,
F2: FnOnce(S::Response) -> T,
S: Service<Request>,
{
Wrap {
Expand All @@ -77,9 +78,10 @@ impl<F, F2> Wrap<(), F, F2> {
/// Returns a new [`Layer`] that produces [`Wrap`] services.
///
/// This is a convenience function that simply calls [`WrapLayer::new`].
pub fn layer<Req, Res, E>(f: F) -> WrapLayer<Pre<Req, Res, E, F>, Post<F2, E>>
pub fn layer<Request, Response, T, E>(f: F) -> WrapLayer<Pre<Request, T, E, F>, Post<F2, E>>
where
F: FnMut(&Req) -> F2,
F: FnMut(&Request) -> F2,
F2: FnOnce(Response) -> T,
{
WrapLayer::new(f)
}
Expand Down Expand Up @@ -125,9 +127,10 @@ impl<F, F2> WrapLayer<F, F2> {
/// given to the `Wrap` service, `Response` is the response returned from the inner service,
/// and `T` is the response returned from the `Wrap` service. If the inner service returns an
/// error the error is output directly without being given to the post function.
pub fn new<Request, Response, E>(f: F) -> WrapLayer<Pre<Request, Response, E, F>, Post<F2, E>>
pub fn new<Request, Response, T, E>(f: F) -> WrapLayer<Pre<Request, T, E, F>, Post<F2, E>>
where
F: FnMut(&Request) -> F2,
F2: FnOnce(Response) -> T,
{
WrapLayer {
pre: Pre {
Expand Down