Skip to content

Conversation

@tomkris
Copy link

@tomkris tomkris commented Mar 9, 2025

Currently EntryRef::or_insert* methods have constraint K: From<&Q> which is required to construct "owned" key from "borrowed" key during insertion operation.

Rust documentation recommends not to use From as trait constraint, and instead prefer to use reversed Into trait constraint:

https://doc.rust-lang.org/std/convert/trait.From.html

Prefer using Into over using From when specifying trait bounds on a generic function. This way, types that directly implement Into can be used as arguments as well.

Changing constraint K: From<&Q> to &Q: Into<K> extends support of insert operation to additional cases where K does not implement trait From<&Q>, but &Q does implement trait Into<K>.

API compatibility: &Q: Into<K> is a strict superset of K: From<&Q> (because of blanket implementation https://doc.rust-lang.org/std/convert/trait.Into.html#impl-Into%3CU%3E-for-T), so this change does not break existing hashbrown API compatibility; all existing code will work with new trait constraints.

…to &Q: Into<K>

Currently `EntryRef::or_insert*` methods have constraint `K: From<&Q>` which is
required to construct "owned" key from "borrowed" key during insertion operation.

Rust documentation recommends not to use `From` as trait constraint, and instead
prefer to use reversed `Into` trait constraint:

https://doc.rust-lang.org/std/convert/trait.From.html

> Prefer using Into over using From when specifying trait bounds on a generic
> function. This way, types that directly implement Into can be used as arguments as well.

Changing constraint `K: From<&Q>` to `&Q: Into<K>` extends support of insert operation
to additional cases where `K` does not implement trait `From<&Q>`, but `&Q` does implement
trait `Into<K>`.

**API compatibility**: `&Q: Into<K>` is a strict superset of `K: From<&Q>` (because of blanket
implementation https://doc.rust-lang.org/std/convert/trait.Into.html#impl-Into%3CU%3E-for-T),
so this change does not break existing hashbrown API compatibility; all existing code will work
with new trait constraints.
@Amanieu Amanieu added this pull request to the merge queue Apr 5, 2025
Merged via the queue into rust-lang:master with commit 8abce3e Apr 5, 2025
26 checks passed
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