Skip to content

original key in RustcOccupiedEntry#719

Closed
malezjaa wants to merge 1 commit intorust-lang:mainfrom
malezjaa:rustc_entry_uninserted_key
Closed

original key in RustcOccupiedEntry#719
malezjaa wants to merge 1 commit intorust-lang:mainfrom
malezjaa:rustc_entry_uninserted_key

Conversation

@malezjaa
Copy link
Copy Markdown

@malezjaa malezjaa commented Apr 17, 2026

This is required for rust-lang/rust#155360
Adds an original_key field to RustcOccupiedEntry

Comment thread src/rustc_entry.rs Outdated
@cuviper
Copy link
Copy Markdown
Member

cuviper commented Apr 17, 2026

Note that this used to hold the lookup key, but that was removed in #535.

See also rust-lang/rust#44286

@cuviper
Copy link
Copy Markdown
Member

cuviper commented Apr 17, 2026

I gave some alternate suggestions on rust-lang/rust#155360

@Amanieu
Copy link
Copy Markdown
Member

Amanieu commented Apr 18, 2026

Could you instead change RustcEntry::Occupied to contain 2 values (the key and the OccupiedEntry)? This is much cleaner and avoids issues with other users of OccupiedEntry in the API.

@malezjaa malezjaa force-pushed the rustc_entry_uninserted_key branch 2 times, most recently from 57dcc94 to c36daab Compare April 18, 2026 11:04
@malezjaa malezjaa force-pushed the rustc_entry_uninserted_key branch from c36daab to d49634b Compare April 18, 2026 11:11
Comment thread src/rustc_entry.rs
/// This `enum` is constructed from the [`rustc_entry`] method on [`HashMap`].
///
/// [`rustc_entry`]: HashMap::rustc_entry
pub enum RustcEntry<'a, K, V, A = Global>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tangentially -- I don't think we need any of these RustcEntry methods, because all std does is immediately deconstruct the variants into its own enum. Maybe we could even remove enum RustcEntry altogether and use a Result, like fn rustc_entry(..) -> Result<(Occupied, key), Vacant>.

@clarfonthey
Copy link
Copy Markdown
Contributor

Just so we're on the same page, I was under the impression that RustcEntry and co. were different from the existing entries because the behaviour on std is different from the behaviour here?

One thing that had come to mind was just having multiple entry types based upon what functionality is desired, and having both be public API for this crate so we don't need to make this an internal module.

Not that this move should happen here, just, a potential justification for not just making methods return a key + entry and instead making it part of the entry, if it's going to be needed longer term for this.

@Amanieu
Copy link
Copy Markdown
Member

Amanieu commented Apr 18, 2026

The main reason RustcEntry exists is that the Entry type in the standard library (which dates back to Rust 1.0) is not generic over the hasher, which limits what can be done with it. Notably it means that that in std we have to always call reserve(1) before creating the entry since it is unable to resize the hash table by itself.

@Amanieu
Copy link
Copy Markdown
Member

Amanieu commented Apr 18, 2026

Could you instead change RustcEntry::Occupied to contain 2 values (the key and the OccupiedEntry)? This is much cleaner and avoids issues with other users of OccupiedEntry in the API.

I'm actually having second thoughts about this since it would mean yet another major version bump for hashbrown. @clarfonthey @cuviper what do you think?

@tgross35
Copy link
Copy Markdown
Contributor

Isn't RustcEntry and similar API semver-exempt by being gated behind rustc-internal-api?

@cuviper
Copy link
Copy Markdown
Member

cuviper commented Apr 20, 2026

Does cargo -Zbuild-std always respect the lock file? (edit: seems not) If there's any doubt about that, then I don't think we can just wave away semver issues without having std pinned to an exact version.

An alternative is to add a new method, so we avoid perturbing the performance of the normal entry path at all.

pub fn rustc_try_insert(&mut self, key: K, value: V) -> Result<&mut V, (RustcOccupiedEntry<'_, K, V, A>, K, V)>

Then the parts in the Err can be mapped to std's OccupiedError. We could wrap that in a RustcOccupiedError here if you really want.

@cuviper
Copy link
Copy Markdown
Member

cuviper commented Apr 20, 2026

See #722 for rustc_try_insert -- I think this is a cleaner approach.

@malezjaa malezjaa closed this Apr 20, 2026
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.

5 participants