From 359fe9a231a9126bcd5d02fb859b69393a7d2fbe Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 27 Dec 2025 17:44:31 +0530 Subject: [PATCH] remove read and write generics, also remove lock from the beginning and follow lock when required pattern --- .../proc-macro-srv-cli/src/main_loop.rs | 186 +++++++----------- 1 file changed, 70 insertions(+), 116 deletions(-) diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main_loop.rs b/src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main_loop.rs index 6ed42204df18..305727b3721e 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main_loop.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main_loop.rs @@ -6,7 +6,7 @@ use proc_macro_api::{ transport::codec::{json::JsonProtocol, postcard::PostcardProtocol}, version::CURRENT_API_VERSION, }; -use std::{io, sync::mpsc}; +use std::io; use legacy::Message; @@ -52,8 +52,8 @@ fn run_new() -> io::Result<()> { } let mut buf = C::Buf::default(); - let mut stdin = io::stdin().lock(); - let mut stdout = io::stdout().lock(); + let mut stdin = io::stdin(); + let mut stdout = io::stdout(); let env_snapshot = EnvSnapshot::default(); let srv = proc_macro_srv::ProcMacroSrv::new(&env_snapshot); @@ -61,7 +61,8 @@ fn run_new() -> io::Result<()> { let mut span_mode = legacy::SpanMode::Id; 'outer: loop { - let req_opt = bidirectional::BidirectionalMessage::read::<_, C>(&mut stdin, &mut buf)?; + let req_opt = + bidirectional::BidirectionalMessage::read::<_, C>(&mut stdin.lock(), &mut buf)?; let Some(req) = req_opt else { break 'outer; }; @@ -76,30 +77,23 @@ fn run_new() -> io::Result<()> { .collect() }); - send_response::<_, C>(&mut stdout, bidirectional::Response::ListMacros(res))?; + send_response::(&stdout, bidirectional::Response::ListMacros(res))?; } bidirectional::Request::ApiVersionCheck {} => { // bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION).write::<_, C>(stdout) - send_response::<_, C>( - &mut stdout, + send_response::( + &stdout, bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION), )?; } bidirectional::Request::SetConfig(config) => { span_mode = config.span_mode; - send_response::<_, C>(&mut stdout, bidirectional::Response::SetConfig(config))?; + send_response::(&stdout, bidirectional::Response::SetConfig(config))?; } bidirectional::Request::ExpandMacro(task) => { - handle_expand::<_, _, C>( - &srv, - &mut stdin, - &mut stdout, - &mut buf, - span_mode, - *task, - )?; + handle_expand::(&srv, &mut stdin, &mut stdout, &mut buf, span_mode, *task)?; } }, _ => continue, @@ -109,25 +103,23 @@ fn run_new() -> io::Result<()> { Ok(()) } -fn handle_expand( +fn handle_expand( srv: &proc_macro_srv::ProcMacroSrv<'_>, - stdin: &mut R, - stdout: &mut W, + stdin: &io::Stdin, + stdout: &io::Stdout, buf: &mut C::Buf, span_mode: legacy::SpanMode, task: bidirectional::ExpandMacro, ) -> io::Result<()> { match span_mode { - legacy::SpanMode::Id => handle_expand_id::<_, C>(srv, stdout, task), - legacy::SpanMode::RustAnalyzer => { - handle_expand_ra::<_, _, C>(srv, stdin, stdout, buf, task) - } + legacy::SpanMode::Id => handle_expand_id::(srv, stdout, task), + legacy::SpanMode::RustAnalyzer => handle_expand_ra::(srv, stdin, stdout, buf, task), } } -fn handle_expand_id( +fn handle_expand_id( srv: &proc_macro_srv::ProcMacroSrv<'_>, - stdout: &mut W, + stdout: &io::Stdout, task: bidirectional::ExpandMacro, ) -> io::Result<()> { let bidirectional::ExpandMacro { lib, env, current_dir, data } = task; @@ -166,28 +158,40 @@ fn handle_expand_id( }) .map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default())); - send_response::<_, C>(stdout, bidirectional::Response::ExpandMacro(res)) + send_response::(&stdout, bidirectional::Response::ExpandMacro(res)) } -struct ProcMacroClientHandle { - subreq_tx: mpsc::Sender, - subresp_rx: mpsc::Receiver, +struct BidirectionalStruct<'a, C: Codec> { + stdin: &'a io::Stdin, + stdout: &'a io::Stdout, + buf: &'a mut C::Buf, } -impl proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle { - fn source_text(&self, file_id: u32, start: u32, end: u32) -> Option { - self.subreq_tx.send(bidirectional::SubRequest::SourceText { file_id, start, end }).ok()?; - - match self.subresp_rx.recv().ok()? { - bidirectional::SubResponse::SourceTextResult { text } => text, +impl proc_macro_srv::ProcMacroClientInterface for BidirectionalStruct<'_, C> { + fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option { + let subrequest = bidirectional::SubRequest::SourceText { file_id, start, end }; + bidirectional::BidirectionalMessage::SubRequest(subrequest) + .write::<_, C>(&mut self.stdout.lock()) + .unwrap(); + let resp = + bidirectional::BidirectionalMessage::read::<_, C>(&mut self.stdin.lock(), self.buf) + .unwrap() + .expect("client closed connection"); + match resp { + bidirectional::BidirectionalMessage::SubResponse(api_resp) => match api_resp { + bidirectional::SubResponse::SourceTextResult { text } => { + return text; + } + }, + other => panic!("expected SubResponse, got {other:?}"), } } } -fn handle_expand_ra( +fn handle_expand_ra( srv: &proc_macro_srv::ProcMacroSrv<'_>, - stdin: &mut R, - stdout: &mut W, + stdin: &io::Stdin, + stdout: &io::Stdout, buf: &mut C::Buf, task: bidirectional::ExpandMacro, ) -> io::Result<()> { @@ -222,81 +226,34 @@ fn handle_expand_ra( }) }); - let (subreq_tx, subreq_rx) = mpsc::channel(); - let (subresp_tx, subresp_rx) = mpsc::channel(); - let (result_tx, result_rx) = mpsc::channel(); - - std::thread::scope(|s| { - s.spawn(|| { - let callback = ProcMacroClientHandle { subreq_tx, subresp_rx }; - - let res = srv - .expand( - lib, - &env, - current_dir, - ¯o_name, - macro_body, - attributes, - def_site, + let res = srv + .expand( + lib, + &env, + current_dir, + ¯o_name, + macro_body, + attributes, + def_site, + call_site, + mixed_site, + Some(Box::new(BidirectionalStruct:: { stdin, stdout, buf })), + ) + .map(|it| { + ( + legacy::FlatTree::from_tokenstream( + it, + CURRENT_API_VERSION, call_site, - mixed_site, - Some(Box::new(callback)), - ) - .map(|it| { - ( - legacy::FlatTree::from_tokenstream( - it, - CURRENT_API_VERSION, - call_site, - &mut span_data_table, - ), - legacy::serialize_span_data_index_map(&span_data_table), - ) - }) - .map(|(tree, span_data_table)| bidirectional::ExpandMacroExtended { - tree, - span_data_table, - }) - .map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default())); + &mut span_data_table, + ), + legacy::serialize_span_data_index_map(&span_data_table), + ) + }) + .map(|(tree, span_data_table)| bidirectional::ExpandMacroExtended { tree, span_data_table }) + .map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default())); - let _ = result_tx.send(res); - }); - - loop { - if let Ok(res) = result_rx.try_recv() { - let _ = send_response::<_, C>( - stdout, - bidirectional::Response::ExpandMacroExtended(res), - ); - break; - } - - let sub_req = match subreq_rx.recv() { - Ok(r) => r, - Err(_) => break, - }; - - if bidirectional::BidirectionalMessage::SubRequest(sub_req) - .write::<_, C>(stdout) - .is_err() - { - break; - } - let resp = match bidirectional::BidirectionalMessage::read::<_, C>(stdin, buf) { - Ok(Some(r)) => r, - _ => break, - }; - match resp { - bidirectional::BidirectionalMessage::SubResponse(resp) => { - let _ = subresp_tx.send(resp); - } - other => panic!("expected SubResponse, got {other:?}"), - } - } - }); - - Ok(()) + send_response::(&stdout, bidirectional::Response::ExpandMacroExtended(res)) } fn run_() -> io::Result<()> { @@ -441,10 +398,7 @@ fn run_() -> io::Result<()> { Ok(()) } -fn send_response( - stdout: &mut W, - resp: bidirectional::Response, -) -> io::Result<()> { +fn send_response(stdout: &io::Stdout, resp: bidirectional::Response) -> io::Result<()> { let resp = bidirectional::BidirectionalMessage::Response(resp); - resp.write::(stdout) + resp.write::<_, C>(&mut stdout.lock()) }