11use std:: io;
22use std:: io:: Write ;
3- use std:: sync:: Mutex ;
43
54use argmax:: Command ;
65
@@ -11,15 +10,15 @@ struct Outputs {
1110 stdout : Vec < u8 > ,
1211 stderr : Vec < u8 > ,
1312}
14- struct OutputBuffer < ' a > {
15- output_permission : & ' a Mutex < ( ) > ,
13+ pub struct OutputBuffer {
14+ null_separator : bool ,
1615 outputs : Vec < Outputs > ,
1716}
1817
19- impl < ' a > OutputBuffer < ' a > {
20- fn new ( output_permission : & ' a Mutex < ( ) > ) -> Self {
18+ impl OutputBuffer {
19+ pub fn new ( null_separator : bool ) -> Self {
2120 Self {
22- output_permission ,
21+ null_separator ,
2322 outputs : Vec :: new ( ) ,
2423 }
2524 }
@@ -29,34 +28,40 @@ impl<'a> OutputBuffer<'a> {
2928 }
3029
3130 fn write ( self ) {
32- // avoid taking the lock if there is nothing to do
33- if self . outputs . is_empty ( ) {
31+ // Avoid taking the lock if there is nothing to do.
32+ // If null_separator is true, then we still need to write the
33+ // null separator, because the output may have been written directly
34+ // to stdout
35+ if self . outputs . is_empty ( ) && !self . null_separator {
3436 return ;
3537 }
36- // While this lock is active, this thread will be the only thread allowed
37- // to write its outputs.
38- let _lock = self . output_permission . lock ( ) . unwrap ( ) ;
3938
4039 let stdout = io:: stdout ( ) ;
4140 let stderr = io:: stderr ( ) ;
4241
42+ // While we hold these locks, only this thread will be able
43+ // to write its outputs.
4344 let mut stdout = stdout. lock ( ) ;
4445 let mut stderr = stderr. lock ( ) ;
4546
4647 for output in self . outputs . iter ( ) {
4748 let _ = stdout. write_all ( & output. stdout ) ;
4849 let _ = stderr. write_all ( & output. stderr ) ;
4950 }
51+ if self . null_separator {
52+ // If null_separator is enabled, then we should write a \0 at the end
53+ // of the output for this entry
54+ let _ = stdout. write_all ( b"\0 " ) ;
55+ }
5056 }
5157}
5258
5359/// Executes a command.
5460pub fn execute_commands < I : Iterator < Item = io:: Result < Command > > > (
5561 cmds : I ,
56- out_perm : & Mutex < ( ) > ,
62+ mut output_buffer : OutputBuffer ,
5763 enable_output_buffering : bool ,
5864) -> ExitCode {
59- let mut output_buffer = OutputBuffer :: new ( out_perm) ;
6065 for result in cmds {
6166 let mut cmd = match result {
6267 Ok ( cmd) => cmd,
0 commit comments