Skip to content
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

feature: add support for StreamMap to return on closed streams #4851

Merged
merged 7 commits into from
Apr 16, 2023

Conversation

aviramha
Copy link
Contributor

@aviramha aviramha commented Jul 21, 2022

Following #4847 - I think this is the best solution - I'm open to alternatives (especially as I'm not so good at generics, so there are probably improvements!)

Options I ruled out:

  • Wrapping the stream with a generic type that does this abstraction - requires wrapping each stream, then implementing all of it's functions if it has any extra besides the trait or needing to interact directly..
  • Different struct - "overkill" in terms of maintenance
  • Adding a function next_or_none - would require many abstractions to be added (next is only provided by StreamExt that relies on other functions, poll etc

@Darksonn Darksonn added the A-tokio-stream Area: The tokio-stream crate label Jul 21, 2022
@Darksonn
Copy link
Contributor

Adding a third generic parameter for this seems a bit overkill. I would definitely prefer just providing a wrapper stream for this use-case instead.

@aviramha
Copy link
Contributor Author

@Darksonn I understand what you're saying, but it feels funny as of right now Stream already provides a way of knowing it is closed (as it returns None when it ends). It's StreamMap implementation that decided to ignore it and wrapping the Stream itself feels like abuse

@Darksonn
Copy link
Contributor

Whether or not the current design was a mistake doesn't really matter when it comes to designing how to add this feature. We need to pick the best design among the backwards compatible ones, and I don't agree that adding a third generic parameter is the best choice.

@aviramha
Copy link
Contributor Author

What about changing the implementation based on a feature flag?

@Darksonn
Copy link
Contributor

Enabling a feature flag must never change the behavior of code that compiles without it. This is because the crate is only compiled once, so if multiple things depend on tokio-stream and only some of them enable the feature flag, then they all get the feature flag.

@Darksonn Darksonn marked this pull request as draft August 10, 2022 13:15
@Darksonn
Copy link
Contributor

Since changes to this PR have been requested, I am marking the PR as a draft. Please remove the draft marker when you would like me to review the PR again. If you no longer want to work on this PR, then please close the PR.

@aviramha
Copy link
Contributor Author

Sorry, I hoped someone would have a better solution then the ones suggested so far. I'll change this PR to match your suggestion.

@aviramha aviramha marked this pull request as ready for review August 23, 2022 10:42
@aviramha
Copy link
Contributor Author

Rewrote this as suggested (wrapper struct).

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

There are some minor issues that I have outlined below, but overall this looks really good! I'm happy to see that you included a test. :)

tokio-stream/src/wrappers/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/wrappers.rs Outdated Show resolved Hide resolved
tokio-stream/src/wrappers/stream_close.rs Outdated Show resolved Hide resolved
pin_project! {
/// Stream that returns None when the stream is exhausted. To be used with StreamMap to know when a stream ends.
#[must_use = "streams do nothing unless polled"]
pub struct StreamNotifyClose<S> {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name is not great — notify normally means something different. But I can't think of anything better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree. StreamClose implies nothing really.. would love suggestions.

tokio-stream/src/wrappers/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/wrappers/stream_close.rs Outdated Show resolved Hide resolved
@aviramha
Copy link
Contributor Author

There are some minor issues that I have outlined below, but overall this looks really good! I'm happy to see that you included a test. :)

Thanks! Untested code is evil code ;)

@aviramha aviramha force-pushed the master branch 3 times, most recently from 2515d3a to 7168215 Compare August 23, 2022 11:50
@aviramha aviramha requested a review from Darksonn August 23, 2022 13:12
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

I have some comments on your documentation.

tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_map.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_map.rs Outdated Show resolved Hide resolved
tokio-stream/tests/stream_close.rs Outdated Show resolved Hide resolved
@Darksonn
Copy link
Contributor

There are some compilation failures in your documentation tests. You can see these on your local machine by running cargo test or cargo test --doc.

@aviramha
Copy link
Contributor Author

aviramha commented Aug 24, 2022

cargo test --doc

Thanks for the feedback. I have submitted changes according to it.
Edit:
Just re-read the CONTRIBUTING.md and realized I'm squashing against the recommendation. Sorry about that.

…urns None when it ends, for use with StreamMap
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
tokio-stream/src/stream_close.rs Outdated Show resolved Hide resolved
@aviramha aviramha requested a review from Darksonn August 24, 2022 14:29
@Darksonn
Copy link
Contributor

You will need to run rustfmt to make CI happy.

Other than that, my only concern about this is the naming. I still don't like using the word "notify" in the name for this.

@aviramha
Copy link
Contributor Author

You will need to run rustfmt to make CI happy.

Other than that, my only concern about this is the naming. I still don't like using the word "notify" in the name for this.

I'm running cargo fmt --all and no changes?

@Darksonn
Copy link
Contributor

The cargo fmt command is broken in the Tokio repo. The CONTRIBUTING.md file has the correct command.

@aviramha
Copy link
Contributor Author

The cargo fmt command is broken in the Tokio repo. The CONTRIBUTING.md file has the correct command.

rustfmt --check --edition 2018 $(git ls-files '*.rs') doesn't show any output? maybe I'll just wait for the CI to show what's the issue and fix it manually.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Hi. I'm sorry that this got lost. However, it LGTM, so I'll go ahead and merge it. Thank you for the PR.

@Darksonn Darksonn enabled auto-merge (squash) April 16, 2023 14:38
@Darksonn Darksonn merged commit 9507f8b into tokio-rs:master Apr 16, 2023
folkertdev pushed a commit to folkertdev/tokio that referenced this pull request Apr 20, 2023
folkertdev pushed a commit to folkertdev/tokio that referenced this pull request Apr 21, 2023
folkertdev pushed a commit to folkertdev/tokio that referenced this pull request May 19, 2023
folkertdev pushed a commit to folkertdev/tokio that referenced this pull request May 23, 2023
folkertdev pushed a commit to folkertdev/tokio that referenced this pull request May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-stream Area: The tokio-stream crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants