@@ -2234,48 +2234,50 @@ fn test_vec_dedup() {
2234
2234
#[ test]
2235
2235
fn test_vec_dedup_panicking ( ) {
2236
2236
#[ derive( Debug ) ]
2237
- struct Panic {
2238
- drop_counter : & ' static AtomicU32 ,
2237
+ struct Panic < ' a > {
2238
+ drop_counter : & ' a Cell < u32 > ,
2239
2239
value : bool ,
2240
2240
index : usize ,
2241
2241
}
2242
2242
2243
- impl PartialEq for Panic {
2243
+ impl < ' a > PartialEq for Panic < ' a > {
2244
2244
fn eq ( & self , other : & Self ) -> bool {
2245
2245
self . value == other. value
2246
2246
}
2247
2247
}
2248
2248
2249
- impl Drop for Panic {
2249
+ impl < ' a > Drop for Panic < ' a > {
2250
2250
fn drop ( & mut self ) {
2251
- let x = self . drop_counter . fetch_add ( 1 , Ordering :: SeqCst ) ;
2252
- assert ! ( x != 4 ) ;
2251
+ self . drop_counter . set ( self . drop_counter . get ( ) + 1 ) ;
2252
+ if !std:: thread:: panicking ( ) {
2253
+ assert ! ( self . index != 4 ) ;
2254
+ }
2253
2255
}
2254
2256
}
2255
2257
2256
- static DROP_COUNTER : AtomicU32 = AtomicU32 :: new ( 0 ) ;
2258
+ let drop_counter = & Cell :: new ( 0 ) ;
2257
2259
let expected = [
2258
- Panic { drop_counter : & DROP_COUNTER , value : false , index : 0 } ,
2259
- Panic { drop_counter : & DROP_COUNTER , value : false , index : 5 } ,
2260
- Panic { drop_counter : & DROP_COUNTER , value : true , index : 6 } ,
2261
- Panic { drop_counter : & DROP_COUNTER , value : true , index : 7 } ,
2260
+ Panic { drop_counter, value : false , index : 0 } ,
2261
+ Panic { drop_counter, value : false , index : 5 } ,
2262
+ Panic { drop_counter, value : true , index : 6 } ,
2263
+ Panic { drop_counter, value : true , index : 7 } ,
2262
2264
] ;
2263
2265
let mut vec = vec ! [
2264
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 0 } ,
2266
+ Panic { drop_counter, value: false , index: 0 } ,
2265
2267
// these elements get deduplicated
2266
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 1 } ,
2267
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 2 } ,
2268
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 3 } ,
2269
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 4 } ,
2270
- // here it panics
2271
- Panic { drop_counter: & DROP_COUNTER , value: false , index: 5 } ,
2272
- Panic { drop_counter: & DROP_COUNTER , value: true , index: 6 } ,
2273
- Panic { drop_counter: & DROP_COUNTER , value: true , index: 7 } ,
2268
+ Panic { drop_counter, value: false , index: 1 } ,
2269
+ Panic { drop_counter, value: false , index: 2 } ,
2270
+ Panic { drop_counter, value: false , index: 3 } ,
2271
+ Panic { drop_counter, value: false , index: 4 } ,
2272
+ // here it panics while dropping the item with index==4
2273
+ Panic { drop_counter, value: false , index: 5 } ,
2274
+ Panic { drop_counter, value: true , index: 6 } ,
2275
+ Panic { drop_counter, value: true , index: 7 } ,
2274
2276
] ;
2275
2277
2276
- let _ = std :: panic :: catch_unwind ( std :: panic :: AssertUnwindSafe ( || {
2277
- vec . dedup ( ) ;
2278
- } ) ) ;
2278
+ let _ = catch_unwind ( AssertUnwindSafe ( || vec . dedup ( ) ) ) . unwrap_err ( ) ;
2279
+
2280
+ assert_eq ! ( drop_counter . get ( ) , 4 ) ;
2279
2281
2280
2282
let ok = vec. iter ( ) . zip ( expected. iter ( ) ) . all ( |( x, y) | x. index == y. index ) ;
2281
2283
0 commit comments