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 more safe methods to RawTable #202

Merged
merged 1 commit into from
Sep 28, 2020

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Sep 12, 2020

These methods combine lookup and bucket operations so the caller doesn't
have to deal with unsafe bucket methods.

  • get: find and as_ref
  • get_mut: find and as_mut
  • insert_entry: insert and as_mut
  • remove_entry: find and remove
  • erase_entry: find and erase

These methods combine lookup and bucket operations so the caller doesn't
have to deal with unsafe bucket methods.

- `get`: `find` and `as_ref`
- `get_mut`: `find` and `as_mut`
- `insert_entry`: `insert` and `as_mut`
- `remove_entry`: `find` and `remove`
- `erase_entry`: `find` and `erase`
@Amanieu
Copy link
Member

Amanieu commented Sep 13, 2020

What is the rationale for adding these methods instead of just using the safe API in HashMap directly?

I have been considering going in the other direction: exposing more functionality in HashMap so that we can eventually deprecate RawTable.

@cuviper
Copy link
Member Author

cuviper commented Sep 15, 2020

I was motivated for this by the cached crate's SizedCache, which was using HashMap<K, usize> and LRUList<(K, V)>, where the latter is indexable by the usize values from the map. I thought it would be nice to remove the K duplication by using RawTable<usize> instead of the map, reading the keys from the list -- jaemk/cached#59. However, dealing with Bucket would introduce the first unsafe in that crate, avoided by this PR's safe methods.

That proposed use case is quite a bit like indexmap, where key storage is managed independently. Another example I've been playing with is this autohash crate, where keys can report their own "hash" without actually going through a Hasher, as a better solution than rust-lang/rust#76233 when the key is already hash-like. So I think RawTable has really nice flexibility, and I'm skeptical that the same uses could be served by HashMap extensions.

@cuviper
Copy link
Member Author

cuviper commented Sep 15, 2020

If anything, I might like to see RawTable pulled out into its own crate, for cases where the map/set implementations are not used. That could be forked out of hashbrown, but it would be nice to continue sharing that implementation.

@cuviper
Copy link
Member Author

cuviper commented Sep 28, 2020

@Amanieu -- any more thoughts on this? We can close if you think it's not viable, but it seems simple enough to me.

@Amanieu
Copy link
Member

Amanieu commented Sep 28, 2020

Sorry, this slipped my mind. I think these are probably fine to add as is.

@Amanieu Amanieu merged commit d2b5aec into rust-lang:master Sep 28, 2020
@cuviper
Copy link
Member Author

cuviper commented Sep 28, 2020

And I see you also published 0.9.1, thanks!

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