aboutsummaryrefslogtreecommitdiff
path: root/crates/proc_macro_api/src/msg.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2021-03-23 19:47:08 +0000
committerEdwin Cheng <[email protected]>2021-03-23 19:51:06 +0000
commit79f583ed6622be591886f99974766a3aeda39182 (patch)
treea332596cd744a1d50b474b5a82d66af171048707 /crates/proc_macro_api/src/msg.rs
parentf41ae64722ab8e501e2123018d1b0101db32442e (diff)
Improve message usage in proc-macro
Reuse storage for the buffer send to child process of proc-macro.
Diffstat (limited to 'crates/proc_macro_api/src/msg.rs')
-rw-r--r--crates/proc_macro_api/src/msg.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/crates/proc_macro_api/src/msg.rs b/crates/proc_macro_api/src/msg.rs
index 21e56cc83..f525df152 100644
--- a/crates/proc_macro_api/src/msg.rs
+++ b/crates/proc_macro_api/src/msg.rs
@@ -55,8 +55,8 @@ pub enum ErrorCode {
55} 55}
56 56
57pub trait Message: Serialize + DeserializeOwned { 57pub trait Message: Serialize + DeserializeOwned {
58 fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> { 58 fn read(inp: &mut impl BufRead, buf: &mut String) -> io::Result<Option<Self>> {
59 Ok(match read_json(inp)? { 59 Ok(match read_json(inp, buf)? {
60 None => None, 60 None => None,
61 Some(text) => { 61 Some(text) => {
62 let mut deserializer = serde_json::Deserializer::from_str(&text); 62 let mut deserializer = serde_json::Deserializer::from_str(&text);
@@ -76,9 +76,13 @@ pub trait Message: Serialize + DeserializeOwned {
76impl Message for Request {} 76impl Message for Request {}
77impl Message for Response {} 77impl Message for Response {}
78 78
79fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> { 79fn read_json<'a>(
80 inp: &mut impl BufRead,
81 mut buf: &'a mut String,
82) -> io::Result<Option<&'a String>> {
80 loop { 83 loop {
81 let mut buf = String::new(); 84 buf.clear();
85
82 inp.read_line(&mut buf)?; 86 inp.read_line(&mut buf)?;
83 buf.pop(); // Remove trailing '\n' 87 buf.pop(); // Remove trailing '\n'
84 88