rust-lang / rust-lang/hashbrown
Feature: `HashTable::try_insert_unique_within_capacity`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 3k
- Forks
- 358
- Avg merge
- 11h 57m
- Merged PRs (30d)
- 2
Description
I'm experimenting with no_panic in FFI calls which use HashTable. The goal is to "prove" that the extern "C" function cannot panic, which is a nice goal because it's not allowed to.
I can use HashTable::try_reserve to avoid the panic in practice. However, that's not good enough for no_panic; if the code for panics exists when linking, then no_panic will cause the crate to fail to build. The problem specifically with insert_unique is that it calls RawTable::insert which calls self.reserve(1, hasher), which will panic out of necessity if allocation fails.
So, I propose add HashTable::try_insert_unique_within_capacity which looks like:
pub fn try_insert_unique_within_capacity(
&mut self,
hash: u64,
value: T,
) -> Result<OccupiedEntry<'_, T, A>, T>
And then we remove the feature gate of Edit: reworked to be #[cfg(feature = "rustc-internal-api")] from RawTable::insert_no_grow, or create an equivalent method that's only pub(crate) or something.safe and return Result instead.
This doesn't seem difficult at all--is there maybe a reason it doesn't already exist?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading HashTable::insert_unique, RawTable::insert, RawTable::insert_no_grow, and HashTable::try_reserve to understand the existing capacity and allocation paths. Add the proposed safe Result-returning API, then verify that insertion within capacity avoids the panic-producing path and reports failure through the result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100