]> git.lizzy.rs Git - rust.git/commitdiff
Refactor a bit
authorEdwin Cheng <edwin0cheng@gmail.com>
Mon, 20 Apr 2020 21:22:17 +0000 (05:22 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Mon, 20 Apr 2020 21:22:17 +0000 (05:22 +0800)
crates/ra_proc_macro/src/process.rs

index 97ba196b8a2a51f7ced343164ccb0599a4db7172..673f80a7ab02aed8aedb0c79bbe14194cf60de00 100644 (file)
@@ -28,60 +28,6 @@ pub(crate) struct ProcMacroProcessThread {
     handle: jod_thread::JoinHandle<()>,
 }
 
-struct Task {
-    req: Request,
-    result_tx: Sender<Option<Response>>,
-}
-
-struct Process {
-    path: PathBuf,
-    args: Vec<OsString>,
-    child: Child,
-}
-
-impl Drop for Process {
-    fn drop(&mut self) {
-        let _ = self.child.kill();
-    }
-}
-
-impl Process {
-    fn run(
-        path: PathBuf,
-        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
-    ) -> io::Result<Process> {
-        let args = args.into_iter().map(|s| s.as_ref().into()).collect();
-
-        let child = Command::new(&path)
-            .args(&args)
-            .stdin(Stdio::piped())
-            .stdout(Stdio::piped())
-            .stderr(Stdio::null())
-            .spawn()?;
-
-        Ok(Process { path, args, child })
-    }
-
-    fn restart(&mut self) -> io::Result<()> {
-        let _ = self.child.kill();
-        self.child = Command::new(&self.path)
-            .args(&self.args)
-            .stdin(Stdio::piped())
-            .stdout(Stdio::piped())
-            .stderr(Stdio::null())
-            .spawn()?;
-        Ok(())
-    }
-
-    fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
-        let stdin = self.child.stdin.take()?;
-        let stdout = self.child.stdout.take()?;
-        let read = BufReader::new(stdout);
-
-        Some((stdin, read))
-    }
-}
-
 impl ProcMacroProcessSrv {
     pub fn run(
         process_path: PathBuf,
@@ -196,6 +142,57 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
     }
 }
 
+struct Task {
+    req: Request,
+    result_tx: Sender<Option<Response>>,
+}
+
+struct Process {
+    path: PathBuf,
+    args: Vec<OsString>,
+    child: Child,
+}
+
+impl Drop for Process {
+    fn drop(&mut self) {
+        let _ = self.child.kill();
+    }
+}
+
+impl Process {
+    fn run(
+        path: PathBuf,
+        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+    ) -> io::Result<Process> {
+        let args = args.into_iter().map(|s| s.as_ref().into()).collect();
+        let child = mk_child(&path, &args)?;
+        Ok(Process { path, args, child })
+    }
+
+    fn restart(&mut self) -> io::Result<()> {
+        let _ = self.child.kill();
+        self.child = mk_child(&self.path, &self.args)?;
+        Ok(())
+    }
+
+    fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
+        let stdin = self.child.stdin.take()?;
+        let stdout = self.child.stdout.take()?;
+        let read = BufReader::new(stdout);
+
+        Some((stdin, read))
+    }
+}
+
+fn mk_child(path: &Path, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> io::Result<Child> {
+    Command::new(&path)
+        .args(args)
+        .stdin(Stdio::piped())
+        .stdout(Stdio::piped())
+        .stderr(Stdio::null())
+        .spawn()
+}
+
 fn send_request(
     mut writer: &mut impl Write,
     mut reader: &mut impl BufRead,