Skip to content

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

Merged
bors merged 3 commits into
rust-lang:masterfrom
the-mikedavis:md/hash-table-iter-hash
Sep 2, 2024
Merged

bors merged 3 commits into
rust-lang:masterfrom
the-mikedavis:md/hash-table-iter-hash

Conversation

@the-mikedavis

Copy link
Copy Markdown
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

Amanieu commented Aug 28, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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

Amanieu commented Aug 28, 2024

Copy link
Copy Markdown
Member

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

Amanieu commented Sep 2, 2024

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

📌 Commit 8b60594 has been approved by Amanieu

It is now in the queue for this repository.

@bors

bors commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

⌛ Testing commit 8b60594 with merge a69af93...

@bors

bors commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

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

@bors
bors merged commit a69af93 into rust-lang:master Sep 2, 2024
@the-mikedavis
the-mikedavis deleted the md/hash-table-iter-hash branch September 2, 2024 12:46
@the-mikedavis

Copy link
Copy Markdown
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