@@ -178,11 +178,17 @@ pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
178178 . next ( )
179179 . unwrap_or_else ( || panic ! ( "couldn't find a test with the provided name '{name}'" ) ) ;
180180 let TestDescAndFn { desc, testfn } = test;
181- let testfn = match testfn {
182- StaticTestFn ( f) => f,
183- _ => panic ! ( "only static tests are supported" ) ,
184- } ;
185- run_test_in_spawned_subprocess ( desc, Box :: new ( testfn) ) ;
181+ match testfn. into_runnable ( ) {
182+ Runnable :: Test ( runnable_test) => {
183+ if runnable_test. is_dynamic ( ) {
184+ panic ! ( "only static tests are supported" ) ;
185+ }
186+ run_test_in_spawned_subprocess ( desc, runnable_test) ;
187+ }
188+ Runnable :: Bench ( _) => {
189+ panic ! ( "benchmarks should not be executed into child processes" )
190+ }
191+ }
186192 }
187193
188194 let args = env:: args ( ) . collect :: < Vec < _ > > ( ) ;
@@ -563,7 +569,7 @@ pub fn run_test(
563569 id : TestId ,
564570 desc : TestDesc ,
565571 monitor_ch : Sender < CompletedTest > ,
566- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
572+ runnable_test : RunnableTest ,
567573 opts : TestRunOpts ,
568574 ) -> Option < thread:: JoinHandle < ( ) > > {
569575 let name = desc. name . clone ( ) ;
@@ -574,7 +580,7 @@ pub fn run_test(
574580 desc,
575581 opts. nocapture ,
576582 opts. time . is_some ( ) ,
577- testfn ,
583+ runnable_test ,
578584 monitor_ch,
579585 opts. time ,
580586 ) ,
@@ -615,37 +621,21 @@ pub fn run_test(
615621 let test_run_opts =
616622 TestRunOpts { strategy, nocapture : opts. nocapture , time : opts. time_options } ;
617623
618- match testfn {
619- DynBenchFn ( benchfn) => {
620- // Benchmarks aren't expected to panic, so we run them all in-process.
621- crate :: bench:: benchmark ( id, desc, monitor_ch, opts. nocapture , benchfn) ;
622- None
624+ match testfn. into_runnable ( ) {
625+ Runnable :: Test ( runnable_test) => {
626+ if runnable_test. is_dynamic ( ) {
627+ match strategy {
628+ RunStrategy :: InProcess => ( ) ,
629+ _ => panic ! ( "Cannot run dynamic test fn out-of-process" ) ,
630+ } ;
631+ }
632+ run_test_inner ( id, desc, monitor_ch, runnable_test, test_run_opts)
623633 }
624- StaticBenchFn ( benchfn ) => {
634+ Runnable :: Bench ( runnable_bench ) => {
625635 // Benchmarks aren't expected to panic, so we run them all in-process.
626- crate :: bench :: benchmark ( id, desc, monitor_ch, opts. nocapture , benchfn ) ;
636+ runnable_bench . run ( id, & desc, & monitor_ch, opts. nocapture ) ;
627637 None
628638 }
629- DynTestFn ( f) => {
630- match strategy {
631- RunStrategy :: InProcess => ( ) ,
632- _ => panic ! ( "Cannot run dynamic test fn out-of-process" ) ,
633- } ;
634- run_test_inner (
635- id,
636- desc,
637- monitor_ch,
638- Box :: new ( move || __rust_begin_short_backtrace ( f) ) ,
639- test_run_opts,
640- )
641- }
642- StaticTestFn ( f) => run_test_inner (
643- id,
644- desc,
645- monitor_ch,
646- Box :: new ( move || __rust_begin_short_backtrace ( f) ) ,
647- test_run_opts,
648- ) ,
649639 }
650640}
651641
@@ -663,7 +653,7 @@ fn run_test_in_process(
663653 desc : TestDesc ,
664654 nocapture : bool ,
665655 report_time : bool ,
666- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
656+ runnable_test : RunnableTest ,
667657 monitor_ch : Sender < CompletedTest > ,
668658 time_opts : Option < time:: TestTimeOptions > ,
669659) {
@@ -675,7 +665,7 @@ fn run_test_in_process(
675665 }
676666
677667 let start = report_time. then ( Instant :: now) ;
678- let result = fold_err ( catch_unwind ( AssertUnwindSafe ( testfn ) ) ) ;
668+ let result = fold_err ( catch_unwind ( AssertUnwindSafe ( || runnable_test . run ( ) ) ) ) ;
679669 let exec_time = start. map ( |start| {
680670 let duration = start. elapsed ( ) ;
681671 TestExecTime ( duration)
@@ -760,10 +750,7 @@ fn spawn_test_subprocess(
760750 monitor_ch. send ( message) . unwrap ( ) ;
761751}
762752
763- fn run_test_in_spawned_subprocess (
764- desc : TestDesc ,
765- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
766- ) -> ! {
753+ fn run_test_in_spawned_subprocess ( desc : TestDesc , runnable_test : RunnableTest ) -> ! {
767754 let builtin_panic_hook = panic:: take_hook ( ) ;
768755 let record_result = Arc :: new ( move |panic_info : Option < & ' _ PanicInfo < ' _ > > | {
769756 let test_result = match panic_info {
@@ -789,7 +776,7 @@ fn run_test_in_spawned_subprocess(
789776 } ) ;
790777 let record_result2 = record_result. clone ( ) ;
791778 panic:: set_hook ( Box :: new ( move |info| record_result2 ( Some ( info) ) ) ) ;
792- if let Err ( message) = testfn ( ) {
779+ if let Err ( message) = runnable_test . run ( ) {
793780 panic ! ( "{}" , message) ;
794781 }
795782 record_result ( None ) ;
0 commit comments