remove read and write generics, also remove lock from the beginning and follow lock when required pattern

This commit is contained in:
bit-aloo 2025-12-27 17:44:31 +05:30
parent 462b4a02cf
commit 359fe9a231
No known key found for this signature in database

View file

@ -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<C: Codec>() -> 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<C: Codec>() -> 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<C: Codec>() -> io::Result<()> {
.collect()
});
send_response::<_, C>(&mut stdout, bidirectional::Response::ListMacros(res))?;
send_response::<C>(&stdout, bidirectional::Response::ListMacros(res))?;
}
bidirectional::Request::ApiVersionCheck {} => {
// bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION).write::<_, C>(stdout)
send_response::<_, C>(
&mut stdout,
send_response::<C>(
&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::<C>(&stdout, bidirectional::Response::SetConfig(config))?;
}
bidirectional::Request::ExpandMacro(task) => {
handle_expand::<_, _, C>(
&srv,
&mut stdin,
&mut stdout,
&mut buf,
span_mode,
*task,
)?;
handle_expand::<C>(&srv, &mut stdin, &mut stdout, &mut buf, span_mode, *task)?;
}
},
_ => continue,
@ -109,25 +103,23 @@ fn run_new<C: Codec>() -> io::Result<()> {
Ok(())
}
fn handle_expand<W: std::io::Write, R: std::io::BufRead, C: Codec>(
fn handle_expand<C: Codec>(
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::<C>(srv, stdout, task),
legacy::SpanMode::RustAnalyzer => handle_expand_ra::<C>(srv, stdin, stdout, buf, task),
}
}
fn handle_expand_id<W: std::io::Write, C: Codec>(
fn handle_expand_id<C: Codec>(
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<W: std::io::Write, C: Codec>(
})
.map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default()));
send_response::<_, C>(stdout, bidirectional::Response::ExpandMacro(res))
send_response::<C>(&stdout, bidirectional::Response::ExpandMacro(res))
}
struct ProcMacroClientHandle {
subreq_tx: mpsc::Sender<bidirectional::SubRequest>,
subresp_rx: mpsc::Receiver<bidirectional::SubResponse>,
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<String> {
self.subreq_tx.send(bidirectional::SubRequest::SourceText { file_id, start, end }).ok()?;
match self.subresp_rx.recv().ok()? {
bidirectional::SubResponse::SourceTextResult { text } => text,
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for BidirectionalStruct<'_, C> {
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
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<W: io::Write, R: io::BufRead, C: Codec>(
fn handle_expand_ra<C: Codec>(
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<W: io::Write, R: io::BufRead, C: Codec>(
})
});
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,
&macro_name,
macro_body,
attributes,
def_site,
let res = srv
.expand(
lib,
&env,
current_dir,
&macro_name,
macro_body,
attributes,
def_site,
call_site,
mixed_site,
Some(Box::new(BidirectionalStruct::<C> { 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::<C>(&stdout, bidirectional::Response::ExpandMacroExtended(res))
}
fn run_<C: Codec>() -> io::Result<()> {
@ -441,10 +398,7 @@ fn run_<C: Codec>() -> io::Result<()> {
Ok(())
}
fn send_response<W: std::io::Write, C: Codec>(
stdout: &mut W,
resp: bidirectional::Response,
) -> io::Result<()> {
fn send_response<C: Codec>(stdout: &io::Stdout, resp: bidirectional::Response) -> io::Result<()> {
let resp = bidirectional::BidirectionalMessage::Response(resp);
resp.write::<W, C>(stdout)
resp.write::<_, C>(&mut stdout.lock())
}