hashbrown icon indicating copy to clipboard operation
hashbrown copied to clipboard

wip: allow providing the key at insertion time for EntryRef

Open djugei opened this issue 1 year ago • 4 comments

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 avatar Oct 17 '24 08:10 djugei

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: https://github.com/rust-lang/hashbrown/blob/eea980411a75c5229a562f9543fe1b035ad384c9/src/set.rs#L957

cuviper avatar Oct 17 '24 17:10 cuviper

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?

djugei avatar Oct 18 '24 07:10 djugei

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

cuviper avatar Oct 18 '24 14:10 cuviper

i disagree with clippy on this one.

djugei avatar Feb 05 '25 20:02 djugei

@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.

djugei avatar Jun 03 '25 08:06 djugei

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 avatar Jun 07 '25 16:06 djugei

@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 avatar Jun 07 '25 16:06 djugei

expanded the comment by adding 2 doctests and added a descriptive panic message to the assert

djugei avatar Nov 18 '25 10:11 djugei

I've cleaned up the doc comment a bit. Thanks for working on this!

Amanieu avatar Nov 19 '25 00:11 Amanieu

oh yeah that is much better documentation!

djugei avatar Nov 19 '25 11:11 djugei