@@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
88use std:: process:: { Command , Stdio } ;
99use std:: sync:: mpsc:: SyncSender ;
1010
11- fn rustfmt ( src : & Path , rustfmt : & Path , paths : & [ PathBuf ] , check : bool ) -> impl FnMut ( ) {
11+ fn rustfmt ( src : & Path , rustfmt : & Path , paths : & [ PathBuf ] , check : bool ) -> impl FnMut ( bool ) -> bool {
1212 let mut cmd = Command :: new ( & rustfmt) ;
1313 // avoid the submodule config paths from coming into play,
1414 // we only allow a single global config for the workspace for now
@@ -23,7 +23,13 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
2323 let cmd_debug = format ! ( "{:?}" , cmd) ;
2424 let mut cmd = cmd. spawn ( ) . expect ( "running rustfmt" ) ;
2525 // poor man's async: return a closure that'll wait for rustfmt's completion
26- move || {
26+ move |block : bool | -> bool {
27+ if !block {
28+ match cmd. try_wait ( ) {
29+ Ok ( Some ( _) ) => { }
30+ _ => return false ,
31+ }
32+ }
2733 let status = cmd. wait ( ) . unwrap ( ) ;
2834 if !status. success ( ) {
2935 eprintln ! (
@@ -34,6 +40,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
3440 ) ;
3541 crate :: detail_exit ( 1 ) ;
3642 }
43+ true
3744 }
3845}
3946
@@ -146,15 +153,23 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
146153 let child = rustfmt ( & src, & rustfmt_path, paths. as_slice ( ) , check) ;
147154 children. push_back ( child) ;
148155
156+ // poll completion before waiting
157+ for i in ( 0 ..children. len ( ) ) . rev ( ) {
158+ if children[ i] ( false ) {
159+ children. swap_remove_back ( i) ;
160+ break ;
161+ }
162+ }
163+
149164 if children. len ( ) >= max_processes {
150165 // await oldest child
151- children. pop_front ( ) . unwrap ( ) ( ) ;
166+ children. pop_front ( ) . unwrap ( ) ( true ) ;
152167 }
153168 }
154169
155170 // await remaining children
156171 for mut child in children {
157- child ( ) ;
172+ child ( true ) ;
158173 }
159174 } ) ;
160175
0 commit comments