From d3019164dcbb46f8c369ed4efff79de5a42a95a8 Mon Sep 17 00:00:00 2001 From: veetaha Date: Mon, 20 Apr 2020 21:26:10 +0300 Subject: ra_proc_macro: cleanups here and there --- crates/ra_proc_macro/src/lib.rs | 17 +++++++---------- crates/ra_proc_macro/src/msg.rs | 37 ++++++++++++++++--------------------- crates/ra_proc_macro/src/process.rs | 27 +++++++++++---------------- crates/ra_proc_macro/src/rpc.rs | 16 ++++++++-------- 4 files changed, 42 insertions(+), 55 deletions(-) (limited to 'crates/ra_proc_macro/src') diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs index b200fd126..004943b9e 100644 --- a/crates/ra_proc_macro/src/lib.rs +++ b/crates/ra_proc_macro/src/lib.rs @@ -2,7 +2,7 @@ //! //! We separate proc-macro expanding logic to an extern program to allow //! different implementations (e.g. wasm or dylib loading). And this crate -//! is used to provide basic infrastructure for communication between two +//! is used to provide basic infrastructure for communication between two //! processes: Client (RA itself), Server (the external program) mod rpc; @@ -13,6 +13,7 @@ use process::{ProcMacroProcessSrv, ProcMacroProcessThread}; use ra_tt::{SmolStr, Subtree}; use std::{ ffi::OsStr, + io, path::{Path, PathBuf}, sync::Arc, }; @@ -57,14 +58,10 @@ pub struct ProcMacroClient { } impl ProcMacroClient { - pub fn extern_process( - process_path: &Path, - args: I, - ) -> Result - where - I: IntoIterator, - S: AsRef, - { + pub fn extern_process( + process_path: PathBuf, + args: impl IntoIterator>, + ) -> io::Result { let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?; Ok(ProcMacroClient { kind: ProcMacroClientKind::Process { process: Arc::new(process), thread }, @@ -84,7 +81,7 @@ impl ProcMacroClient { ProcMacroClientKind::Process { process, .. } => { let macros = match process.find_proc_macros(dylib_path) { Err(err) => { - eprintln!("Fail to find proc macro. Error: {:#?}", err); + eprintln!("Failed to find proc macros. Error: {:#?}", err); return vec![]; } Ok(macros) => macros, diff --git a/crates/ra_proc_macro/src/msg.rs b/crates/ra_proc_macro/src/msg.rs index aa95bcc8f..95d9b8804 100644 --- a/crates/ra_proc_macro/src/msg.rs +++ b/crates/ra_proc_macro/src/msg.rs @@ -1,4 +1,4 @@ -//! Defines messages for cross-process message based on `ndjson` wire protocol +//! Defines messages for cross-process message passing based on `ndjson` wire protocol use std::{ convert::TryFrom, @@ -31,7 +31,7 @@ macro_rules! impl_try_from_response { fn try_from(value: Response) -> Result { match value { Response::$tag(res) => Ok(res), - _ => Err("Fail to convert from response"), + _ => Err(concat!("Failed to convert response to ", stringify!($tag))), } } } @@ -53,18 +53,16 @@ pub enum ErrorCode { ExpansionError, } -pub trait Message: Sized + Serialize + DeserializeOwned { - fn read(r: &mut impl BufRead) -> io::Result> { - let text = match read_json(r)? { - None => return Ok(None), - Some(text) => text, - }; - let msg = serde_json::from_str(&text)?; - Ok(Some(msg)) +pub trait Message: Serialize + DeserializeOwned { + fn read(inp: &mut impl BufRead) -> io::Result> { + Ok(match read_json(inp)? { + None => None, + Some(text) => Some(serde_json::from_str(&text)?), + }) } - fn write(self, w: &mut impl Write) -> io::Result<()> { + fn write(self, out: &mut impl Write) -> io::Result<()> { let text = serde_json::to_string(&self)?; - write_json(w, &text) + write_json(out, &text) } } @@ -73,15 +71,12 @@ impl Message for Response {} fn read_json(inp: &mut impl BufRead) -> io::Result> { let mut buf = String::new(); - if inp.read_line(&mut buf)? == 0 { - return Ok(None); - } - // Remove ending '\n' - let buf = &buf[..buf.len() - 1]; - if buf.is_empty() { - return Ok(None); - } - Ok(Some(buf.to_string())) + inp.read_line(&mut buf)?; + buf.pop(); // Remove traling '\n' + Ok(match buf.len() { + 0 => None, + _ => Some(buf), + }) } fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> { diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs index f851570bc..b1ebf46a1 100644 --- a/crates/ra_proc_macro/src/process.rs +++ b/crates/ra_proc_macro/src/process.rs @@ -45,24 +45,23 @@ impl Drop for Process { } impl Process { - fn run(process_path: &Path, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - let child = Command::new(process_path.clone()) + fn run( + process_path: PathBuf, + args: impl IntoIterator>, + ) -> Result { + let child = Command::new(&process_path) .args(args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) .spawn()?; - Ok(Process { path: process_path.into(), child }) + Ok(Process { path: process_path, child }) } fn restart(&mut self) -> Result<(), io::Error> { let _ = self.child.kill(); - self.child = Command::new(self.path.clone()) + self.child = Command::new(&self.path) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) @@ -80,14 +79,10 @@ impl Process { } impl ProcMacroProcessSrv { - pub fn run( - process_path: &Path, - args: I, - ) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> - where - I: IntoIterator, - S: AsRef, - { + pub fn run( + process_path: PathBuf, + args: impl IntoIterator>, + ) -> io::Result<(ProcMacroProcessThread, ProcMacroProcessSrv)> { let process = Process::run(process_path, args)?; let (task_tx, task_rx) = bounded(0); diff --git a/crates/ra_proc_macro/src/rpc.rs b/crates/ra_proc_macro/src/rpc.rs index 66b3f55db..e97b90860 100644 --- a/crates/ra_proc_macro/src/rpc.rs +++ b/crates/ra_proc_macro/src/rpc.rs @@ -1,9 +1,9 @@ -//! Data struture serialization related stuffs for RPC +//! Data struture serialization related stuff for RPC //! -//! Define all necessary rpc serialization data structure, -//! which include ra_tt related data and some task messages. -//! Although adding Serialize and Deserialize trait to ra_tt directly seem to be much easier, -//! we deliberately duplicate the ra_tt struct with #[serde(with = "XXDef")] +//! Defines all necessary rpc serialization data structures, +//! which includes `ra_tt` related data and some task messages. +//! Although adding `Serialize` and `Deserialize` traits to `ra_tt` directly seems +//! to be much easier, we deliberately duplicate `ra_tt` structs with `#[serde(with = "XXDef")]` //! for separation of code responsibility. use ra_tt::{ @@ -34,15 +34,15 @@ pub struct ListMacrosResult { pub struct ExpansionTask { /// Argument of macro call. /// - /// In custom derive that would be a struct or enum; in attribute-like macro - underlying + /// In custom derive this will be a struct or enum; in attribute-like macro - underlying /// item; in function-like macro - the macro body. #[serde(with = "SubtreeDef")] pub macro_body: Subtree, - /// Names of macros to expand. + /// Names of macros to expand. // TODO: are they comma-separated? /// /// In custom derive those are names of derived traits (`Serialize`, `Getters`, etc.). In - /// attribute-like and functiona-like macros - single name of macro itself (`show_streams`). + /// attribute-like and function-like macros - single name of macro itself (`show_streams`). pub macro_name: String, /// Possible attributes for the attribute-like macros. -- cgit v1.2.3