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 3 times, most recently from 33c5e70 to 808b211 Compare April 24, 2025 19:43
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants