-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add Option::take_if
#98934
Comments
Is there a reason to have both |
To be honest, personally I can't think of a concrete reason at the moment. The question would then also be which name to keep: I am also not sure about all the edit: I am totally fine with only |
Vec::retain is a mistake and should not be followed. I prefer keep 'retain' only with '&mut' signature. |
Done :) |
@ivan770 Do you think there are further changes needed? |
Signature looks good now, can't say anything about trait bounds though. |
…imulacrum Implement `Option::take_if` Tracking issue: rust-lang#98934 ACP: rust-lang/libs-team#70 [accepted]
Hi! Any progress on this unstable-feature? |
Hi @cuviper, I can't get a comment on whether it's ready to be stabilized, so I had to ping a member of the libs team! I'd be happy to have them review it. |
The implementation is straightforward, but API goes to a different team -- anyone from @rust-lang/libs-api want to comment? |
I think this is a generally useful utility function that can make some use cases easier to write & more readable. @rfcbot fcp merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
What about
|
I guess replace_if would be useful as well. |
+1 for I've encountered several occasions recently when the implementation of |
This may be starting to cross the "out-of-scope event horizon", but, similarly, I would like to see The pattern of fn maybe_do_a_thing(&mut self) -> Option<ThingStatus> {
...
// Arbitrary operations and state
// maybe capturable in the subsequent closures
...
if self.owned_type
.as_ref()
.some_lazy_calculation()
.is_some_and(predicate) // <-- "collapse" lazy calculation, end immutable borrow
{
owned_type
.somehow_get_mut_ref()
.this_would_be_bad_unless_predicate()
.by_the_way_this_could_also_be_a_lazy_calculation_now() // <-- nice for composable APIs
} else { //
None // I'd rather not see this
} //
} has a habit of cropping up independently in other places. The For instance, when conditionally mutating a shared object behind an Or, when using one-or-more-layers of Or (and I'm not joking), a function that returns an iterator in an I'm a big fan of
You can sort of do this today, with an_option
.is_some_and(predicate)
.then_some(())
.and_then(maybe_returns_none) but The pattern could then turn into fn maybe_do_a_thing(&mut self) -> Option<ThingStatus> {
self.owned_type
.as_ref()
.some_lazy_calculation()
.is_some_and(predicate)
.and_then(|| {
owned_type
.somehow_get_mut_ref()
.this_would_be_bad_unless_predicate()
.by_the_way_this_could_also_be_a_lazy_calculation_now()
})
}
// if self.as_mut().map_or(false, predicate) { self.take() } else { None }
self.as_mut().is_some_and(predicate).and_then(|| self.take) and self.as_mut().is_some_and(predicate).and_then(|| self.replace(value)) So, in a sense, |
Actually, I think The implementation looks like fn and_then<F, T>(self, f: F) -> Option<T> where F: FnOnce() -> Option<T> {
self.then(f).flatten()
} Looks rather more like the |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Stabilize Option::take_if Closes rust-lang#98934 ed: FCP complete in rust-lang#98934 (comment)
Stabilize Option::take_if Closes rust-lang#98934 ed: FCP complete in rust-lang#98934 (comment)
Rollup merge of rust-lang#126089 - wutchzone:option_take_if, r=scottmcm Stabilize Option::take_if Closes rust-lang#98934 ed: FCP complete in rust-lang#98934 (comment)
Feature gate:
#![feature(option_take_if)]
This is a tracking issue for adding
Option::retain_if
similar toOption::take
but requires an additional predicate.It
takes()
the value if the predicate evaluates totrue
. If the predicate evaluates tofalse
, the value of theOption
remainsSome(...)
. The predicate (Fn(&mut T) -> bool
) also has the chance to modify the value.Steps / History
Option::retain
libs-team#70Option::take_if
#98935Unresolved Questions
Provide both immutable and mutable retain methods (keeping it analogue toVec
)?Consensus seems to be to only implement
retain
take_if
but with&mut T
Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: