Skip to content
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 Vec::retain_mut #34265

Closed
wants to merge 1 commit into from
Closed

Conversation

shepmaster
Copy link
Member

No description provided.

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

@shepmaster
Copy link
Member Author

I really wanted to just change Vec::retain's closure to accept a &mut T, but that is sadly backwards incompatible. It's a shame that you can't pattern-match a mutable reference with just a &.

Since I had the main bulk of the work, I figured I'd send in a PR and see what we thought of it. Does something like this require a RFC, or is it tiny enough to sneak in?

@shepmaster
Copy link
Member Author

For reference, this was originally spurred from this Stack Overflow question. Conceptually, if you have a Vec<Monster> and want to apply some damage to all the monsters and remove those with 0 health, you only need to iterate once through the vector.

@@ -684,6 +684,44 @@ impl<T> Vec<T> {
}
}

/// Retains only the elements specified by the predicate.
///
/// In other words, remove all elements `e` such that `f(&e)` returns false.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be f(&mut e).

@TimNN
Copy link
Contributor

TimNN commented Jun 14, 2016

Since retain_mut is strictly more powerful than retain, I believe it should be possible to implement retain in terms of retain_mut, to reduce code duplication.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 14, 2016

this has been discussed: #25477
and has an RFC: rust-lang/rfcs#1353

@shepmaster
Copy link
Member Author

possible to implement retain in terms of retain_mut

I believe you'd have to make an intermediate form that abstracts over the mutability. Otherwise in one direction you give up the mutability and the other direction needs to manifest mutability out of thin air.

this has [...] an RFC

@oli-obk Ah, thanks! Obviously my searching was not very thorough at all..

Since the implementation isn't the hard part, I'm going to close this as the RFC shows that there's much more to discuss.

@shepmaster shepmaster closed this Jun 14, 2016
@TimNN
Copy link
Contributor

TimNN commented Jun 14, 2016

@shepmaster couldn't you just define retain(&mut self, mut f) as self.retain_mut(|e| f(e))?

@shepmaster
Copy link
Member Author

@TimNN Hmm, quite right. Not sure what I was thinking 😸

@shepmaster shepmaster deleted the retain-mut branch September 24, 2016 13:58
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc `@m-ou-se` `@jonas-schievink` `@Mark-Simulacrum`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc ``@m-ou-se`` ``@jonas-schievink`` ``@Mark-Simulacrum``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc ```@m-ou-se``` ```@jonas-schievink``` ```@Mark-Simulacrum```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc ````@m-ou-se```` ````@jonas-schievink```` ````@Mark-Simulacrum````
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 17, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc `````@m-ou-se````` `````@jonas-schievink````` `````@Mark-Simulacrum`````
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 17, 2021
…shtriplett

Add Vec::retain_mut

This is to continue the discussion started in rust-lang#83218.

Original comment was:

> Take 2 of rust-lang#34265, since I needed this today.

The reason I think why we should add `retain_mut` is for coherency and for discoverability. For example we have `chunks` and `chunks_mut` or `get` and `get_mut` or `iter` and `iter_mut`, etc. When looking for mutable `retain`, I would expect `retain_mut` to exist. It took me a while to find out about `drain_filter`. So even if it provides an API close to `drain_filter`, just for the discoverability, I think it's worth it.

cc ``````@m-ou-se`````` ``````@jonas-schievink`````` ``````@Mark-Simulacrum``````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants