@@ -40,7 +40,7 @@ pub struct ProcessBuilder {
40
40
/// See [`ProcessBuilder::retry_with_argfile`] for more information.
41
41
retry_with_argfile : bool ,
42
42
/// Data to write to stdin.
43
- stdin : Vec < u8 > ,
43
+ stdin : Option < Vec < u8 > > ,
44
44
}
45
45
46
46
impl fmt:: Display for ProcessBuilder {
@@ -82,7 +82,7 @@ impl ProcessBuilder {
82
82
jobserver : None ,
83
83
display_env_vars : false ,
84
84
retry_with_argfile : false ,
85
- stdin : Vec :: new ( ) ,
85
+ stdin : None ,
86
86
}
87
87
}
88
88
@@ -212,7 +212,7 @@ impl ProcessBuilder {
212
212
213
213
/// Sets a value that will be written to stdin of the process on launch.
214
214
pub fn stdin < T : Into < Vec < u8 > > > ( & mut self , stdin : T ) -> & mut Self {
215
- self . stdin = stdin. into ( ) ;
215
+ self . stdin = Some ( stdin. into ( ) ) ;
216
216
self
217
217
}
218
218
@@ -284,18 +284,22 @@ impl ProcessBuilder {
284
284
fn _output ( & self ) -> io:: Result < Output > {
285
285
if !debug_force_argfile ( self . retry_with_argfile ) {
286
286
let mut cmd = self . build_command ( ) ;
287
- match piped ( & mut cmd) . spawn ( ) {
287
+ match piped ( & mut cmd, self . stdin . is_some ( ) ) . spawn ( ) {
288
288
Err ( ref e) if self . should_retry_with_argfile ( e) => { }
289
289
Err ( e) => return Err ( e) ,
290
290
Ok ( mut child) => {
291
- child. stdin . take ( ) . unwrap ( ) . write_all ( & self . stdin ) ?;
291
+ if let Some ( stdin) = & self . stdin {
292
+ child. stdin . take ( ) . unwrap ( ) . write_all ( stdin) ?;
293
+ }
292
294
return child. wait_with_output ( ) ;
293
295
}
294
296
}
295
297
}
296
298
let ( mut cmd, argfile) = self . build_command_with_argfile ( ) ?;
297
- let mut child = piped ( & mut cmd) . spawn ( ) ?;
298
- child. stdin . take ( ) . unwrap ( ) . write_all ( & self . stdin ) ?;
299
+ let mut child = piped ( & mut cmd, self . stdin . is_some ( ) ) . spawn ( ) ?;
300
+ if let Some ( stdin) = & self . stdin {
301
+ child. stdin . take ( ) . unwrap ( ) . write_all ( stdin) ?;
302
+ }
299
303
let output = child. wait_with_output ( ) ;
300
304
close_tempfile_and_log_error ( argfile) ;
301
305
output
@@ -340,14 +344,14 @@ impl ProcessBuilder {
340
344
341
345
let spawn = |mut cmd| {
342
346
if !debug_force_argfile ( self . retry_with_argfile ) {
343
- match piped ( & mut cmd) . spawn ( ) {
347
+ match piped ( & mut cmd, false ) . spawn ( ) {
344
348
Err ( ref e) if self . should_retry_with_argfile ( e) => { }
345
349
Err ( e) => return Err ( e) ,
346
350
Ok ( child) => return Ok ( ( child, None ) ) ,
347
351
}
348
352
}
349
353
let ( mut cmd, argfile) = self . build_command_with_argfile ( ) ?;
350
- Ok ( ( piped ( & mut cmd) . spawn ( ) ?, Some ( argfile) ) )
354
+ Ok ( ( piped ( & mut cmd, false ) . spawn ( ) ?, Some ( argfile) ) )
351
355
} ;
352
356
353
357
let status = ( || {
@@ -541,11 +545,15 @@ fn debug_force_argfile(retry_enabled: bool) -> bool {
541
545
cfg ! ( debug_assertions) && env:: var ( "__CARGO_TEST_FORCE_ARGFILE" ) . is_ok ( ) && retry_enabled
542
546
}
543
547
544
- /// Creates new pipes for stderr, stdout and stdin.
545
- fn piped ( cmd : & mut Command ) -> & mut Command {
548
+ /// Creates new pipes for stderr, stdout, and optionally stdin.
549
+ fn piped ( cmd : & mut Command , pipe_stdin : bool ) -> & mut Command {
546
550
cmd. stdout ( Stdio :: piped ( ) )
547
551
. stderr ( Stdio :: piped ( ) )
548
- . stdin ( Stdio :: piped ( ) )
552
+ . stdin ( if pipe_stdin {
553
+ Stdio :: piped ( )
554
+ } else {
555
+ Stdio :: null ( )
556
+ } )
549
557
}
550
558
551
559
fn close_tempfile_and_log_error ( file : NamedTempFile ) {
0 commit comments