aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_proc_macro/src/process.rs103
1 files changed, 52 insertions, 51 deletions
diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs
index e24944af4..673f80a7a 100644
--- a/crates/ra_proc_macro/src/process.rs
+++ b/crates/ra_proc_macro/src/process.rs
@@ -9,7 +9,7 @@ use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTas
9use io::{BufRead, BufReader}; 9use io::{BufRead, BufReader};
10use std::{ 10use std::{
11 convert::{TryFrom, TryInto}, 11 convert::{TryFrom, TryInto},
12 ffi::OsStr, 12 ffi::{OsStr, OsString},
13 io::{self, Write}, 13 io::{self, Write},
14 path::{Path, PathBuf}, 14 path::{Path, PathBuf},
15 process::{Child, Command, Stdio}, 15 process::{Child, Command, Stdio},
@@ -28,56 +28,6 @@ pub(crate) struct ProcMacroProcessThread {
28 handle: jod_thread::JoinHandle<()>, 28 handle: jod_thread::JoinHandle<()>,
29} 29}
30 30
31struct Task {
32 req: Request,
33 result_tx: Sender<Option<Response>>,
34}
35
36struct Process {
37 path: PathBuf,
38 child: Child,
39}
40
41impl Drop for Process {
42 fn drop(&mut self) {
43 let _ = self.child.kill();
44 }
45}
46
47impl Process {
48 fn run(
49 process_path: PathBuf,
50 args: impl IntoIterator<Item = impl AsRef<OsStr>>,
51 ) -> io::Result<Process> {
52 let child = Command::new(&process_path)
53 .args(args)
54 .stdin(Stdio::piped())
55 .stdout(Stdio::piped())
56 .stderr(Stdio::null())
57 .spawn()?;
58
59 Ok(Process { path: process_path, child })
60 }
61
62 fn restart(&mut self) -> io::Result<()> {
63 let _ = self.child.kill();
64 self.child = Command::new(&self.path)
65 .stdin(Stdio::piped())
66 .stdout(Stdio::piped())
67 .stderr(Stdio::null())
68 .spawn()?;
69 Ok(())
70 }
71
72 fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
73 let stdin = self.child.stdin.take()?;
74 let stdout = self.child.stdout.take()?;
75 let read = BufReader::new(stdout);
76
77 Some((stdin, read))
78 }
79}
80
81impl ProcMacroProcessSrv { 31impl ProcMacroProcessSrv {
82 pub fn run( 32 pub fn run(
83 process_path: PathBuf, 33 process_path: PathBuf,
@@ -192,6 +142,57 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
192 } 142 }
193} 143}
194 144
145struct Task {
146 req: Request,
147 result_tx: Sender<Option<Response>>,
148}
149
150struct Process {
151 path: PathBuf,
152 args: Vec<OsString>,
153 child: Child,
154}
155
156impl Drop for Process {
157 fn drop(&mut self) {
158 let _ = self.child.kill();
159 }
160}
161
162impl Process {
163 fn run(
164 path: PathBuf,
165 args: impl IntoIterator<Item = impl AsRef<OsStr>>,
166 ) -> io::Result<Process> {
167 let args = args.into_iter().map(|s| s.as_ref().into()).collect();
168 let child = mk_child(&path, &args)?;
169 Ok(Process { path, args, child })
170 }
171
172 fn restart(&mut self) -> io::Result<()> {
173 let _ = self.child.kill();
174 self.child = mk_child(&self.path, &self.args)?;
175 Ok(())
176 }
177
178 fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
179 let stdin = self.child.stdin.take()?;
180 let stdout = self.child.stdout.take()?;
181 let read = BufReader::new(stdout);
182
183 Some((stdin, read))
184 }
185}
186
187fn mk_child(path: &Path, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> io::Result<Child> {
188 Command::new(&path)
189 .args(args)
190 .stdin(Stdio::piped())
191 .stdout(Stdio::piped())
192 .stderr(Stdio::null())
193 .spawn()
194}
195
195fn send_request( 196fn send_request(
196 mut writer: &mut impl Write, 197 mut writer: &mut impl Write,
197 mut reader: &mut impl BufRead, 198 mut reader: &mut impl BufRead,