@@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
8
8
use std:: process:: { Command , Stdio } ;
9
9
use std:: sync:: mpsc:: SyncSender ;
10
10
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 {
12
12
let mut cmd = Command :: new ( & rustfmt) ;
13
13
// avoid the submodule config paths from coming into play,
14
14
// 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
23
23
let cmd_debug = format ! ( "{:?}" , cmd) ;
24
24
let mut cmd = cmd. spawn ( ) . expect ( "running rustfmt" ) ;
25
25
// 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
+ }
27
33
let status = cmd. wait ( ) . unwrap ( ) ;
28
34
if !status. success ( ) {
29
35
eprintln ! (
@@ -34,6 +40,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
34
40
) ;
35
41
crate :: detail_exit ( 1 ) ;
36
42
}
43
+ true
37
44
}
38
45
}
39
46
@@ -146,15 +153,23 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
146
153
let child = rustfmt ( & src, & rustfmt_path, paths. as_slice ( ) , check) ;
147
154
children. push_back ( child) ;
148
155
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
+
149
164
if children. len ( ) >= max_processes {
150
165
// await oldest child
151
- children. pop_front ( ) . unwrap ( ) ( ) ;
166
+ children. pop_front ( ) . unwrap ( ) ( true ) ;
152
167
}
153
168
}
154
169
155
170
// await remaining children
156
171
for mut child in children {
157
- child ( ) ;
172
+ child ( true ) ;
158
173
}
159
174
} ) ;
160
175
0 commit comments