Skip to content

Commit 7002f9f

Browse files
committed
In insert_unique_unchecked return a reference to just inserted key and value
1 parent 8d6c278 commit 7002f9f

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/map.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,8 @@ where
12811281
/// Insert a key-value pair into the map without checking
12821282
/// if the key already exists in the map.
12831283
///
1284+
/// Returns a reference to the key and value just inserted.
1285+
///
12841286
/// This operation is safe if a key does not exist in the map.
12851287
///
12861288
/// However, if a key exists in the map already, the behavior is unspecified:
@@ -1297,10 +1299,13 @@ where
12971299
/// For example, when constructing a map from another map, we know
12981300
/// that keys are unique.
12991301
#[cfg_attr(feature = "inline-more", inline)]
1300-
pub fn insert_unique_unchecked(&mut self, k: K, v: V) {
1302+
pub fn insert_unique_unchecked(&mut self, k: K, v: V) -> (&K, &mut V) {
13011303
let hash = make_insert_hash::<K, S>(&self.hash_builder, &k);
1302-
self.table
1304+
let bucket = self
1305+
.table
13031306
.insert(hash, (k, v), make_hasher::<K, _, V, S>(&self.hash_builder));
1307+
let (k_ref, v_ref) = unsafe { bucket.as_mut() };
1308+
(k_ref, v_ref)
13041309
}
13051310

13061311
/// Tries to insert a key-value pair into the map, and returns
@@ -3926,8 +3931,10 @@ mod test_map {
39263931
#[test]
39273932
fn test_insert_unique_unchecked() {
39283933
let mut map = HashMap::new();
3929-
map.insert_unique_unchecked(10, 11);
3930-
map.insert_unique_unchecked(20, 21);
3934+
let (k1, v1) = map.insert_unique_unchecked(10, 11);
3935+
assert_eq!((&10, &mut 11), (k1, v1));
3936+
let (k2, v2) = map.insert_unique_unchecked(20, 21);
3937+
assert_eq!((&20, &mut 21), (k2, v2));
39313938
assert_eq!(Some(&11), map.get(&10));
39323939
assert_eq!(Some(&21), map.get(&20));
39333940
assert_eq!(None, map.get(&30));

src/set.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ where
993993

994994
/// Insert a value the set without checking if the value already exists in the set.
995995
///
996+
/// Returns a reference to the value just inserted.
997+
///
996998
/// This operation is safe if a value does not exist in the set.
997999
///
9981000
/// However, if a value exists in the set already, the behavior is unspecified:
@@ -1009,8 +1011,8 @@ where
10091011
/// For example, when constructing a set from another set, we know
10101012
/// that values are unique.
10111013
#[cfg_attr(feature = "inline-more", inline)]
1012-
pub fn insert_unique_unchecked(&mut self, value: T) {
1013-
self.map.insert_unique_unchecked(value, ());
1014+
pub fn insert_unique_unchecked(&mut self, value: T) -> &T {
1015+
self.map.insert_unique_unchecked(value, ()).0
10141016
}
10151017

10161018
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given

0 commit comments

Comments
 (0)