@@ -107,6 +107,78 @@ fn test_zip_assign_into_cell() {
107107 assert_abs_diff_eq ! ( a2, & b + & c, epsilon = 1e-6 ) ;
108108}
109109
110+ #[ test]
111+ fn test_zip_collect_drop ( ) {
112+ use std:: cell:: RefCell ;
113+ use std:: panic;
114+
115+ struct Recorddrop < ' a > ( ( usize , usize ) , & ' a RefCell < Vec < ( usize , usize ) > > ) ;
116+
117+ impl < ' a > Drop for Recorddrop < ' a > {
118+ fn drop ( & mut self ) {
119+ self . 1 . borrow_mut ( ) . push ( self . 0 ) ;
120+ }
121+ }
122+
123+ #[ derive( Copy , Clone ) ]
124+ enum Config {
125+ CC ,
126+ CF ,
127+ FF ,
128+ }
129+
130+ impl Config {
131+ fn a_is_f ( self ) -> bool {
132+ match self {
133+ Config :: CC | Config :: CF => false ,
134+ _ => true ,
135+ }
136+ }
137+ fn b_is_f ( self ) -> bool {
138+ match self {
139+ Config :: CC => false ,
140+ _ => true ,
141+ }
142+ }
143+ }
144+
145+ let test_collect_panic = |config : Config , will_panic : bool , slice : bool | {
146+ let mut inserts = RefCell :: new ( Vec :: new ( ) ) ;
147+ let mut drops = RefCell :: new ( Vec :: new ( ) ) ;
148+
149+ let mut a = Array :: from_shape_fn ( ( 5 , 10 ) . set_f ( config. a_is_f ( ) ) , |idx| idx) ;
150+ let mut b = Array :: from_shape_fn ( ( 5 , 10 ) . set_f ( config. b_is_f ( ) ) , |_| 0 ) ;
151+ if slice {
152+ a = a. slice_move ( s ! [ .., ..-1 ] ) ;
153+ b = b. slice_move ( s ! [ .., ..-1 ] ) ;
154+ }
155+
156+ let _result = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
157+ Zip :: from ( & a) . and ( & b) . apply_collect ( |& elt, _| {
158+ if elt. 0 > 3 && will_panic {
159+ panic ! ( ) ;
160+ }
161+ inserts. borrow_mut ( ) . push ( elt) ;
162+ Recorddrop ( elt, & drops)
163+ } ) ;
164+ } ) ) ;
165+
166+ println ! ( "{:?}" , inserts. get_mut( ) ) ;
167+ println ! ( "{:?}" , drops. get_mut( ) ) ;
168+
169+ assert_eq ! ( inserts. get_mut( ) . len( ) , drops. get_mut( ) . len( ) , "Incorrect number of drops" ) ;
170+ assert_eq ! ( inserts. get_mut( ) , drops. get_mut( ) , "Incorrect order of drops" ) ;
171+ } ;
172+
173+ for & should_panic in & [ true , false ] {
174+ for & should_slice in & [ false , true ] {
175+ test_collect_panic ( Config :: CC , should_panic, should_slice) ;
176+ test_collect_panic ( Config :: CF , should_panic, should_slice) ;
177+ test_collect_panic ( Config :: FF , should_panic, should_slice) ;
178+ }
179+ }
180+ }
181+
110182
111183#[ test]
112184fn test_azip_syntax_trailing_comma ( ) {
0 commit comments