Skip to content

Commit f3e266e

Browse files
author
Artem Kryvokrysenko
committed
Relax constraint in hash_map::EntryRef insertion methods K: From<&Q> 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.
1 parent b5b0655 commit f3e266e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/map.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,7 +4131,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> {
41314131
#[cfg_attr(feature = "inline-more", inline)]
41324132
pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
41334133
where
4134-
K: Hash + From<&'b Q>,
4134+
K: Hash,
4135+
&'b Q: Into<K>,
41354136
S: BuildHasher,
41364137
{
41374138
match self {
@@ -4164,7 +4165,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> {
41644165
#[cfg_attr(feature = "inline-more", inline)]
41654166
pub fn or_insert(self, default: V) -> &'a mut V
41664167
where
4167-
K: Hash + From<&'b Q>,
4168+
K: Hash,
4169+
&'b Q: Into<K>,
41684170
S: BuildHasher,
41694171
{
41704172
match self {
@@ -4194,7 +4196,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> {
41944196
#[cfg_attr(feature = "inline-more", inline)]
41954197
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V
41964198
where
4197-
K: Hash + From<&'b Q>,
4199+
K: Hash,
4200+
&'b Q: Into<K>,
41984201
S: BuildHasher,
41994202
{
42004203
match self {
@@ -4225,7 +4228,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> {
42254228
#[cfg_attr(feature = "inline-more", inline)]
42264229
pub fn or_insert_with_key<F: FnOnce(&Q) -> V>(self, default: F) -> &'a mut V
42274230
where
4228-
K: Hash + Borrow<Q> + From<&'b Q>,
4231+
K: Hash + Borrow<Q>,
4232+
&'b Q: Into<K>,
42294233
S: BuildHasher,
42304234
{
42314235
match self {
@@ -4320,7 +4324,8 @@ impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V
43204324
#[cfg_attr(feature = "inline-more", inline)]
43214325
pub fn or_default(self) -> &'a mut V
43224326
where
4323-
K: Hash + From<&'b Q>,
4327+
K: Hash,
4328+
&'b Q: Into<K>,
43244329
S: BuildHasher,
43254330
{
43264331
match self {
@@ -4368,7 +4373,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43684373
#[cfg_attr(feature = "inline-more", inline)]
43694374
pub fn insert(self, value: V) -> &'a mut V
43704375
where
4371-
K: Hash + From<&'b Q>,
4376+
K: Hash,
4377+
&'b Q: Into<K>,
43724378
S: BuildHasher,
43734379
{
43744380
let table = &mut self.table.table;
@@ -4399,7 +4405,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43994405
#[cfg_attr(feature = "inline-more", inline)]
44004406
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
44014407
where
4402-
K: Hash + From<&'b Q>,
4408+
K: Hash,
4409+
&'b Q: Into<K>,
44034410
S: BuildHasher,
44044411
{
44054412
let elem = self.table.table.insert(

0 commit comments

Comments
 (0)