-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Rename and expose LoopState as ControlFlow #76204
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
r? @scottmcm |
Ping @rust-lang/libs to check that this is a plausible addition (as unstable, so no full FCP needed):
|
@NoraCodes Looks like tidy is complaining:
Can you fix that, rebase to upstream/master (to avoid the merge conflict that kept CI from running initially), and force-push the new change, please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @NoraCodes! I left some minor notes/suggestions. You're free to ignore them until someone from the libs team weighs in.
|
||
/// Used to make try_fold closures more like normal loops | ||
#[unstable(feature="control_flow_enum", reason="new API", issue="75744")] | ||
#[derive(Debug, Clone, Copy, PartialEq)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[derive(Debug, Clone, Copy, PartialEq)] | |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
Hash
and especially Eq
seem handy, although they are not needed to replace LoopState
and are less essential for ControlFlow
than for Option
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that seems sensible! I don't know when you'd want to hash this but I suppose there's no reason to prevent it.
/// Used to make try_fold closures more like normal loops | ||
#[unstable(feature="control_flow_enum", reason="new API", issue="75744")] | ||
#[derive(Debug, Clone, Copy, PartialEq)] | ||
pub enum ControlFlow<C, B> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have found that C = ()
is very common when using this enum, common enough that it might deserve a defaulted type parameter. We could add one backwards compatibly but only if C
follows B
in the parameter list. Otherwise we would have to default B
at the same time and anyone that wishes to override it would have to override C
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be confusing to do this without changing the order of the variants as well, and we may as well do that too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On consideration, I think a separate PR would be best.
} | ||
} | ||
|
||
impl<R: Try> ControlFlow<R::Ok, R> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the second parameter be R::Error
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see - we can't do this because it's an unconstrained type parameter. Is there a good way to solve that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the break part here is intentionally the full impl Try
type (Result
, etc). They're definitely strange APIs, though -- break_value
is something that could plausibly stabilize, but these ones probably aren't.
Maybe leave this particular impl block over in iter
as non-pub so they can still be used there, but don't show up in the rustdoc? (And won't need #[unstable]
since they'll be unusable outside iter
.)
I've been contemplating making a pass through the implementations to use feature(try_blocks)
instead of explicit Try::foo
calls where possible; this PR going in would be a good impetus to go do that -- and hopefully delete these methods while I'm at it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. I misunderstood how these were being used. You're right, it's a very strange API.
57c720f
to
d0af125
Compare
r=me for the code; waiting on a "seems plausible" comment from a libs member. |
Looks plausible, so @bors r=scottmcm I agree with #75744 (comment) that "a |
📌 Commit 96eb5e1 has been approved by |
Rollup of 12 pull requests Successful merges: - rust-lang#75150 (Add a note for Ipv4Addr::to_ipv6_compatible) - rust-lang#76120 (Add `[T; N]::as_[mut_]slice`) - rust-lang#76142 (Make all methods of `std::net::Ipv4Addr` const) - rust-lang#76164 (Link to slice pattern in array docs) - rust-lang#76167 (Replace MinGW library hack with heuristic controlling link mode) - rust-lang#76204 (Rename and expose LoopState as ControlFlow) - rust-lang#76238 (Move to intra-doc links for library/core/src/iter/traits/iterator.rs) - rust-lang#76242 (Read: adjust a FIXME reference) - rust-lang#76243 (Fix typos in vec try_reserve(_exact) docs) - rust-lang#76245 (inliner: Avoid query cycles when optimizing generators) - rust-lang#76255 (Update books) - rust-lang#76261 (Use intra-doc links in `core::marker`) Failed merges: r? @ghost
Demote `ControlFlow::{from|into}_try` to `pub(crate)` They have mediocre names and non-obvious semantics, so personally I don't think they're worth trying to stabilize, and thus might as well just be internal (they're used for convenience in iterator adapters), not something shown in the rustdocs. I don't think anyone actually wanted to use them outside `core` -- they just got made public-but-unstable along with the whole type in rust-lang#76204 that promoted `LoopState` from an internal type to the exposed `ControlFlow` type. cc rust-lang#75744, the tracking issue they mention.
Demote `ControlFlow::{from|into}_try` to `pub(crate)` They have mediocre names and non-obvious semantics, so personally I don't think they're worth trying to stabilize, and thus might as well just be internal (they're used for convenience in iterator adapters), not something shown in the rustdocs. I don't think anyone actually wanted to use them outside `core` -- they just got made public-but-unstable along with the whole type in rust-lang#76204 that promoted `LoopState` from an internal type to the exposed `ControlFlow` type. cc rust-lang#75744, the tracking issue they mention. cc rust-lang#85608, the PR where I'm proposing stabilizing the type.
Demote `ControlFlow::{from|into}_try` to `pub(crate)` They have mediocre names and non-obvious semantics, so personally I don't think they're worth trying to stabilize, and thus might as well just be internal (they're used for convenience in iterator adapters), not something shown in the rustdocs. I don't think anyone actually wanted to use them outside `core` -- they just got made public-but-unstable along with the whole type in rust-lang#76204 that promoted `LoopState` from an internal type to the exposed `ControlFlow` type. cc rust-lang#75744, the tracking issue they mention. cc rust-lang#85608, the PR where I'm proposing stabilizing the type.
Basic PR for #75744. Addresses everything there except for documentation; lots of examples are probably a good idea.