@@ -10,12 +10,12 @@ use std::thread::JoinHandle;
10
10
///
11
11
/// Calling `spawn!` macro will return `Result<CmdChildren>`
12
12
pub struct CmdChildren {
13
- children : Vec < CmdChild > ,
13
+ children : Vec < Result < CmdChild > > ,
14
14
ignore_error : bool ,
15
15
}
16
16
17
17
impl CmdChildren {
18
- pub ( crate ) fn new ( children : Vec < CmdChild > , ignore_error : bool ) -> Self {
18
+ pub ( crate ) fn new ( children : Vec < Result < CmdChild > > , ignore_error : bool ) -> Self {
19
19
Self {
20
20
children,
21
21
ignore_error,
@@ -32,19 +32,32 @@ impl CmdChildren {
32
32
pub fn wait ( & mut self ) -> CmdResult {
33
33
// wait for the last child result
34
34
let handle = self . children . pop ( ) . unwrap ( ) ;
35
- if let Err ( e) = handle. wait ( true ) {
36
- let _ = Self :: wait_children ( & mut self . children ) ;
37
- return Err ( e) ;
35
+ match handle {
36
+ Err ( e) => {
37
+ let _ = Self :: wait_children ( & mut self . children ) ;
38
+ return Err ( e) ;
39
+ }
40
+ Ok ( handle) => {
41
+ if let Err ( e) = handle. wait ( true ) {
42
+ let _ = Self :: wait_children ( & mut self . children ) ;
43
+ return Err ( e) ;
44
+ }
45
+ }
38
46
}
39
47
Self :: wait_children ( & mut self . children )
40
48
}
41
49
42
- fn wait_children ( children : & mut Vec < CmdChild > ) -> CmdResult {
50
+ fn wait_children ( children : & mut Vec < Result < CmdChild > > ) -> CmdResult {
43
51
let mut ret = Ok ( ( ) ) ;
44
52
while !children. is_empty ( ) {
45
53
let child_handle = children. pop ( ) . unwrap ( ) ;
46
- if let Err ( e) = child_handle. wait ( false ) {
47
- ret = Err ( e) ;
54
+ match child_handle {
55
+ Err ( e) => ret = Err ( e) ,
56
+ Ok ( child_handle) => {
57
+ if let Err ( e) = child_handle. wait ( false ) {
58
+ ret = Err ( e) ;
59
+ }
60
+ }
48
61
}
49
62
}
50
63
ret
@@ -56,40 +69,48 @@ impl CmdChildren {
56
69
///
57
70
/// Calling `spawn_with_output!` macro will return `Result<FunChildren>`
58
71
pub struct FunChildren {
59
- children : Vec < CmdChild > ,
72
+ children : Vec < Result < CmdChild > > ,
60
73
ignore_error : bool ,
61
74
}
62
75
63
76
impl FunChildren {
64
77
pub fn wait_with_output ( & mut self ) -> FunResult {
65
78
// wait for the last child result
66
79
let handle = self . children . pop ( ) . unwrap ( ) ;
67
- let wait_last = handle. wait_with_output ( self . ignore_error ) ;
68
- match wait_last {
80
+ match handle {
69
81
Err ( e) => {
70
82
let _ = CmdChildren :: wait_children ( & mut self . children ) ;
71
83
Err ( e)
72
84
}
73
- Ok ( output) => {
74
- let mut s = String :: from_utf8_lossy ( & output) . to_string ( ) ;
75
- if s. ends_with ( '\n' ) {
76
- s. pop ( ) ;
77
- }
78
- let ret = CmdChildren :: wait_children ( & mut self . children ) ;
79
- if let Err ( e) = ret {
80
- if !self . ignore_error {
81
- return Err ( e) ;
85
+ Ok ( handle) => {
86
+ let wait_last = handle. wait_with_output ( self . ignore_error ) ;
87
+ match wait_last {
88
+ Err ( e) => {
89
+ let _ = CmdChildren :: wait_children ( & mut self . children ) ;
90
+ Err ( e)
91
+ }
92
+ Ok ( output) => {
93
+ let mut s = String :: from_utf8_lossy ( & output) . to_string ( ) ;
94
+ if s. ends_with ( '\n' ) {
95
+ s. pop ( ) ;
96
+ }
97
+ let ret = CmdChildren :: wait_children ( & mut self . children ) ;
98
+ if let Err ( e) = ret {
99
+ if !self . ignore_error {
100
+ return Err ( e) ;
101
+ }
102
+ }
103
+ Ok ( s)
82
104
}
83
105
}
84
- Ok ( s)
85
106
}
86
107
}
87
108
}
88
109
89
110
pub fn wait_with_pipe ( & mut self , f : & mut dyn FnMut ( Box < dyn Read > ) ) -> CmdResult {
90
- let child = self . children . pop ( ) . unwrap ( ) ;
111
+ let child = self . children . pop ( ) . unwrap ( ) ? ;
91
112
let polling_stderr = StderrLogging :: new ( & child. cmd , child. stderr ) ;
92
- match child. handle ? {
113
+ match child. handle {
93
114
CmdChildHandle :: Proc ( mut proc) => {
94
115
if let Some ( stdout) = child. stdout {
95
116
f ( Box :: new ( stdout) ) ;
@@ -113,15 +134,15 @@ impl FunChildren {
113
134
}
114
135
115
136
pub ( crate ) struct CmdChild {
116
- handle : Result < CmdChildHandle > ,
137
+ handle : CmdChildHandle ,
117
138
cmd : String ,
118
139
stdout : Option < PipeReader > ,
119
140
stderr : Option < PipeReader > ,
120
141
}
121
142
122
143
impl CmdChild {
123
144
pub ( crate ) fn new (
124
- handle : Result < CmdChildHandle > ,
145
+ handle : CmdChildHandle ,
125
146
cmd : String ,
126
147
stdout : Option < PipeReader > ,
127
148
stderr : Option < PipeReader > ,
@@ -135,7 +156,7 @@ impl CmdChild {
135
156
}
136
157
137
158
fn wait ( self , is_last : bool ) -> CmdResult {
138
- let res = self . handle ? . wait_with_stderr ( self . stderr , & self . cmd ) ;
159
+ let res = self . handle . wait_with_stderr ( self . stderr , & self . cmd ) ;
139
160
if let Err ( e) = res {
140
161
if is_last || process:: pipefail_enabled ( ) {
141
162
return Err ( e) ;
@@ -158,7 +179,7 @@ impl CmdChild {
158
179
vec ! [ ]
159
180
}
160
181
} ;
161
- let res = self . handle ? . wait_with_stderr ( self . stderr , & self . cmd ) ;
182
+ let res = self . handle . wait_with_stderr ( self . stderr , & self . cmd ) ;
162
183
if let Err ( e) = res {
163
184
if !ignore_error {
164
185
return Err ( e) ;
0 commit comments