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
extern crate rusting; use rusting::Rust; use std::thread; use std::process::{Command, Stdio}; use std::io::{BufReader, Write, BufRead}; use std::sync::mpsc::{Sender, Receiver, channel}; pub struct AsyncCommand { process: std::process::Child, tx: Sender<Option<String>>, rx: Receiver<Option<String>>, } fn e<T: ::std::fmt::Display>(e: T) { println!("doesn't rust: {}", e); } fn o() { println!("no rusting for empty options"); } impl AsyncCommand { pub fn new(c: &mut Command) -> AsyncCommand { let process = c .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() .rust(e); let (tx, rx) = channel(); AsyncCommand { process: process, tx: tx, rx: rx, } } pub fn run(&mut self) { let tx = self.tx.clone(); let stdout = self.process.stdout.take().rust(o); thread::spawn(move || { let reader = BufReader::new(stdout); for line in reader.lines() { tx.send(Some(line.rust(e))).rust(e); } }); } pub fn push(&mut self, buf: &[u8]) { let stdin = self.process.stdin.as_mut().rust(o); stdin.write(buf).rust(e); stdin.flush().rust(e); } pub fn packets(&mut self) -> AsyncCommandIntoIterator { AsyncCommandIntoIterator { subprocess: self, } } } pub struct AsyncCommandIntoIterator<'a> { subprocess: &'a mut AsyncCommand, } impl <'a>Iterator for AsyncCommandIntoIterator<'a> { type Item = String; fn next(&mut self) -> Option<String> { let data = self.subprocess.rx.try_recv(); if data.is_ok() { data.rust(e) } else { None } } }