This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
library/std/src/collections/hash Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -868,7 +868,9 @@ where
868868 /// Returns whether the value was newly inserted. That is:
869869 ///
870870 /// - If the set did not previously contain this value, `true` is returned.
871- /// - If the set already contained this value, `false` is returned.
871+ /// - If the set already contained this value, `false` is returned,
872+ /// and the set is not modified: original value is not replaced,
873+ /// and the value passed as argument is dropped.
872874 ///
873875 /// # Examples
874876 ///
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use super::HashSet;
33
44use crate :: panic:: { catch_unwind, AssertUnwindSafe } ;
55use crate :: sync:: atomic:: { AtomicU32 , Ordering } ;
6+ use crate :: sync:: Arc ;
67
78#[ test]
89fn test_zero_capacities ( ) {
@@ -502,3 +503,22 @@ fn const_with_hasher() {
502503 const X : HashSet < ( ) , ( ) > = HashSet :: with_hasher ( ( ) ) ;
503504 assert_eq ! ( X . len( ) , 0 ) ;
504505}
506+
507+ #[ test]
508+ fn test_insert_does_not_overwrite_the_value ( ) {
509+ let first_value = Arc :: new ( 17 ) ;
510+ let second_value = Arc :: new ( 17 ) ;
511+
512+ let mut set = HashSet :: new ( ) ;
513+ let inserted = set. insert ( first_value. clone ( ) ) ;
514+ assert ! ( inserted) ;
515+
516+ let inserted = set. insert ( second_value) ;
517+ assert ! ( !inserted) ;
518+
519+ assert ! (
520+ Arc :: ptr_eq( set. iter( ) . next( ) . unwrap( ) , & first_value) ,
521+ "Insert must not overwrite the value, so the contained value pointer \
522+ must be the same as first value pointer we inserted"
523+ ) ;
524+ }
You can’t perform that action at this time.
0 commit comments