-
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
Tracking issue for ControlFlow
enum, for use with try_fold
and in Try
#75744
Comments
Big +1 for this. A |
As the one who added @NoraCodes Want to take on making a PR here? There's probably a few parts:
Feel free to ping me (here, in a draft PR, on community Discord, or on Zulip) if you have questions. Its methods are probably imperfect right now, but if it's unstable that's ok. People can start using it on nightly as we'll find out what needs fixing before stabilization that way. |
I would be happy to do this. I may ask for some help getting a development environment set up as I have yet to contribute to the core libraries. |
This comment has been minimized.
This comment has been minimized.
LoopState
to make try_fold
closures easier to write and more idiomaticControlFlow
enum, for use with try_fold
and in Try
…=scottmcm Rename and expose LoopState as ControlFlow Basic PR for rust-lang#75744. Addresses everything there except for documentation; lots of examples are probably a good idea.
Congrats on your first core libraries PR, @NoraCodes! Want to take on the generic parameter reordering (that morse brought up in #76204 (comment)) next? |
Thanks @scottmcm! I definitely do want to do that. I'll put in a PR either this weekend or Tuesday.
|
Another example of a type that's pretty close to this: the |
An MCP to use this more often in the compiler: rust-lang/compiler-team#374 Conveniently that also suggests that EDIT: A great comment on the PR that implements the MCP (#78182 (comment)):
Nice to see confirmation of the value 🙂 |
Apologies for taking forever on this. I set up a Rust dev env on my new PC and fixed the nits in that PR so we should be good to go. I'll move forward with the other work, and documentation. |
…, r=scottmcm Add `ControlFlow::is_{break,continue}` methods r? @scottmcm cc rust-lang#75744
…inators, r=scottmcm Add map_continue and continue_value combinators to ControlFlow As suggested in this comment: rust-lang#75744 (comment) Related tracking issue: rust-lang#75744 r? `````@scottmcm`````
…inators, r=scottmcm Add map_continue and continue_value combinators to ControlFlow As suggested in this comment: rust-lang#75744 (comment) Related tracking issue: rust-lang#75744 r? ``````@scottmcm``````
…r=scottmcm Add map_continue and continue_value combinators to ControlFlow As suggested in this comment: rust-lang/rust#75744 (comment) Related tracking issue: rust-lang/rust#75744 r? ``````@scottmcm``````
I found a usecase where maybe ControlFlow could be used as well, having an early exit from a In case this should be in a new issue, let me know. Something like impl<T> ControlFlow<T, T> {
pub fn value(self) -> T {
match self {
ControlFlow::Continue(value) | ControlFlow::Break(value) => value,
}
}
} allowing to do let value = smth.try_fold(Vec::new(), |aggr, curr| {
// Do something
if todo!("Some condition that checks weather aggr is ready for early exit e.g. full") {
ControlFlow::Break(aggr)
} else {
ControlFlow::Continue(aggr)
}
}).value(); |
I've been wondering about whether we could move some of the shared operations between I realized that Proposed Method Naming
|
@ModProg I have also run into a case where such a method would be desirable |
The naming of Should the two methods be renamed before stabilization? Proposed Method Naming
|
They cannot have those names because they are strict keywords. |
Can we stabilize |
Hello libs-api folks! Inspired by the previous comment, I'm nominating this to get your thoughts on the methods currently tracked by this issue: impl<B, C> ControlFlow<B, C> {
pub fn break_value(self) -> Option<B>;
pub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>
where F: FnOnce(B) -> T;
pub fn continue_value(self) -> Option<C>;
pub fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>
where F: FnOnce(C) -> T;
} Methods like this were intentionally not part of the RFC that added These are basically like The other naming question from the thread is how to name the map methods, #75744 (comment). I added them as |
@scottmcm If we add these methods, |
@scottmcm We discussed your comment in the libs-api meeting. We're happy with the current names (with |
I'm sorry if this is the wrong place to mention this; but I've just been using
|
I would be in favor of a I'm concerned that implementing |
Thanks @NoraCodes - and just to add I much prefer the |
I've put up a stabilization PR for the remaining things tracked under this issue in #130518 @dhedey, can you open a new item for your request so it's more likely to be considered instead of lost? Maybe open an ACP to make the case to the libs-api folks, then if that's approved the attribute addition can be PRed? |
@ModProg @schuelermine Could one of your open an ACP proposing the (This one's over 4 years old, so I'd like to get it closed out rather than make people expand the history and read a bunch of no-longer-relevant things.) |
Will do, thanks @scottmcm 👍 |
…ra, r=dtolnay Stabilize the `map`/`value` methods on `ControlFlow` And fix the stability attribute on the `pub use` in `core::ops`. libs-api in rust-lang#75744 (comment) seemed reasonably happy with naming for these, so let's try for an FCP. Summary: ```rust impl<B, C> ControlFlow<B, C> { pub fn break_value(self) -> Option<B>; pub fn map_break<T>(self, f: impl FnOnce(B) -> T) -> ControlFlow<T, C>; pub fn continue_value(self) -> Option<C>; pub fn map_continue<T>(self, f: impl FnOnce(C) -> T) -> ControlFlow<B, T>; } ``` Resolves rust-lang#75744 `@rustbot` label +needs-fcp +t-libs-api -t-libs --- Aside, in case it keeps someone else from going down the same dead end: I looked at the `{break,continue}_value` methods and tried to make them `const` as part of this, but that's disallowed because of not having `const Drop`, so put it back to not even unstably-const.
Rollup merge of rust-lang#130518 - scottmcm:stabilize-controlflow-extra, r=dtolnay Stabilize the `map`/`value` methods on `ControlFlow` And fix the stability attribute on the `pub use` in `core::ops`. libs-api in rust-lang#75744 (comment) seemed reasonably happy with naming for these, so let's try for an FCP. Summary: ```rust impl<B, C> ControlFlow<B, C> { pub fn break_value(self) -> Option<B>; pub fn map_break<T>(self, f: impl FnOnce(B) -> T) -> ControlFlow<T, C>; pub fn continue_value(self) -> Option<C>; pub fn map_continue<T>(self, f: impl FnOnce(C) -> T) -> ControlFlow<B, T>; } ``` Resolves rust-lang#75744 ``@rustbot`` label +needs-fcp +t-libs-api -t-libs --- Aside, in case it keeps someone else from going down the same dead end: I looked at the `{break,continue}_value` methods and tried to make them `const` as part of this, but that's disallowed because of not having `const Drop`, so put it back to not even unstably-const.
Huzzah, everything tracked here is now stable! For anyone with |
…lnay Stabilize the `map`/`value` methods on `ControlFlow` And fix the stability attribute on the `pub use` in `core::ops`. libs-api in rust-lang/rust#75744 (comment) seemed reasonably happy with naming for these, so let's try for an FCP. Summary: ```rust impl<B, C> ControlFlow<B, C> { pub fn break_value(self) -> Option<B>; pub fn map_break<T>(self, f: impl FnOnce(B) -> T) -> ControlFlow<T, C>; pub fn continue_value(self) -> Option<C>; pub fn map_continue<T>(self, f: impl FnOnce(C) -> T) -> ControlFlow<B, T>; } ``` Resolves #75744 ``@rustbot`` label +needs-fcp +t-libs-api -t-libs --- Aside, in case it keeps someone else from going down the same dead end: I looked at the `{break,continue}_value` methods and tried to make them `const` as part of this, but that's disallowed because of not having `const Drop`, so put it back to not even unstably-const.
(edited to turn this into a tracking issue, as it's referenced by the
unstable
attributes)This is a tracking issue for the
std::ops::ControlFlow
type.The feature gate for the issue is
#![feature(control_flow_enum)]
.About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
Result
stry_trait_v2
#84767Iterator::try_fold
andIterator::try_for_each
Unresolved Questions
Should we change the generic parameter order? https://github.com/rust-lang/rust/pull/76204/files#r481357223done in change the order of type arguments on ControlFlow #76614(probably not theRemoved those ones in Demoteinto_try
ones, https://github.com/rust-lang/rust/pull/76204/files#r481515347)ControlFlow::{from|into}_try
topub(crate)
#85645Are theCONTINUE
/BREAK
constants valuable? See StabilizeControlFlow::{BREAK, CONTINUE}
#102697ControlFlow::{BREAK, CONTINUE}
#107398B = ()
too? (Might be nice fortry_for_each
uses.)Implementation history
Initial PR that added
LoopState
as an implementation detail: #45595PR that exposed as unstable and renamed to
ControlFlow
: #76204Added
BREAK
/CONTINUE
associated constants: #76318Changed type parameter order and defaulted
C = ()
: #76614Add
is_break
andis_continue
methods: #78200I work with an organization that has a large amount of Rust graph traversal code as part of its core business. We used to use
itertools
'sfold_while
for short-circuiting functional-style loops over slices of our graphs, which is now deprecated in favor oftry_fold
.try_fold
is great, but the lack of a standard library providedTry
implementation that makes the loop semantics clear is confusing. We created our own, which is fine, but I think it would make a lot of sense to exposeLoopState
and provide an example in the docs.Originally related to: rust-itertools/itertools#469
The text was updated successfully, but these errors were encountered: