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 retain_mut #198

Merged
merged 3 commits into from
Jul 5, 2024
Merged

Add retain_mut #198

merged 3 commits into from
Jul 5, 2024

Conversation

Fuuzetsu
Copy link
Contributor

This adds a retain_mut method to ArrayVec and
TinyVec that's just retain but with mutable
references. This method is present on
std::vec::Vec as of a while ago so it's nice to
have in tinyvec too.

Fixes #195

This adds a `retain_mut` method to `ArrayVec` and
`TinyVec` that's just `retain` but with mutable
references. This method is present on
`std::vec::Vec` as of a while ago so it's nice to
have in `tinyvec` too.

Fixes Lokathor#195
@Fuuzetsu
Copy link
Contributor Author

The implementation is just copy-pasted retain with a change to pass in mutable reference in.

@Fuuzetsu
Copy link
Contributor Author

I guess this fails on 1.47 because retain_mut on std::vec::Vec wasn't present yet. I'm not sure how to deal with it.

The options are:

  1. bump MSRV
  2. do not add retain_mut
  3. Use something like https://crates.io/crates/rustversion/ to only add retain_mut for rustc versions where retain_mut is present in the stdlib.

Seems Vec::retain_mut appeared in 1.61.0 according to https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1610-2022-05-19 (2 years ago!)

I guess I'd lean towards the third option?

@Fuuzetsu
Copy link
Contributor Author

I had a look at Cargo.toml, seems there's:

# features that require rustc 1.57
# add try_reserve functions to types that heap allocate.
rustc_1_57 = ["rustc_1_55"]

So perhaps we add rustc_1_61 feature?

@Fuuzetsu
Copy link
Contributor Author

I guess 4th option is to only add retain_mut to ArrayVec as that's manually implemented and not add it to TinyVec but that's sad.

@Lokathor
Copy link
Owner

Adding rustc_1_61 as a new feature seems best. It's very easy for users to understand.

It calls `std::vec::Vec::retain_mut` which is only
available since Rust 1.61. People that still want
to use a very old compiler (CI seems to use 1.47)
still get to compile the crate, they just don't
get `TinyVec::retain_mut`.

Note that `ArrayVec::retain_mut` is implemented
inside the crate so it's still available, just the
`TinyVec` format is not.
@Fuuzetsu
Copy link
Contributor Author

@Lokathor Thanks, I added a commit that adds the feature. Not sure if I have to change something else, like docs features etc. Let me know if I have to change something.

@Lokathor
Copy link
Owner

This seems to pass CI now at least.

Can you add some test cases for this new method? The TinyVec method just calls the version in ArrayVec or Vec, but the new ArrayVec code should have some test cases.

@Fuuzetsu
Copy link
Contributor Author

Fuuzetsu commented Jul 5, 2024

This seems to pass CI now at least.

Can you add some test cases for this new method? The TinyVec method just calls the version in ArrayVec or Vec, but the new ArrayVec code should have some test cases.

I've added some tests. Technically it was tested already as much as retain: via doctest on ArrayVec::retain_mut...; but sure, I added a few tests that demonstrate some obvious cases. Not sure if you're after some specific tests. I wonder if std has some tests for Vec::retain_mut, maybe we should copy-paste those in because semantics should be pretty much the same.

@Lokathor Lokathor merged commit 5097217 into Lokathor:main Jul 5, 2024
5 checks passed
@Lokathor Lokathor added Enhancement New feature or request semver-minor labels Jul 5, 2024
@Lokathor
Copy link
Owner

Lokathor commented Jul 5, 2024

released in tinyvec-1.7.0

@Fuuzetsu Fuuzetsu deleted the retain_mut branch July 7, 2024 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request semver-minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

retain_mut
2 participants