1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::io::Read;
use std::io::Result as IoResult;
use std::panic;
use std::process::Child;
use std::thread::Builder as ThreadBuilder;
use std::thread::JoinHandle;
use std::time::Duration;
use super::imp;
use super::ExitStatus;
use super::Output;
use super::Timeout;
macro_rules! r#impl {
(
$struct:ident $(< $lifetime:lifetime >)? ,
$process_type:ty ,
$return_type:ty ,
$wait_fn:expr $(,)?
) => {
#[derive(Debug)]
pub struct $struct$(<$lifetime>)? {
process: $process_type,
handle: imp::Handle,
time_limit: Duration,
strict_errors: bool,
terminate: bool,
}
impl$(<$lifetime>)? $struct$(<$lifetime>)? {
pub(crate) fn new(
process: $process_type,
time_limit: Duration,
) -> Self {
Self {
handle: imp::Handle::inherited(&process),
process,
time_limit,
strict_errors: false,
terminate: false,
}
}
fn run_wait(&mut self) -> IoResult<Option<ExitStatus>> {
let result = self.process.try_wait();
if let Ok(Some(exit_status)) = result {
return Ok(Some(exit_status.into()));
}
self
.handle
.wait_with_timeout(self.time_limit)
.map(|x| x.map(ExitStatus))
}
}
#[allow(single_use_lifetimes)]
impl$(<$lifetime>)? Timeout for $struct$(<$lifetime>)? {
type Result = $return_type;
#[inline]
fn strict_errors(mut self) -> Self {
self.strict_errors = true;
self
}
#[inline]
fn terminating(mut self) -> Self {
self.terminate = true;
self
}
#[inline]
fn wait(mut self) -> IoResult<Option<Self::Result>> {
let _ = self.process.stdin.take();
let mut result = $wait_fn(&mut self);
macro_rules! try_run {
( $result:expr ) => {
let next_result = $result;
if self.strict_errors && result.is_ok() {
if let Err(error) = next_result {
result = Err(error);
}
}
};
}
if self.terminate {
if let Ok(Some(_)) = result {
} else {
try_run!(self.process.kill().and(self.process.wait()));
}
}
try_run!(self.process.try_wait());
result
}
}
};
}
r#impl!(
ExitStatusTimeout<'a>,
&'a mut Child,
ExitStatus,
Self::run_wait,
);
r#impl!(OutputTimeout, Child, Output, |timeout: &mut Self| {
let stdout_reader = spawn_reader(&mut timeout.process.stdout)?;
let stderr_reader = spawn_reader(&mut timeout.process.stderr)?;
return timeout
.run_wait()?
.map(|x| {
Ok(Output {
status: x,
stdout: join_reader(stdout_reader)?,
stderr: join_reader(stderr_reader)?,
})
})
.transpose();
fn spawn_reader<TSource>(
source: &mut Option<TSource>,
) -> IoResult<Option<JoinHandle<IoResult<Vec<u8>>>>>
where
TSource: 'static + Read + Send,
{
source
.take()
.map(|mut x| {
ThreadBuilder::new().spawn(move || {
let mut buffer = Vec::new();
let _ = x.read_to_end(&mut buffer)?;
Ok(buffer)
})
})
.transpose()
}
fn join_reader(
reader: Option<JoinHandle<IoResult<Vec<u8>>>>,
) -> IoResult<Vec<u8>> {
reader
.map(|x| x.join().unwrap_or_else(|x| panic::resume_unwind(x)))
.transpose()
.map(|x| x.unwrap_or_else(Vec::new))
}
});