Skip to content

allow providing the key at insertion time for EntryRef #579

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

djugei
Copy link

@djugei djugei commented Oct 17, 2024

Either explicitly through insert_kv or implicitly through insert_clone.

I have added an assert in insert_kv just to be sure, but if it is only a logic error to insert a different key at an entry or is caught deeper in the stack it should be removed/changed to debug_assert.

If this is something the maintainers are interested in i can implement the EntryRef and OccupiedEntryRef variants and document them.

@djugei djugei marked this pull request as draft October 17, 2024 08:09
@cuviper
Copy link
Member

cuviper commented Oct 17, 2024

I have added an assert in insert_kv just to be sure, but if it is only a logic error to insert a different key at an entry or is caught deeper in the stack it should be removed/changed to debug_assert.

I expect we do want a full assert, just like we did in HashSet::get_or_insert_with:

assert!(value.equivalent(&new), "new value is not equivalent");

@djugei
Copy link
Author

djugei commented Oct 18, 2024

i think i will drop the insert_clone as insert_kv already covers the usecase.
if passing the wrong item is just a logic error i do think that using debug_assert is the correct move, though it is somewhat likely for the compiler to be smart even about the full assert.

should i make the full pr?

@cuviper
Copy link
Member

cuviper commented Oct 18, 2024

I'll defer to @Amanieu to decide on the API design.

@djugei djugei force-pushed the master branch 2 times, most recently from f705fc8 to afe3b0c Compare February 5, 2025 20:50
@djugei
Copy link
Author

djugei commented Feb 5, 2025

i disagree with clippy on this one.

@djugei
Copy link
Author

djugei commented Jun 3, 2025

@Amanieu can i get some feedback on the api design here? i would like to start depending on the main hashbrown again.

this saves clones when trying to insert in a loop.

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

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

Overall, I think the API additions make sense. I've added some comments on the specific.

And finally, the docs will need to be improved.

@djugei djugei force-pushed the master branch 2 times, most recently from 8bfe87c to 760eb3e Compare June 7, 2025 16:44
@djugei
Copy link
Author

djugei commented Jun 7, 2025

EntryRef already has or_insert_with_key which is a bit of a name collision, so i am for now only adding the functionality to VacantEntryRef.

@djugei djugei marked this pull request as ready for review June 7, 2025 16:46
@djugei
Copy link
Author

djugei commented Jun 7, 2025

@Amanieu i have integrated the suggestions. Unsure if the docs are enough, i mainly point to the regular insert docs instead of duplicating.

This should be ready to merge now i think, i have rebased it on top of master.

@djugei djugei changed the title wip: allow providing the key at insertion time for EntryRef allow providing the key at insertion time for EntryRef Jun 7, 2025
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.

4 participants