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
use std::io::{stdin, Read};
use std::thread;
use std::time::Duration;
pub fn optional_stdin() -> Option<Vec<u8>> {
let (sender, receiver) = std::sync::mpsc::channel();
thread::spawn(move || {
let sin = stdin();
let mut bytes = vec![];
sin.lock()
.read_to_end(&mut bytes)
.expect("Failed to read from stdin");
sender.send(bytes).expect("Failed to send input");
});
thread::sleep(Duration::from_millis(1));
match receiver.try_recv() {
Ok(l) => Some(l),
_ => None,
}
}