Skip to content

Add and_then combinator - #485

Merged
hawkw merged 7 commits into
tower-rs:masterfrom
hlb8122:add-andthen-combinator
Jan 6, 2021
Merged

Add and_then combinator#485
hawkw merged 7 commits into
tower-rs:masterfrom
hlb8122:add-andthen-combinator

Conversation

@hlb8122

@hlb8122 hlb8122 commented Nov 23, 2020

Copy link
Copy Markdown
Contributor

Motivation
https://docs.rs/futures/0.3.8/futures/future/trait.TryFutureExt.html#method.and_then is a useful method on futures. Perhaps it'd be nice to replicate this for the ServiceExt API.

TODO

  • Documentation
  • Are the generics sane?

Comment thread tower/src/util/and_then.rs Outdated
@hlb8122
hlb8122 marked this pull request as ready for review November 27, 2020 15:47
@hlb8122

hlb8122 commented Nov 27, 2020

Copy link
Copy Markdown
Contributor Author

@LucioFranco Just in case you missed this discussion: hlb8122#1

@hawkw hawkw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good! I had one suggestion around error types.

Also, it would be nice to add a ServiceBuilder method for pushing an and_then layer, similar to the ones for other combinators.

Comment thread tower/src/util/and_then.rs Outdated
@hlb8122
hlb8122 force-pushed the add-andthen-combinator branch from ad83154 to cfd520d Compare January 6, 2021 20:27

@hawkw hawkw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall, this looks good. let's change the response future type to a newtype.

i also had a small style suggestion, but you can take it or leave it.

Comment thread tower/src/util/and_then.rs Outdated
{
type Response = Fut::Ok;
type Error = Error;
type Future = AndThenFut<ErrIntoFut<S::Future, Error>, Fut, F>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's change this to a named type re-exported from util::future, the way the other combinators do. I added a macro to make this easier --- if you rebase onto master, you can follow the example of PR #509

@hlb8122 hlb8122 Jan 6, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem possible:

opaque_future! {
    /// Response future from [`AndThen`] services.
    ///
    /// [`AndThen`]: crate::util::AndThen
    pub type AndThenFuture<F1, F2: TryFuture, N> = future::AndThen<future::ErrInto<F1, F2::Error>, F2, N>;
}

Is this acceptable instead?

opaque_future! {
    /// Response future from [`AndThen`] services.
    ///
    /// [`AndThen`]: crate::util::AndThen
    pub type AndThenFuture<F1, F2, F2Error, N> = future::AndThen<future::ErrInto<F1, F2Error>, F2, N>;
}

EDIT: I could also just write the top one without using the macro.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either is fine --- you're right that the macro doesn't support trait bounds currently. We could try to hack that in, but I think it would be fine to manually write our own newtype.

Remember that if you do that, you should write pub struct AndThenFuture<...>(...) and write a Future impl for it, rather than pub type AndThenFuture.... This is what the macro would expand to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's that look?

Comment thread tower/src/util/and_then.rs Outdated

@hawkw hawkw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, thank you!

@hawkw
hawkw merged commit f171390 into tower-rs:master Jan 6, 2021
@hlb8122
hlb8122 deleted the add-andthen-combinator branch January 7, 2021 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants