diff --git a/src/weak_key_hash_map.rs b/src/weak_key_hash_map.rs index 7dcb8c4..307b772 100644 --- a/src/weak_key_hash_map.rs +++ b/src/weak_key_hash_map.rs @@ -715,6 +715,24 @@ impl<'a, K: WeakKey, V> Entry<'a, K, V> { } } + /// Ensures a value is in the entry by inserting, if empty, the result of the default function. + /// This method allows for generating key-derived values for insertion by providing the default + /// function a reference to the key that was moved during the `.entry(key)` method call. + /// + /// The reference to the moved key is provided so that cloning or copying the key is + /// unnecessary, unlike with `.or_insert_with(|| ... )`. + /// + /// *O*(1) time + pub fn or_insert_with_key V>(self, default: F) -> &'a mut V { + match self { + Entry::Occupied(occupied) => occupied.into_mut(), + Entry::Vacant(vacant) => { + let value = default(vacant.key()); + vacant.insert(value) + }, + } + } + /// Returns a reference to this entry's key. /// /// *O*(1) time diff --git a/src/weak_value_hash_map.rs b/src/weak_value_hash_map.rs index 7177e41..1816724 100644 --- a/src/weak_value_hash_map.rs +++ b/src/weak_value_hash_map.rs @@ -612,6 +612,24 @@ impl<'a, K, V: WeakElement> Entry<'a, K, V> { } } + /// Ensures a value is in the entry by inserting, if empty, the result of the default function. + /// This method allows for generating key-derived values for insertion by providing the default + /// function a reference to the key that was moved during the `.entry(key)` method call. + /// + /// The reference to the moved key is provided so that cloning or copying the key is + /// unnecessary, unlike with `.or_insert_with(|| ... )`. + /// + /// *O*(1) time + pub fn or_insert_with_key V::Strong>(self, default: F) -> V::Strong { + match self { + Entry::Occupied(occupied) => occupied.get_strong(), + Entry::Vacant(vacant) => { + let value = default(vacant.key()); + vacant.insert(value) + }, + } + } + /// Returns a reference to this entry's key. /// /// *O*(1) time diff --git a/src/weak_weak_hash_map.rs b/src/weak_weak_hash_map.rs index 801bc64..5e834b9 100644 --- a/src/weak_weak_hash_map.rs +++ b/src/weak_weak_hash_map.rs @@ -621,6 +621,24 @@ impl<'a, K: WeakKey, V: WeakElement> Entry<'a, K, V> { } } + /// Ensures a value is in the entry by inserting, if empty, the result of the default function. + /// This method allows for generating key-derived values for insertion by providing the default + /// function a reference to the key that was moved during the `.entry(key)` method call. + /// + /// The reference to the moved key is provided so that cloning or copying the key is + /// unnecessary, unlike with `.or_insert_with(|| ... )`. + /// + /// *O*(1) time + pub fn or_insert_with_key V::Strong>(self, default: F) -> V::Strong { + match self { + Entry::Occupied(occupied) => occupied.get_strong(), + Entry::Vacant(vacant) => { + let value = default(vacant.key()); + vacant.insert(value) + }, + } + } + /// Returns a reference to this entry's key. /// /// *O*(1) time