@@ -835,18 +835,16 @@ mod test_drain_filter {
835
835
}
836
836
}
837
837
838
- let mut map = BTreeMap :: new ( ) ;
839
- map. insert ( 0 , D ) ;
840
- map. insert ( 4 , D ) ;
841
- map. insert ( 8 , D ) ;
838
+ // Keys are multiples of 4, so that each key is counted by a hexadecimal digit.
839
+ let mut map = ( 0 ..3 ) . map ( |i| ( i * 4 , D ) ) . collect :: < BTreeMap < _ , _ > > ( ) ;
842
840
843
841
catch_unwind ( move || {
844
842
drop ( map. drain_filter ( |i, _| {
845
843
PREDS . fetch_add ( 1usize << i, Ordering :: SeqCst ) ;
846
844
true
847
845
} ) )
848
846
} )
849
- . ok ( ) ;
847
+ . unwrap_err ( ) ;
850
848
851
849
assert_eq ! ( PREDS . load( Ordering :: SeqCst ) , 0x011 ) ;
852
850
assert_eq ! ( DROPS . load( Ordering :: SeqCst ) , 3 ) ;
@@ -864,10 +862,8 @@ mod test_drain_filter {
864
862
}
865
863
}
866
864
867
- let mut map = BTreeMap :: new ( ) ;
868
- map. insert ( 0 , D ) ;
869
- map. insert ( 4 , D ) ;
870
- map. insert ( 8 , D ) ;
865
+ // Keys are multiples of 4, so that each key is counted by a hexadecimal digit.
866
+ let mut map = ( 0 ..3 ) . map ( |i| ( i * 4 , D ) ) . collect :: < BTreeMap < _ , _ > > ( ) ;
871
867
872
868
catch_unwind ( AssertUnwindSafe ( || {
873
869
drop ( map. drain_filter ( |i, _| {
@@ -878,7 +874,45 @@ mod test_drain_filter {
878
874
}
879
875
} ) )
880
876
} ) )
881
- . ok ( ) ;
877
+ . unwrap_err ( ) ;
878
+
879
+ assert_eq ! ( PREDS . load( Ordering :: SeqCst ) , 0x011 ) ;
880
+ assert_eq ! ( DROPS . load( Ordering :: SeqCst ) , 1 ) ;
881
+ assert_eq ! ( map. len( ) , 2 ) ;
882
+ assert_eq ! ( map. first_entry( ) . unwrap( ) . key( ) , & 4 ) ;
883
+ assert_eq ! ( map. last_entry( ) . unwrap( ) . key( ) , & 8 ) ;
884
+ }
885
+
886
+ // Same as above, but attempt to use the iterator again after the panic in the predicate
887
+ #[ test]
888
+ fn pred_panic_reuse ( ) {
889
+ static PREDS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
890
+ static DROPS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
891
+
892
+ struct D ;
893
+ impl Drop for D {
894
+ fn drop ( & mut self ) {
895
+ DROPS . fetch_add ( 1 , Ordering :: SeqCst ) ;
896
+ }
897
+ }
898
+
899
+ // Keys are multiples of 4, so that each key is counted by a hexadecimal digit.
900
+ let mut map = ( 0 ..3 ) . map ( |i| ( i * 4 , D ) ) . collect :: < BTreeMap < _ , _ > > ( ) ;
901
+
902
+ {
903
+ let mut it = map. drain_filter ( |i, _| {
904
+ PREDS . fetch_add ( 1usize << i, Ordering :: SeqCst ) ;
905
+ match i {
906
+ 0 => true ,
907
+ _ => panic ! ( ) ,
908
+ }
909
+ } ) ;
910
+ catch_unwind ( AssertUnwindSafe ( || while it. next ( ) . is_some ( ) { } ) ) . unwrap_err ( ) ;
911
+ // Iterator behaviour after a panic is explicitly unspecified,
912
+ // so this is just the current implementation:
913
+ let result = catch_unwind ( AssertUnwindSafe ( || it. next ( ) ) ) ;
914
+ assert ! ( matches!( result, Ok ( None ) ) ) ;
915
+ }
882
916
883
917
assert_eq ! ( PREDS . load( Ordering :: SeqCst ) , 0x011 ) ;
884
918
assert_eq ! ( DROPS . load( Ordering :: SeqCst ) , 1 ) ;
@@ -1319,7 +1353,7 @@ fn test_into_iter_drop_leak_height_0() {
1319
1353
map. insert ( "d" , D ) ;
1320
1354
map. insert ( "e" , D ) ;
1321
1355
1322
- catch_unwind ( move || drop ( map. into_iter ( ) ) ) . ok ( ) ;
1356
+ catch_unwind ( move || drop ( map. into_iter ( ) ) ) . unwrap_err ( ) ;
1323
1357
1324
1358
assert_eq ! ( DROPS . load( Ordering :: SeqCst ) , 5 ) ;
1325
1359
}
@@ -1343,7 +1377,7 @@ fn test_into_iter_drop_leak_height_1() {
1343
1377
DROPS . store ( 0 , Ordering :: SeqCst ) ;
1344
1378
PANIC_POINT . store ( panic_point, Ordering :: SeqCst ) ;
1345
1379
let map: BTreeMap < _ , _ > = ( 0 ..size) . map ( |i| ( i, D ) ) . collect ( ) ;
1346
- catch_unwind ( move || drop ( map. into_iter ( ) ) ) . ok ( ) ;
1380
+ catch_unwind ( move || drop ( map. into_iter ( ) ) ) . unwrap_err ( ) ;
1347
1381
assert_eq ! ( DROPS . load( Ordering :: SeqCst ) , size) ;
1348
1382
}
1349
1383
}
0 commit comments