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

Support get_or_insert_with #355

Open
p4l1ly opened this issue Oct 21, 2024 · 4 comments
Open

Support get_or_insert_with #355

p4l1ly opened this issue Oct 21, 2024 · 4 comments

Comments

@p4l1ly
Copy link

p4l1ly commented Oct 21, 2024

Could you please support something like this? https://docs.rs/hashbrown/latest/hashbrown/struct.HashSet.html#method.get_or_insert_with

@cuviper
Copy link
Member

cuviper commented Oct 21, 2024

I would prefer to wait for the std version to stabilize, so we can make sure to match its final API.
rust-lang/rust#60896

(I authored that initially, but anyone could chase that to completion.)

@cuviper
Copy link
Member

cuviper commented Oct 21, 2024

It's worth mentioning that the version by value, get_or_insert, is trivial with IndexSet::insert_full and then &set[i]. I'm sure we'll add that method too when the time comes, but it won't be any better than doing that yourself.

@p4l1ly
Copy link
Author

p4l1ly commented Oct 22, 2024

My use case is kind of a micro-optimisation when I want to insert a possibly big borrowed value into the set. For efficiency reasons, I want to clone it only if it is not present. But IndexSet::insert_full takes ownership, so I have to clone it also if it is not stored. With get_or_insert_with I could do set.get_or_insert_with(value, |v| v.clone()).

@cuviper
Copy link
Member

cuviper commented Oct 22, 2024

Yeah, understood. Another potential workaround is to make the clone cheaper, like wrapping the expensive part in Rc/Arc so it can be shared, but that's not always feasible.

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

No branches or pull requests

2 participants