-
Notifications
You must be signed in to change notification settings - Fork 165
Implement get_disjoint_mut (previously get_many_mut) #238
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
Conversation
0ef0e90
to
d3f1968
Compare
I think ideally we would want two things for this, especially the first:
I suspect part of this is because you should be using a raw pointer to offset/index each of the elements, otherwise you have the main |
Yeah, this makes a lot of sense. Is it okay to just leave the PR open until then?
Haven't seen this before but it would indeed fit pretty well. It seems to have been open for quite a while without resolution though. Also, if they opt for a result-based return value, that'd not match the hashmap api (as it is now) so we'd have to do conversions in the implementation.
Yeah, exactly, the lambda captures the whole array lifetime mutably and can't be invoked multiple times. I did not consider pointer arithmetic though, I'll experiment a bit and see how it looks. Probably similar to the |
Yeah, that's fine. I should probably make a label for this... |
Can IndexMap add it under a nightly or unstable opt-in cargo feature? Then people will have to opt in to breaking changes, and you can document that changes to this API won't result in a new major version. (Or if you don't feel strongly, you can just make a new major release if the HashMap API changes for some reason.) |
I don't like using nightly/unstable features, in part because it's an extra maintenance burden, but also because the feature may be opted-in on your behalf, arbitrarily deep in your dependency tree. I'd rather not set us up for needing a semver bump either. If this is something that folks really need, let's apply that pressure on |
Rust 1.86 (now in beta) will stabilize this as For pub fn get_disjoint_mut<I, const N: usize>(
&mut self,
indices: [I; N],
) -> Result<[&mut <I as SliceIndex<[T]>>::Output; N], GetDisjointMutError>
where
I: GetDisjointMutIndex + SliceIndex<[T]>, For pub fn get_disjoint_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> [Option<&mut V>; N]
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized, We can decide for ourselves whether it makes sense to build on the standard library, or (unsafely) implement our own. @NiklasJonsson it's been some time -- are you still interested in working on this? |
Either way, I'd be fine with supporting just |
Yeah I've been following the stabilization and I'd like to finish this up.
I'll have some time this weekend to have a deeper look but I think I'll
favor supporting less to start out and then I'll see if I have time to do
more.
…On Tue, Feb 25, 2025, 02:29 Josh Stone ***@***.***> wrote:
Rust 1.86 (now in beta) will stabilize this as get_disjoint_mut with two
different styles:
For [T]:
https://doc.rust-lang.org/beta/std/primitive.slice.html#method.get_disjoint_mut
pub fn get_disjoint_mut<I, const N: usize>(
&mut self,
indices: [I; N],) -> Result<[&mut <I as SliceIndex<[T]>>::Output; N], GetDisjointMutError>where
I: GetDisjointMutIndex + SliceIndex<[T]>,
For HashMap<K, V>:
https://doc.rust-lang.org/beta/std/collections/struct.HashMap.html#method.get_disjoint_mut
pub fn get_disjoint_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],) -> [Option<&mut V>; N]where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
We can decide for ourselves whether it makes sense to build on the
standard library, or (unsafely) implement our own. GetDisjointMutIndex
won't be stable though, nor the old SliceIndex, so if we want to
replicate that support for both usize and ranges, then I think we'll be
on our own anyway.
@NiklasJonsson <https://github.com/NiklasJonsson> it's been some time --
are you still interested in working on this?
—
Reply to this email directly, view it on GitHub
<#238 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADNRXP6VM3HKI4SGOGBRNOD2RPBONAVCNFSM6AAAAABXZNCFMWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOBQGEZDQMRTGI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
[image: cuviper]*cuviper* left a comment (indexmap-rs/indexmap#238)
<#238 (comment)>
Rust 1.86 (now in beta) will stabilize this as get_disjoint_mut with two
different styles:
For [T]:
https://doc.rust-lang.org/beta/std/primitive.slice.html#method.get_disjoint_mut
pub fn get_disjoint_mut<I, const N: usize>(
&mut self,
indices: [I; N],) -> Result<[&mut <I as SliceIndex<[T]>>::Output; N], GetDisjointMutError>where
I: GetDisjointMutIndex + SliceIndex<[T]>,
For HashMap<K, V>:
https://doc.rust-lang.org/beta/std/collections/struct.HashMap.html#method.get_disjoint_mut
pub fn get_disjoint_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],) -> [Option<&mut V>; N]where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
We can decide for ourselves whether it makes sense to build on the
standard library, or (unsafely) implement our own. GetDisjointMutIndex
won't be stable though, nor the old SliceIndex, so if we want to
replicate that support for both usize and ranges, then I think we'll be
on our own anyway.
@NiklasJonsson <https://github.com/NiklasJonsson> it's been some time --
are you still interested in working on this?
—
Reply to this email directly, view it on GitHub
<#238 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADNRXP6VM3HKI4SGOGBRNOD2RPBONAVCNFSM6AAAAABXZNCFMWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOBQGEZDQMRTGI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Sounds great, thanks! And please don't take that as any pressure to do this quickly -- we can take our time and get it right. |
Correcting myself -- this should be plural, so now it would be |
@cuviper Just to verify that I understand what you mean. An alright initial implementation would be:
and then we can leave the implementation of |
9f800a0
to
c6f35fd
Compare
I went ahead and implemented what I understood you to mean 🙂 Also added some extra tests. |
3ec4a1b
to
11017c9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, Rust 1.86.0 shipped for us to match API, and I pushed the last few review suggestions myself. I think this is ready to go -- thank you for your contribution!
Thanks, happy to help! |
std::collections::HashMap
is addingget_many_mut
in rust-lang/rust#97601 and I'm working on replacing some uses of that hash map with this one where some of the code usesget_many_mut
(currently only a single location in rustc) so I would appreciate it ifindexmap::IndexMap
could provide the same interface.This code uses a decent amount of unsafe compared to the rest of the file and I saw the comment in lib.rs on having almost all unsafe code in raw.rs but I couldn't figure out how to move parts of my implementation there as it is mostly dealing with initializing the array. I tried to use
array::map
to avoid all unsafe but couldn't quite get it to work with the lifetimes and the lambda.If you want this change, I'll also write some docs.