-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 vec::Drain{,Filter}::keep_rest
#101122
Comments
Why was this closed? The feature is still in nightly according to the docs, https://doc.rust-lang.org/1.78.0/std/vec/struct.Drain.html#method.keep_rest Is there a separate ticket for Drain specifically? If not, is there some way to stabilize |
On one of the unresolved issues:
This is now no longer unresolved, as it was handled in #104455
I think this is the best option, since it will also work if iteration panics in some way. Setting the flag is equivalent to just having
This would require setting a flag, and also handles panics, but I don't see any reason to set the flag some time after construction. And it's quite easy to add a flag on top of a struct Drain<'a, T> {
should_drain_on_drop: bool,
drain: std::vec::Drain<'a, T, false>, // << with const-generic param to control drop behavior
}
impl<'a, T> Drop for Drain<'a, T> {
fn drop(&mut self) {
if self.should_drain_on_drop {
// optionally user a scope guard to guarantee that all items get dropped, but that's not to hard to add as well
self.for_each(drop);
}
}
}
impl Iterator for Drain<'a, T> { /* forward to std::vec::Drain */ } so this is also easy to do. So all in all, I think the const-generics approach remains the most flexible, and correct even in the face of panics. |
We briefly discussed this in the libs-api meeting today. We would like to defer stabilizing this until |
There is also |
IMO |
I’d like to make one observation here, so it isn’t possibly missed in the future: (I haven’t seen it mentioned anywhere around here yet) For The relevant context:
So stabilizing (Either method on its own is fine though; |
Feature gate:
#![feature(drain_keep_rest)]
This is a tracking issue for
vec::Drain::keep_rest
, methods allowing to keep elements in aVec
after draining some of them.Public API
Steps / History
vec::Drain{,Filter}::keep_rest
#95376Unresolved Questions
.drain_filter(f).take(N)
works as expectedDrop
forDrain
drain_filter
) then you'd need to write.foreach(drop)
to explicitly drop all the rest of the range that matches the filter.&mut self
instead ofself
?Drain
inside a struct and are operating on it from a&mut self
method of the struct,keep_rest(self)
is impossible to use. :(mem::replace(&mut self.drain_filter, Vec::new().drain(..)).keep_rest()
but the borrow checker won't like that.Drain
field toOption<Drain>
and useself.drain_filter.take().unwrap().keep_rest()
along withunwrap()
everywhere else that the drain is used. Not good..next()
returnsNone
.next()
panic!
s.keep_rest()
sets a flag inside theDrain
whichDrop
will use to decide its behavior, and you're free to continue accessing iterator elements in between.const EXHAUST_ON_DROP: bool = true
const generic parameter&mut Drain<'a, T, A, false>
from the beginningFootnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: