@@ -2578,7 +2578,7 @@ fn assert_covariance() {
25782578
25792579#[ cfg( test) ]
25802580mod test_set {
2581- use super :: HashSet ;
2581+ use super :: { make_hash , Equivalent , HashSet } ;
25822582 use crate :: DefaultHashBuilder ;
25832583 use std:: vec:: Vec ;
25842584
@@ -3045,4 +3045,57 @@ mod test_set {
30453045 // (and without the `map`, it does not).
30463046 let mut _set: HashSet < _ > = ( 0 ..3 ) . map ( |_| ( ) ) . collect ( ) ;
30473047 }
3048+
3049+ #[ test]
3050+ fn duplicate_insert ( ) {
3051+ let mut set = HashSet :: new ( ) ;
3052+ set. insert ( 1 ) ;
3053+ set. get_or_insert_with ( & 1 , |_| 1 ) ;
3054+ set. get_or_insert_with ( & 1 , |_| 1 ) ;
3055+ assert ! ( [ 1 ] . iter( ) . eq( set. iter( ) ) ) ;
3056+ }
3057+
3058+ #[ test]
3059+ #[ should_panic]
3060+ fn some_invalid_equivalent ( ) {
3061+ use core:: hash:: { Hash , Hasher } ;
3062+ struct Invalid {
3063+ count : u32 ,
3064+ other : u32 ,
3065+ }
3066+
3067+ struct InvalidRef {
3068+ count : u32 ,
3069+ other : u32 ,
3070+ }
3071+
3072+ impl PartialEq for Invalid {
3073+ fn eq ( & self , other : & Self ) -> bool {
3074+ self . count == other. count && self . other == other. other
3075+ }
3076+ }
3077+ impl Eq for Invalid { }
3078+
3079+ impl Equivalent < Invalid > for InvalidRef {
3080+ fn equivalent ( & self , key : & Invalid ) -> bool {
3081+ self . count == key. count && self . other == key. other
3082+ }
3083+ }
3084+ impl Hash for Invalid {
3085+ fn hash < H : Hasher > ( & self , state : & mut H ) {
3086+ self . count . hash ( state) ;
3087+ }
3088+ }
3089+ impl Hash for InvalidRef {
3090+ fn hash < H : Hasher > ( & self , state : & mut H ) {
3091+ self . count . hash ( state) ;
3092+ }
3093+ }
3094+ let mut set: HashSet < Invalid > = HashSet :: new ( ) ;
3095+ let key = InvalidRef { count : 1 , other : 1 } ;
3096+ let value = Invalid { count : 1 , other : 2 } ;
3097+ if make_hash ( set. hasher ( ) , & key) == make_hash ( set. hasher ( ) , & value) {
3098+ set. get_or_insert_with ( & key, |_| value) ;
3099+ }
3100+ }
30483101}
0 commit comments