diff options
Diffstat (limited to 'crates/ra_proc_macro')
| -rw-r--r-- | crates/ra_proc_macro/src/process.rs | 105 |
1 files changed, 51 insertions, 54 deletions
diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs index 97ba196b8..673f80a7a 100644 --- a/crates/ra_proc_macro/src/process.rs +++ b/crates/ra_proc_macro/src/process.rs | |||
| @@ -28,60 +28,6 @@ pub(crate) struct ProcMacroProcessThread { | |||
| 28 | handle: jod_thread::JoinHandle<()>, | 28 | handle: jod_thread::JoinHandle<()>, |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | struct Task { | ||
| 32 | req: Request, | ||
| 33 | result_tx: Sender<Option<Response>>, | ||
| 34 | } | ||
| 35 | |||
| 36 | struct Process { | ||
| 37 | path: PathBuf, | ||
| 38 | args: Vec<OsString>, | ||
| 39 | child: Child, | ||
| 40 | } | ||
| 41 | |||
| 42 | impl Drop for Process { | ||
| 43 | fn drop(&mut self) { | ||
| 44 | let _ = self.child.kill(); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | impl Process { | ||
| 49 | fn run( | ||
| 50 | path: PathBuf, | ||
| 51 | args: impl IntoIterator<Item = impl AsRef<OsStr>>, | ||
| 52 | ) -> io::Result<Process> { | ||
| 53 | let args = args.into_iter().map(|s| s.as_ref().into()).collect(); | ||
| 54 | |||
| 55 | let child = Command::new(&path) | ||
| 56 | .args(&args) | ||
| 57 | .stdin(Stdio::piped()) | ||
| 58 | .stdout(Stdio::piped()) | ||
| 59 | .stderr(Stdio::null()) | ||
| 60 | .spawn()?; | ||
| 61 | |||
| 62 | Ok(Process { path, args, child }) | ||
| 63 | } | ||
| 64 | |||
| 65 | fn restart(&mut self) -> io::Result<()> { | ||
| 66 | let _ = self.child.kill(); | ||
| 67 | self.child = Command::new(&self.path) | ||
| 68 | .args(&self.args) | ||
| 69 | .stdin(Stdio::piped()) | ||
| 70 | .stdout(Stdio::piped()) | ||
| 71 | .stderr(Stdio::null()) | ||
| 72 | .spawn()?; | ||
| 73 | Ok(()) | ||
| 74 | } | ||
| 75 | |||
| 76 | fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> { | ||
| 77 | let stdin = self.child.stdin.take()?; | ||
| 78 | let stdout = self.child.stdout.take()?; | ||
| 79 | let read = BufReader::new(stdout); | ||
| 80 | |||
| 81 | Some((stdin, read)) | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | impl ProcMacroProcessSrv { | 31 | impl ProcMacroProcessSrv { |
| 86 | pub fn run( | 32 | pub fn run( |
| 87 | process_path: PathBuf, | 33 | process_path: PathBuf, |
| @@ -196,6 +142,57 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) { | |||
| 196 | } | 142 | } |
| 197 | } | 143 | } |
| 198 | 144 | ||
| 145 | struct Task { | ||
| 146 | req: Request, | ||
| 147 | result_tx: Sender<Option<Response>>, | ||
| 148 | } | ||
| 149 | |||
| 150 | struct Process { | ||
| 151 | path: PathBuf, | ||
| 152 | args: Vec<OsString>, | ||
| 153 | child: Child, | ||
| 154 | } | ||
| 155 | |||
| 156 | impl Drop for Process { | ||
| 157 | fn drop(&mut self) { | ||
| 158 | let _ = self.child.kill(); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | impl 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 | |||
| 187 | fn 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 | |||
| 199 | fn send_request( | 196 | fn send_request( |
| 200 | mut writer: &mut impl Write, | 197 | mut writer: &mut impl Write, |
| 201 | mut reader: &mut impl BufRead, | 198 | mut reader: &mut impl BufRead, |
