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

Add HashTable::iter_hash, HashTable::iter_hash_mut #549

Merged
merged 3 commits into from
Sep 2, 2024

Conversation

the-mikedavis
Copy link
Contributor

This is a follow-up to #546 (comment). iter_hash from the old raw API can be useful for reading from a "bag" / "multi map" type which allows duplicate key-value pairs. Exposing it safely in HashTable takes a fairly small wrapper around RawIterHash. This PR partially reverts #546 to restore RawTable::iter_hash and its associated types.

This partially reverts 26ef4a1 so that
the child commit can expose a safe `iter_hash` in
`HashTable::iter_hash`.
This is a safe wrapper around `RawTable::iter_hash`. `iter_hash` can be
useful for looking up duplicate values in the table. For example you
might use it to build a "bag" / "multi map" type which blindly inserts
with `HashTable::insert_unique` and allows lookup of multiple values for
the same key.
@Amanieu
Copy link
Member

Amanieu commented Aug 28, 2024

Should there be a mut version of this iterator? What about removing one item out of multiple with the same key? Do we also need to add APIs for those use cases?

@the-mikedavis
Copy link
Contributor Author

For my use-case I will only need to read entries as-is after insertion. I could see mutation being useful hypothetically though. Maybe that could benefit from some kind of iterator that returns Entry-like items?

@Amanieu
Copy link
Member

Amanieu commented Aug 28, 2024

Returning Entry doesn't work because you then end up with multiples Entrys each of which has a mutable reference to the table, which violates aliasing rules. I think just adding iter_hash_mut for now would be enough.

@the-mikedavis the-mikedavis force-pushed the md/hash-table-iter-hash branch from ab005f6 to 8b60594 Compare August 28, 2024 23:19
@the-mikedavis the-mikedavis changed the title Add HashTable::iter_hash Add HashTable::iter_hash, HashTable::iter_hash_mut Aug 28, 2024
@Amanieu
Copy link
Member

Amanieu commented Sep 2, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Sep 2, 2024

📌 Commit 8b60594 has been approved by Amanieu

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 2, 2024

⌛ Testing commit 8b60594 with merge a69af93...

@bors
Copy link
Contributor

bors commented Sep 2, 2024

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing a69af93 to master...

@bors bors merged commit a69af93 into rust-lang:master Sep 2, 2024
24 checks passed
@the-mikedavis the-mikedavis deleted the md/hash-table-iter-hash branch September 2, 2024 12:46
@the-mikedavis
Copy link
Contributor Author

Thanks @Amanieu! My multi map type is much cleaner as a HashTable now :)

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.

3 participants