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

Minor improvements to All and Any adapters #2782

Merged
merged 3 commits into from
Oct 22, 2023

Conversation

cstyles
Copy link
Contributor

@cstyles cstyles commented Oct 22, 2023

I found a few things in the implementation for these adapters that I thought could be improved while adding TryAll and TryAny.

…rcuit

These futures should panic if they are polled after completing.
Currently they do so but only if they complete due to exhausting the
`Stream` that they pull data from. If they complete due to
short-circuiting, they are left in a state where `fut` and `accum` are
still `Some`. This means that if they are polled again, they end up
polling the inner `fut` again. That usually causes a panic but the error
message will likely reference the internal `Future`, not `All` / `Any`.

With this commit, `All` and `Any`'s internal state will be set such that
if they are polled again after completing, they will panic without
polling `fut`.
It looks like `All` was originally implemented by copying from `TryFold`
from which it inherited its `accum` field. However, `accum` can only
ever be one of two values: `None` (if `All` has already completed) or
`Some(true)` (if it's still processing values from the inner `Stream`).
It doesn't need to keep track of an accumulator because the very fact
that it hasn't short-circuited yet means that the accumulated value
can't be `Some(false)`. Therefore, we only need two values here and we
can represent them with a `bool` indicating whether or not `All` has
already completed.

The same principle applies for `Any` but substituting `Some(false)` for
`Some(true)`.
@cstyles cstyles force-pushed the minor-improvements-to-all-and-any branch from e1ac268 to 631a25e Compare October 22, 2023 02:48
@cstyles cstyles force-pushed the minor-improvements-to-all-and-any branch from 631a25e to 830333c Compare October 22, 2023 02:49
Copy link
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

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

Thanks!

@taiki-e taiki-e merged commit 2338478 into rust-lang:master Oct 22, 2023
25 checks passed
@taiki-e taiki-e mentioned this pull request Oct 26, 2023
@taiki-e taiki-e added 0.3-backport: completed A-stream Area: futures::stream labels Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants