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: Make the futures for Pre and Post opaque
  • Loading branch information
lilyball committed Jul 11, 2022
commit fbe69a135c6598b05de7a36fd707a9e1e907776b
2 changes: 1 addition & 1 deletion tower/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub mod future {
pub use super::map_result::MapResultFuture;
pub use super::optional::future as optional;
pub use super::then::ThenFuture;
pub use super::wrap::WrapFuture;
pub use super::wrap::{WrapFuture, WrapPreFuture, WrapPostFuture};
}

pub mod helper {
Expand Down
19 changes: 15 additions & 4 deletions tower/src/util/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,20 @@ where
type Value = Post;
type Response = Res;
type Error = E;
type Future = std::future::Ready<Result<(Req, Post), Result<Res, E>>>;
type Future = WrapPreFuture<Req, Post, Res, E>;

fn call(&mut self, request: Req) -> Self::Future {
let f2 = (self.f)(&request);
std::future::ready(Ok((request, f2)))
WrapPreFuture::new(futures_util::future::ready(Ok((request, f2))))
}
}

opaque_future! {
/// Future returned by the [`PreFn`] impl for [`Pre`].
pub type WrapPreFuture<Req, Post, Res, E> =
futures_util::future::Ready<Result<(Req, Post), Result<Res, E>>>;
}

/// Helper type for use with [`Wrap::new`].
///
/// This type is an adaptor from [`Wrap::new`]'s synchronous post function to the more generic
Expand All @@ -386,13 +392,18 @@ where
{
type Response = T;
type Error = E;
type Future = std::future::Ready<Result<T, E>>;
type Future = WrapPostFuture<T, E>;

fn call(self, result: Result<Res, E>, value: F) -> Self::Future {
std::future::ready(result.map(value))
WrapPostFuture::new(futures_util::future::ready(result.map(value)))
}
}

opaque_future! {
/// Future returned by the [`PostFn`] impl for [`Post`].
pub type WrapPostFuture<T, E> = futures_util::future::Ready<Result<T, E>>;
}

impl<S: fmt::Debug, Pre, Post> fmt::Debug for Wrap<S, Pre, Post> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Wrap")
Expand Down