-
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
Vec::retain could (should?) give &mut T
to the closure, not just &T
#25477
Comments
Speaking in context of rust-lang/rfcs#1105 this is a breaking change, but probably qualifies as a minor change. It might be interesting to pull it into the discussion as an example. For the idea itself, it sounds sound and useful. |
Oh, right. I was thinking of using this method with a closure with inferred parameter types, but it is indeed breaking if you have type annotations. From the RFC:
This case seems unusual since using the more implicit form is the change you could do in advance, but I suppose it qualifies. |
I am +1 for this change in principle, but slightly worried about potential breakage. It's probably worth prototyping and testing out with @brson's crater tool. |
If this causes breakage, an alternative would be to add a separate |
Changing the parameter to &mut breaks uses of One example in rustc:
|
I'm not a huge fan of doing this. We made a bad call. That sucks, but this is also why we expose the guts of Vec. |
It's interesting in general that we could choose to give out Sounds like the resolution shall be a separate API @gankro, |
Another case is |
retain_mut is simple to lift out (retain uses only public api). I've put retain_mut into the crate odds. I've suggested we have some kind of gathering of more-than-libstd collections API in a crate in contain-rs, no other interested people have said anything in that discussion yet. |
Given that |
I wrote an RFC for |
I'm ok with this change. |
Triage: as time goes on here, I don't think that we can make this change, as the breakage is much more significant than in the few days post 1.0. I'm going to give it a close. |
So what is planned to remove elements based on predicates and mutate the elements? #34265 has been closed in favor of this issue, but this issue has been closed too. |
Drain filter is going to cover that. |
If I do understand, |
Well, that's up to your judgement. Existing retain usages will continue to work, which has a value. |
Also,
|
Existing drain_filter impl does not need to be driven explicitly -- it is driven in drop, too. It makes panic behaviour interesting but it is similar to .drain(). |
Ah, I guess that makes sense. |
FYI, I just published a crate |
I've read all the comments, but still confused about what to do, that's not good! Ideally there would be a pinned comment or something with current best solution. I see that there are two crates |
@cheako You can also use |
@Boscop |
85: Replace Vec remove by a retain_mut function in Container operations r=Kerollmops a=Kerollmops We introduce a `retain_mut` function that allows us to mutate the iterated element. There already were [a discussion about this `Vec::retain` parameter restriction](rust-lang/rust#25477). Using the `retain` algorithm is much better when multiple stores must be removed in one batch, same as in #83. Co-authored-by: Clément Renault <[email protected]>
For reference, |
85: Replace Vec remove by a retain_mut function in Container operations r=Kerollmops a=Kerollmops We introduce a `retain_mut` function that allows us to mutate the iterated element. There already were [a discussion about this `Vec::retain` parameter restriction](rust-lang/rust#25477). Using the `retain` algorithm is much better when multiple stores must be removed in one batch, same as in RoaringBitmap#83. Co-authored-by: Clément Renault <[email protected]>
Vec<T>
has this method:However, looking at its implementation, I don’t see a reason why the
&T
part couldn’t be&mut T
, allowing in-place mutation of retained items. This would make the method more useful. The only downside that I can see is that the name does not reflect this new capability of the method. I believe this change would be backward-compatible.This came up at today’s meetup where someone have a
Vec<Item>
and wanted (but didn’t know how) to iterate the vector, decreasing some quantity insideItem
but also removing items from the vector when the quantity reaches zero.CC @aturon
The text was updated successfully, but these errors were encountered: