Improve message usage in proc-macro
Reuse storage for the buffer send to child process of proc-macro.
This commit is contained in:
parent
f41ae64722
commit
79f583ed66
@ -55,8 +55,8 @@ pub enum ErrorCode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Message: Serialize + DeserializeOwned {
|
pub trait Message: Serialize + DeserializeOwned {
|
||||||
fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> {
|
fn read(inp: &mut impl BufRead, buf: &mut String) -> io::Result<Option<Self>> {
|
||||||
Ok(match read_json(inp)? {
|
Ok(match read_json(inp, buf)? {
|
||||||
None => None,
|
None => None,
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
let mut deserializer = serde_json::Deserializer::from_str(&text);
|
let mut deserializer = serde_json::Deserializer::from_str(&text);
|
||||||
@ -76,9 +76,13 @@ pub trait Message: Serialize + DeserializeOwned {
|
|||||||
impl Message for Request {}
|
impl Message for Request {}
|
||||||
impl Message for Response {}
|
impl Message for Response {}
|
||||||
|
|
||||||
fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> {
|
fn read_json<'a>(
|
||||||
|
inp: &mut impl BufRead,
|
||||||
|
mut buf: &'a mut String,
|
||||||
|
) -> io::Result<Option<&'a String>> {
|
||||||
loop {
|
loop {
|
||||||
let mut buf = String::new();
|
buf.clear();
|
||||||
|
|
||||||
inp.read_line(&mut buf)?;
|
inp.read_line(&mut buf)?;
|
||||||
buf.pop(); // Remove trailing '\n'
|
buf.pop(); // Remove trailing '\n'
|
||||||
|
|
||||||
|
@ -90,8 +90,10 @@ impl ProcMacroProcessSrv {
|
|||||||
fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
||||||
let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio");
|
let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio");
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
|
||||||
for Task { req, result_tx } in task_rx {
|
for Task { req, result_tx } in task_rx {
|
||||||
match send_request(&mut stdin, &mut stdout, req) {
|
match send_request(&mut stdin, &mut stdout, req, &mut buf) {
|
||||||
Ok(res) => result_tx.send(res).unwrap(),
|
Ok(res) => result_tx.send(res).unwrap(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!(
|
log::error!(
|
||||||
@ -152,7 +154,8 @@ fn send_request(
|
|||||||
mut writer: &mut impl Write,
|
mut writer: &mut impl Write,
|
||||||
mut reader: &mut impl BufRead,
|
mut reader: &mut impl BufRead,
|
||||||
req: Request,
|
req: Request,
|
||||||
|
buf: &mut String,
|
||||||
) -> io::Result<Option<Response>> {
|
) -> io::Result<Option<Response>> {
|
||||||
req.write(&mut writer)?;
|
req.write(&mut writer)?;
|
||||||
Response::read(&mut reader)
|
Response::read(&mut reader, buf)
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,9 @@ use std::io;
|
|||||||
|
|
||||||
pub fn run() -> io::Result<()> {
|
pub fn run() -> io::Result<()> {
|
||||||
let mut srv = ProcMacroSrv::default();
|
let mut srv = ProcMacroSrv::default();
|
||||||
|
let mut buf = String::new();
|
||||||
|
|
||||||
while let Some(req) = read_request()? {
|
while let Some(req) = read_request(&mut buf)? {
|
||||||
let res = match req {
|
let res = match req {
|
||||||
msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro),
|
msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro),
|
||||||
msg::Request::ExpansionMacro(task) => {
|
msg::Request::ExpansionMacro(task) => {
|
||||||
@ -30,8 +31,8 @@ pub fn run() -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_request() -> io::Result<Option<msg::Request>> {
|
fn read_request(buf: &mut String) -> io::Result<Option<msg::Request>> {
|
||||||
msg::Request::read(&mut io::stdin().lock())
|
msg::Request::read(&mut io::stdin().lock(), buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_response(msg: msg::Response) -> io::Result<()> {
|
fn write_response(msg: msg::Response) -> io::Result<()> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user