![](/style/images/good.png)
![](/style/images/bad.png)
Add `vec::Drain{,Filter}::keep_rest` by WaffleLapkin · Pull Request #95376 · rus...
source link: https://github.com/rust-lang/rust/pull/95376
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
This PR adds keep_rest
methods to vec::Drain
and vec::DrainFilter
under drain_keep_rest
feature gate:
// mod alloc::vec
impl<T, A: Allocator> Drain<'_, T, A> {
pub fn keep_rest(self);
}
impl<T, F, A: Allocator> DrainFilter<'_, T, F, A>
where
F: FnMut(&mut T) -> bool,
{
pub fn keep_rest(self);
}
Both these methods cancel draining of elements that were not yet yielded from the iterators. While this needs more testing & documentation, I want at least start the discussion. This may be a potential way out of the "should DrainFilter
exhaust itself on drop?" argument.
Collaborator
rust-highfive commented on Mar 27
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label
I haven't looked at the code, but I think this is a better answer to 90% of what |
Contributor
Author
WaffleLapkin commented on Mar 27
Truth be told, when I struggled with the default behavior of |
|
Member
JohnCSimon commented on May 8
Ping from triage: |
I think this should be the default for
Perhaps adding a defaulted const generic |
added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
and removed T-libs Relevant to the library team, which will review and decide on the PR/issue.
labels
|
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
Member
dtolnay
left a comment
I am on board with this API. Next steps are:
- I think this is the kind of method that would benefit from a usage example in the rustdoc.
- Needs a tracking issue.
Some considerations to note in the tracking issue for discussion before we could stabilize:
- Just change the not-yet-stable
Drop for DrainFilter
to keep rest?- Advantage: usually what you want (??)
- I don't have a good picture what the use cases involve drain_filter and not exhausting the iterator obtained from it.
- Disadvantage: inconsistent with stable
Drop for Drain
- If you want to remove rest (matching the current unstable behavior of drain_filter) then you'd need to write
.foreach(drop)
to explicitly drop all the rest of the range that matches the filter.
- Advantage: usually what you want (??)
&mut self
instead ofself
?- If you're holding a
Drain
inside a struct and are operating on it from a&mut self
method of the struct,keep_rest(self)
is impossible to use. :(- You'd want something like
mem::replace(&mut self.drain_filter, Vec::new().drain(..)).keep_rest()
but the borrow checker won't like that. - Failing that, you'd need to change your
Drain
field toOption<Drain>
and useself.drain_filter.take().unwrap().keep_rest()
along withunwrap()
everywhere else that the drain is used. Not good.
- You'd want something like
- We'd need to define behavior of calling
.next()
after.keep_rest()
. Presumably one of:.next()
returnsNone
- this is considered incorrect usage and so
.next()
panicks .keep_rest()
sets a flag inside the Drain which Drop will use to decide its behavior, and you're free to continue accessing iterator elements in between.
- If you're holding a
Contributor
Author
WaffleLapkin commented 11 days ago
I've updated docs and the tracking issue |
Member
dtolnay commented 10 days ago
@bors r+ |
Contributor
bors commented 10 days ago
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Successfully merging this pull request may close these issues.
None yet
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK