Skip to content

implement continue_ok and break_ok for ControlFlow #140267

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 2 commits into
base: master
Choose a base branch
from

Conversation

jogru0
Copy link
Contributor

@jogru0 jogru0 commented Apr 24, 2025

Tracking issue: #140266

r? @dtolnay

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 24, 2025
@jogru0 jogru0 force-pushed the control_flow branch 4 times, most recently from 808b211 to 95d8b51 Compare April 24, 2025 19:44
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thank you!

It would help people understand when they would benefit from these methods if the example code in the documentation showed a little bit more fleshed out skeleton of a real-world use case where using the new helpers is a nontrivial improvement.

The current example code is not useful for this purpose because it bears no resemblance to anything we would expect a user to write. In fact, even for the thing it is intended to show (that break_ok maps Break to Ok) I think it is not even the best way to show that. It could instead just say something like "control_flow.break_ok() is equivalent to: match control_flow { ...". But this is redundant with someone clicking the "Source" link if they are still confused after reading the 1-sentence method description and the updated example, so please decide if it's worth having after updating the example.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2025
@jogru0
Copy link
Contributor Author

jogru0 commented Apr 26, 2025

Thanks for the feedback!

I attempted to add some more fleshed out examples for both methods. Please have a look.

I think my example for continue_ok makes roughly sense, and is basically the initial motivation for the ACP: Have an interface function to validate that an operation was completed, which uses Result since that conveys the intended semantics.

I tried to find a sensible analogous usecase for break_ok, but as long as ControlFlow uses () for C, it feels a bit arbitrary to work with Result<B, ()> instead of just using break_value to get an Option<B>. For now, my example demonstrates how to implement find that way, which of course has this exact issue just mentioned: Conventionally, find would return a Option<&T>, so it would make more sense to implement it via break_value.

Therefore, a proper good usage example for break_value requires an usecase for ControlFlow where C is not (). I don't really know what that would be, but maybe someone else has a good idea? If we find one, maybe my current find example can be converted to an example for break_value, as that currently also doesn't have any fleshed out example in the docs.

Finally, something I noticed: My examples all derive from the existing ControlFlow module test. Interestingly, for the find implementation, I had to fight quite a bit with the borrow checker until I managed to get it to compile. It was only possible after adding explicit lifetimes to the traverse_inorder method from the module example. Without that, the compiler told me:

note: due to current limitations in the borrow checker, this implies a `'static` lifetime
  --> src/main.rs:26:22
   |
26 |         f: &mut impl FnMut(&T) -> ControlFlow<B>,
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^

So I was wondering, should we maybe add the explicit lifetimes in the module wide example as well, if that seems to be necessary to be able to use the method for certain usecases?

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 26, 2025
@bors
Copy link
Collaborator

bors commented May 17, 2025

☔ The latest upstream changes (presumably #138087) made this pull request unmergeable. Please resolve the merge conflicts.

@dtolnay dtolnay added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Aug 5, 2025
@dtolnay
Copy link
Member

dtolnay commented Aug 5, 2025

@dtolnay
Copy link
Member

dtolnay commented Aug 5, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 5, 2025

📌 Commit b981b84 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 5, 2025
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 6, 2025
implement continue_ok and break_ok for ControlFlow

Tracking issue: rust-lang#140266

r? `@dtolnay`
bors added a commit that referenced this pull request Aug 6, 2025
Rollup of 9 pull requests

Successful merges:

 - #137831 (Tweak auto trait errors)
 - #138689 (add nvptx_target_feature)
 - #140267 (implement continue_ok and break_ok for ControlFlow)
 - #143764 (lower pattern bindings in the order they're written and base drop order on primary bindings' order)
 - #143857 (Port #[macro_export] to the new attribute parsing infrastructure)
 - #144650 (Additional tce tests)
 - #144676 (Add documentation for unstable_feature_bound)
 - #144794 (Port `#[coroutine]` to the new attribute system)
 - #144835 (Anonymize binders in tail call sig)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Aug 6, 2025
Rollup of 9 pull requests

Successful merges:

 - #137831 (Tweak auto trait errors)
 - #138689 (add nvptx_target_feature)
 - #140267 (implement continue_ok and break_ok for ControlFlow)
 - #143764 (lower pattern bindings in the order they're written and base drop order on primary bindings' order)
 - #143857 (Port #[macro_export] to the new attribute parsing infrastructure)
 - #144650 (Additional tce tests)
 - #144676 (Add documentation for unstable_feature_bound)
 - #144794 (Port `#[coroutine]` to the new attribute system)
 - #144835 (Anonymize binders in tail call sig)

r? `@ghost`
`@rustbot` modify labels: rollup
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 6, 2025
implement continue_ok and break_ok for ControlFlow

Tracking issue: rust-lang#140266

r? ``@dtolnay``
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 6, 2025
implement continue_ok and break_ok for ControlFlow

Tracking issue: rust-lang#140266

r? ```@dtolnay```
bors added a commit that referenced this pull request Aug 6, 2025
Rollup of 9 pull requests

Successful merges:

 - #137831 (Tweak auto trait errors)
 - #138689 (add nvptx_target_feature)
 - #140267 (implement continue_ok and break_ok for ControlFlow)
 - #143764 (lower pattern bindings in the order they're written and base drop order on primary bindings' order)
 - #143857 (Port #[macro_export] to the new attribute parsing infrastructure)
 - #144676 (Add documentation for unstable_feature_bound)
 - #144794 (Port `#[coroutine]` to the new attribute system)
 - #144835 (Anonymize binders in tail call sig)
 - #144836 (Change visibility of Args new function)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 6, 2025
implement continue_ok and break_ok for ControlFlow

Tracking issue: rust-lang#140266

r? ````@dtolnay````
bors added a commit that referenced this pull request Aug 6, 2025
Rollup of 21 pull requests

Successful merges:

 - #137831 (Tweak auto trait errors)
 - #138689 (add nvptx_target_feature)
 - #140267 (implement continue_ok and break_ok for ControlFlow)
 - #143679 (Preserve the .debug_gdb_scripts section)
 - #143857 (Port #[macro_export] to the new attribute parsing infrastructure)
 - #143929 (Mark all deprecation lints in name resolution as deny-by-default and report-in-deps)
 - #144133 (Stabilize const TypeId::of)
 - #144369 (Upgrade semicolon_in_expressions_from_macros from warn to deny)
 - #144473 (Address libunwind.a inconsistency issues in the bootstrap program)
 - #144498 (Add --print target-spec-json-schema)
 - #144552 (Rehome 33 `tests/ui/issues/` tests to other subdirectories under `tests/ui/`)
 - #144659 (bootstrap: refactor mingw dist and fix gnullvm)
 - #144676 (Add documentation for unstable_feature_bound)
 - #144794 (Port `#[coroutine]` to the new attribute system)
 - #144835 (Anonymize binders in tail call sig)
 - #144836 (Change visibility of Args new function)
 - #144861 (Stabilize `panic_payload_as_str` feature)
 - #144910 (Add regression tests for seemingly fixed issues)
 - #144913 ([rustdoc] Fix wrong `i` tooltip icon)
 - #144917 (Enforce tail call type is related to body return type in borrowck)
 - #144924 (compiletest: add hint for when a ui test produces no errors)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants