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

wip: allow providing the key at insertion time for EntryRef #579

Draft
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");

pub fn insert_kv(self, key: K, value: V) -> &'map mut V
where
K: Hash,
for<'k> &'k K: PartialEq<&'key Q>,
Copy link
Member

Choose a reason for hiding this comment

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

I think this should use Q: Equivalent<K> instead, just like entry_ref did when it was created.

Copy link
Author

Choose a reason for hiding this comment

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

ah yes that covers exactly what is needed there. had not seen that trait so far and tbh i have not read a lot of hashbrowns code, i just needed one of those methods to avoid either copying my key more than needed or doing a double lookup.

where
K: Hash + From<&'b Q>,
K: Hash + From<&'key Q>,
Copy link
Member

Choose a reason for hiding this comment

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

Not your problem, but this is an issue too, since downstream Q might implement a bad From<&Q> for K.

Copy link
Author

Choose a reason for hiding this comment

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

yeah i noticed that too, that was half the reason i asked about the assert

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

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.

2 participants