Unnamed repository; edit this file 'description' to name the repository.
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/bidirectional_protocol.rs | 23 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/bidirectional_protocol/msg.rs | 21 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol.rs | 12 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol/msg.rs | 38 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/process.rs | 21 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport.rs | 4 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/codec.rs | 15 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/codec/json.rs | 58 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/codec/postcard.rs | 40 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/framing.rs | 14 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/json.rs | 48 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/postcard.rs | 30 | ||||
| -rw-r--r-- | crates/proc-macro-srv-cli/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/proc-macro-srv-cli/src/main_loop.rs | 72 | ||||
| -rw-r--r-- | crates/proc-macro-srv-cli/tests/common/utils.rs | 11 |
17 files changed, 192 insertions, 218 deletions
diff --git a/Cargo.lock b/Cargo.lock index a2a18cf8ee..2cf3e37a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1882,7 +1882,6 @@ dependencies = [ "expect-test", "intern", "paths", - "postcard", "proc-macro-api", "proc-macro-srv", "proc-macro-test", diff --git a/crates/proc-macro-api/src/bidirectional_protocol.rs b/crates/proc-macro-api/src/bidirectional_protocol.rs index a13bff7d7d..8311df23d7 100644 --- a/crates/proc-macro-api/src/bidirectional_protocol.rs +++ b/crates/proc-macro-api/src/bidirectional_protocol.rs @@ -9,7 +9,7 @@ use paths::AbsPath; use span::Span; use crate::{ - Codec, ProcMacro, ProcMacroKind, ServerError, + ProcMacro, ProcMacroKind, ServerError, bidirectional_protocol::msg::{ BidirectionalMessage, ExpandMacro, ExpandMacroData, ExpnGlobals, Request, Response, SubRequest, SubResponse, @@ -22,25 +22,25 @@ use crate::{ }, }, process::ProcMacroServerProcess, - transport::codec::postcard::PostcardProtocol, + transport::postcard, }; pub mod msg; pub type SubCallback<'a> = &'a dyn Fn(SubRequest) -> Result<SubResponse, ServerError>; -pub fn run_conversation<C: Codec>( +pub fn run_conversation( writer: &mut dyn Write, reader: &mut dyn BufRead, - buf: &mut C::Buf, + buf: &mut Vec<u8>, msg: BidirectionalMessage, callback: SubCallback<'_>, ) -> Result<BidirectionalMessage, ServerError> { - let encoded = C::encode(&msg).map_err(wrap_encode)?; - C::write(writer, &encoded).map_err(wrap_io("failed to write initial request"))?; + let encoded = postcard::encode(&msg).map_err(wrap_encode)?; + postcard::write(writer, &encoded).map_err(wrap_io("failed to write initial request"))?; loop { - let maybe_buf = C::read(reader, buf).map_err(wrap_io("failed to read message"))?; + let maybe_buf = postcard::read(reader, buf).map_err(wrap_io("failed to read message"))?; let Some(b) = maybe_buf else { return Err(ServerError { message: "proc-macro server closed the stream".into(), @@ -48,7 +48,7 @@ pub fn run_conversation<C: Codec>( }); }; - let msg: BidirectionalMessage = C::decode(b).map_err(wrap_decode)?; + let msg: BidirectionalMessage = postcard::decode(b).map_err(wrap_decode)?; match msg { BidirectionalMessage::Response(response) => { @@ -57,8 +57,9 @@ pub fn run_conversation<C: Codec>( BidirectionalMessage::SubRequest(sr) => { let resp = callback(sr)?; let reply = BidirectionalMessage::SubResponse(resp); - let encoded = C::encode(&reply).map_err(wrap_encode)?; - C::write(writer, &encoded).map_err(wrap_io("failed to write sub-response"))?; + let encoded = postcard::encode(&reply).map_err(wrap_encode)?; + postcard::write(writer, &encoded) + .map_err(wrap_io("failed to write sub-response"))?; } _ => { return Err(ServerError { @@ -207,7 +208,7 @@ fn run_request( if let Some(err) = srv.exited() { return Err(err.clone()); } - srv.run_bidirectional::<PostcardProtocol>(msg, callback) + srv.run_bidirectional(msg, callback) } pub fn reject_subrequests(req: SubRequest) -> Result<SubResponse, ServerError> { diff --git a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs index d030498e59..1df0c68379 100644 --- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs +++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs @@ -1,6 +1,9 @@ //! Bidirectional protocol messages -use std::ops::Range; +use std::{ + io::{self, BufRead, Write}, + ops::Range, +}; use paths::Utf8PathBuf; use serde::{Deserialize, Serialize}; @@ -8,6 +11,7 @@ use serde::{Deserialize, Serialize}; use crate::{ ProcMacroKind, legacy_protocol::msg::{FlatTree, Message, PanicMessage, ServerConfig}, + transport::postcard, }; #[derive(Debug, Serialize, Deserialize)] @@ -97,4 +101,17 @@ pub struct ExpnGlobals { pub mixed_site: usize, } -impl Message for BidirectionalMessage {} +impl Message for BidirectionalMessage { + type Buf = Vec<u8>; + + fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> { + Ok(match postcard::read(inp, buf)? { + None => None, + Some(buf) => Some(postcard::decode(buf)?), + }) + } + fn write(self, out: &mut dyn Write) -> io::Result<()> { + let value = postcard::encode(&self)?; + postcard::write(out, &value) + } +} diff --git a/crates/proc-macro-api/src/legacy_protocol.rs b/crates/proc-macro-api/src/legacy_protocol.rs index eedf66d460..ee1795d39c 100644 --- a/crates/proc-macro-api/src/legacy_protocol.rs +++ b/crates/proc-macro-api/src/legacy_protocol.rs @@ -18,8 +18,6 @@ use crate::{ flat::serialize_span_data_index_map, }, process::ProcMacroServerProcess, - transport::codec::Codec, - transport::codec::json::JsonProtocol, version, }; @@ -149,21 +147,21 @@ fn send_task(srv: &ProcMacroServerProcess, req: Request) -> Result<Response, Ser return Err(server_error.clone()); } - srv.send_task::<_, _, JsonProtocol>(send_request::<JsonProtocol>, req) + srv.send_task_legacy::<_, _>(send_request, req) } /// Sends a request to the server and reads the response. -fn send_request<P: Codec>( +fn send_request( mut writer: &mut dyn Write, mut reader: &mut dyn BufRead, req: Request, - buf: &mut P::Buf, + buf: &mut String, ) -> Result<Option<Response>, ServerError> { - req.write::<P>(&mut writer).map_err(|err| ServerError { + req.write(&mut writer).map_err(|err| ServerError { message: "failed to write request".into(), io: Some(Arc::new(err)), })?; - let res = Response::read::<P>(&mut reader, buf).map_err(|err| ServerError { + let res = Response::read(&mut reader, buf).map_err(|err| ServerError { message: "failed to read response".into(), io: Some(Arc::new(err)), })?; diff --git a/crates/proc-macro-api/src/legacy_protocol/msg.rs b/crates/proc-macro-api/src/legacy_protocol/msg.rs index 1b65906933..bb0dde4728 100644 --- a/crates/proc-macro-api/src/legacy_protocol/msg.rs +++ b/crates/proc-macro-api/src/legacy_protocol/msg.rs @@ -8,7 +8,7 @@ use paths::Utf8PathBuf; use serde::de::DeserializeOwned; use serde_derive::{Deserialize, Serialize}; -use crate::{Codec, ProcMacroKind}; +use crate::{ProcMacroKind, transport::json}; /// Represents requests sent from the client to the proc-macro-srv. #[derive(Debug, Serialize, Deserialize)] @@ -155,20 +155,40 @@ impl ExpnGlobals { } pub trait Message: serde::Serialize + DeserializeOwned { - fn read<C: Codec>(inp: &mut dyn BufRead, buf: &mut C::Buf) -> io::Result<Option<Self>> { - Ok(match C::read(inp, buf)? { + type Buf; + fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>>; + fn write(self, out: &mut dyn Write) -> io::Result<()>; +} + +impl Message for Request { + type Buf = String; + + fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> { + Ok(match json::read(inp, buf)? { None => None, - Some(buf) => Some(C::decode(buf)?), + Some(buf) => Some(json::decode(buf)?), }) } - fn write<C: Codec>(self, out: &mut dyn Write) -> io::Result<()> { - let value = C::encode(&self)?; - C::write(out, &value) + fn write(self, out: &mut dyn Write) -> io::Result<()> { + let value = json::encode(&self)?; + json::write(out, &value) } } -impl Message for Request {} -impl Message for Response {} +impl Message for Response { + type Buf = String; + + fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> { + Ok(match json::read(inp, buf)? { + None => None, + Some(buf) => Some(json::decode(buf)?), + }) + } + fn write(self, out: &mut dyn Write) -> io::Result<()> { + let value = json::encode(&self)?; + json::write(out, &value) + } +} #[cfg(test)] mod tests { diff --git a/crates/proc-macro-api/src/lib.rs b/crates/proc-macro-api/src/lib.rs index 3acd0b292a..e4b121b033 100644 --- a/crates/proc-macro-api/src/lib.rs +++ b/crates/proc-macro-api/src/lib.rs @@ -27,7 +27,6 @@ use semver::Version; use span::{ErasedFileAstId, FIXUP_ERASED_FILE_AST_ID_MARKER, Span}; use std::{fmt, io, sync::Arc, time::SystemTime}; -pub use crate::transport::codec::Codec; use crate::{ bidirectional_protocol::SubCallback, pool::ProcMacroServerPool, process::ProcMacroServerProcess, }; diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs index 2f5bef69ab..9f80880965 100644 --- a/crates/proc-macro-api/src/process.rs +++ b/crates/proc-macro-api/src/process.rs @@ -17,7 +17,7 @@ use span::Span; use stdx::JodChild; use crate::{ - Codec, ProcMacro, ProcMacroKind, ProtocolFormat, ServerError, + ProcMacro, ProcMacroKind, ProtocolFormat, ServerError, bidirectional_protocol::{self, SubCallback, msg::BidirectionalMessage, reject_subrequests}, legacy_protocol::{self, SpanMode}, version, @@ -305,17 +305,17 @@ impl ProcMacroServerProcess { result } - pub(crate) fn send_task<Request, Response, C: Codec>( + pub(crate) fn send_task_legacy<Request, Response>( &self, send: impl FnOnce( &mut dyn Write, &mut dyn BufRead, Request, - &mut C::Buf, + &mut String, ) -> Result<Option<Response>, ServerError>, req: Request, ) -> Result<Response, ServerError> { - self.with_locked_io::<C, _>(|writer, reader, buf| { + self.with_locked_io(String::new(), |writer, reader, buf| { send(writer, reader, req, buf).and_then(|res| { res.ok_or_else(|| { let message = "proc-macro server did not respond with data".to_owned(); @@ -331,13 +331,12 @@ impl ProcMacroServerProcess { }) } - pub(crate) fn with_locked_io<C: Codec, R>( + fn with_locked_io<R, B>( &self, - f: impl FnOnce(&mut dyn Write, &mut dyn BufRead, &mut C::Buf) -> Result<R, ServerError>, + mut buf: B, + f: impl FnOnce(&mut dyn Write, &mut dyn BufRead, &mut B) -> Result<R, ServerError>, ) -> Result<R, ServerError> { let state = &mut *self.state.lock().unwrap(); - let mut buf = C::Buf::default(); - f(&mut state.stdin, &mut state.stdout, &mut buf).map_err(|e| { if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) { match state.process.exit_err() { @@ -352,13 +351,13 @@ impl ProcMacroServerProcess { }) } - pub(crate) fn run_bidirectional<C: Codec>( + pub(crate) fn run_bidirectional( &self, initial: BidirectionalMessage, callback: SubCallback<'_>, ) -> Result<BidirectionalMessage, ServerError> { - self.with_locked_io::<C, _>(|writer, reader, buf| { - bidirectional_protocol::run_conversation::<C>(writer, reader, buf, initial, callback) + self.with_locked_io(Vec::new(), |writer, reader, buf| { + bidirectional_protocol::run_conversation(writer, reader, buf, initial, callback) }) } diff --git a/crates/proc-macro-api/src/transport.rs b/crates/proc-macro-api/src/transport.rs index b7a1d8f732..f383edb0cb 100644 --- a/crates/proc-macro-api/src/transport.rs +++ b/crates/proc-macro-api/src/transport.rs @@ -1,3 +1,3 @@ //! Contains construct for transport of messages. -pub mod codec; -pub mod framing; +pub(crate) mod json; +pub(crate) mod postcard; diff --git a/crates/proc-macro-api/src/transport/codec.rs b/crates/proc-macro-api/src/transport/codec.rs deleted file mode 100644 index c9afad260a..0000000000 --- a/crates/proc-macro-api/src/transport/codec.rs +++ /dev/null @@ -1,15 +0,0 @@ -//! Protocol codec - -use std::io; - -use serde::de::DeserializeOwned; - -use crate::transport::framing::Framing; - -pub mod json; -pub mod postcard; - -pub trait Codec: Framing { - fn encode<T: serde::Serialize>(msg: &T) -> io::Result<Self::Buf>; - fn decode<T: DeserializeOwned>(buf: &mut Self::Buf) -> io::Result<T>; -} diff --git a/crates/proc-macro-api/src/transport/codec/json.rs b/crates/proc-macro-api/src/transport/codec/json.rs deleted file mode 100644 index 96db802e0b..0000000000 --- a/crates/proc-macro-api/src/transport/codec/json.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! Protocol functions for json. -use std::io::{self, BufRead, Write}; - -use serde::{Serialize, de::DeserializeOwned}; - -use crate::{Codec, transport::framing::Framing}; - -pub struct JsonProtocol; - -impl Framing for JsonProtocol { - type Buf = String; - - fn read<'a, R: BufRead + ?Sized>( - inp: &mut R, - buf: &'a mut String, - ) -> io::Result<Option<&'a mut String>> { - loop { - buf.clear(); - - inp.read_line(buf)?; - buf.pop(); // Remove trailing '\n' - - if buf.is_empty() { - return Ok(None); - } - - // Some ill behaved macro try to use stdout for debugging - // We ignore it here - if !buf.starts_with('{') { - tracing::error!("proc-macro tried to print : {}", buf); - continue; - } - - return Ok(Some(buf)); - } - } - - fn write<W: Write + ?Sized>(out: &mut W, buf: &String) -> io::Result<()> { - tracing::debug!("> {}", buf); - out.write_all(buf.as_bytes())?; - out.write_all(b"\n")?; - out.flush() - } -} - -impl Codec for JsonProtocol { - fn encode<T: Serialize>(msg: &T) -> io::Result<String> { - Ok(serde_json::to_string(msg)?) - } - - fn decode<T: DeserializeOwned>(buf: &mut String) -> io::Result<T> { - let mut deserializer = serde_json::Deserializer::from_str(buf); - // Note that some proc-macro generate very deep syntax tree - // We have to disable the current limit of serde here - deserializer.disable_recursion_limit(); - Ok(T::deserialize(&mut deserializer)?) - } -} diff --git a/crates/proc-macro-api/src/transport/codec/postcard.rs b/crates/proc-macro-api/src/transport/codec/postcard.rs deleted file mode 100644 index 6f5319e75b..0000000000 --- a/crates/proc-macro-api/src/transport/codec/postcard.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! Postcard encode and decode implementations. - -use std::io::{self, BufRead, Write}; - -use serde::{Serialize, de::DeserializeOwned}; - -use crate::{Codec, transport::framing::Framing}; - -pub struct PostcardProtocol; - -impl Framing for PostcardProtocol { - type Buf = Vec<u8>; - - fn read<'a, R: BufRead + ?Sized>( - inp: &mut R, - buf: &'a mut Vec<u8>, - ) -> io::Result<Option<&'a mut Vec<u8>>> { - buf.clear(); - let n = inp.read_until(0, buf)?; - if n == 0 { - return Ok(None); - } - Ok(Some(buf)) - } - - fn write<W: Write + ?Sized>(out: &mut W, buf: &Vec<u8>) -> io::Result<()> { - out.write_all(buf)?; - out.flush() - } -} - -impl Codec for PostcardProtocol { - fn encode<T: Serialize>(msg: &T) -> io::Result<Vec<u8>> { - postcard::to_allocvec_cobs(msg).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - } - - fn decode<T: DeserializeOwned>(buf: &mut Self::Buf) -> io::Result<T> { - postcard::from_bytes_cobs(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - } -} diff --git a/crates/proc-macro-api/src/transport/framing.rs b/crates/proc-macro-api/src/transport/framing.rs deleted file mode 100644 index 56c3b68e8c..0000000000 --- a/crates/proc-macro-api/src/transport/framing.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Protocol framing - -use std::io::{self, BufRead, Write}; - -pub trait Framing { - type Buf: Default + Send + Sync; - - fn read<'a, R: BufRead + ?Sized>( - inp: &mut R, - buf: &'a mut Self::Buf, - ) -> io::Result<Option<&'a mut Self::Buf>>; - - fn write<W: Write + ?Sized>(out: &mut W, buf: &Self::Buf) -> io::Result<()>; -} diff --git a/crates/proc-macro-api/src/transport/json.rs b/crates/proc-macro-api/src/transport/json.rs new file mode 100644 index 0000000000..f433bb7de0 --- /dev/null +++ b/crates/proc-macro-api/src/transport/json.rs @@ -0,0 +1,48 @@ +//! Protocol functions for json. +use std::io::{self, BufRead, Write}; + +use serde::{Serialize, de::DeserializeOwned}; + +pub(crate) fn read<'a, R: BufRead + ?Sized>( + inp: &mut R, + buf: &'a mut String, +) -> io::Result<Option<&'a mut String>> { + loop { + buf.clear(); + + inp.read_line(buf)?; + buf.pop(); // Remove trailing '\n' + + if buf.is_empty() { + return Ok(None); + } + + // Some ill behaved macro try to use stdout for debugging + // We ignore it here + if !buf.starts_with('{') { + tracing::error!("proc-macro tried to print : {}", buf); + continue; + } + + return Ok(Some(buf)); + } +} + +pub(crate) fn write<W: Write + ?Sized>(out: &mut W, buf: &String) -> io::Result<()> { + tracing::debug!("> {}", buf); + out.write_all(buf.as_bytes())?; + out.write_all(b"\n")?; + out.flush() +} + +pub(crate) fn encode<T: Serialize>(msg: &T) -> io::Result<String> { + Ok(serde_json::to_string(msg)?) +} + +pub(crate) fn decode<T: DeserializeOwned>(buf: &mut str) -> io::Result<T> { + let mut deserializer = serde_json::Deserializer::from_str(buf); + // Note that some proc-macro generate very deep syntax tree + // We have to disable the current limit of serde here + deserializer.disable_recursion_limit(); + Ok(T::deserialize(&mut deserializer)?) +} diff --git a/crates/proc-macro-api/src/transport/postcard.rs b/crates/proc-macro-api/src/transport/postcard.rs new file mode 100644 index 0000000000..75aa90e4c4 --- /dev/null +++ b/crates/proc-macro-api/src/transport/postcard.rs @@ -0,0 +1,30 @@ +//! Postcard encode and decode implementations. + +use std::io::{self, BufRead, Write}; + +use serde::{Serialize, de::DeserializeOwned}; + +pub(crate) fn read<'a, R: BufRead + ?Sized>( + inp: &mut R, + buf: &'a mut Vec<u8>, +) -> io::Result<Option<&'a mut Vec<u8>>> { + buf.clear(); + let n = inp.read_until(0, buf)?; + if n == 0 { + return Ok(None); + } + Ok(Some(buf)) +} + +pub(crate) fn write<W: Write + ?Sized>(out: &mut W, buf: &[u8]) -> io::Result<()> { + out.write_all(buf)?; + out.flush() +} + +pub(crate) fn encode<T: Serialize>(msg: &T) -> io::Result<Vec<u8>> { + postcard::to_allocvec_cobs(msg).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} + +pub(crate) fn decode<T: DeserializeOwned>(buf: &mut [u8]) -> io::Result<T> { + postcard::from_bytes_cobs(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} diff --git a/crates/proc-macro-srv-cli/Cargo.toml b/crates/proc-macro-srv-cli/Cargo.toml index a25e3b64ad..f586fe7644 100644 --- a/crates/proc-macro-srv-cli/Cargo.toml +++ b/crates/proc-macro-srv-cli/Cargo.toml @@ -16,7 +16,6 @@ doctest = false [dependencies] proc-macro-srv.workspace = true proc-macro-api.workspace = true -postcard.workspace = true clap = {version = "4.5.42", default-features = false, features = ["std"]} [dev-dependencies] diff --git a/crates/proc-macro-srv-cli/src/main_loop.rs b/crates/proc-macro-srv-cli/src/main_loop.rs index 70e1e091c1..758629fd1f 100644 --- a/crates/proc-macro-srv-cli/src/main_loop.rs +++ b/crates/proc-macro-srv-cli/src/main_loop.rs @@ -1,9 +1,6 @@ //! The main loop of the proc-macro server. use proc_macro_api::{ - Codec, ProtocolFormat, - bidirectional_protocol::msg as bidirectional, - legacy_protocol::msg as legacy, - transport::codec::{json::JsonProtocol, postcard::PostcardProtocol}, + ProtocolFormat, bidirectional_protocol::msg as bidirectional, legacy_protocol::msg as legacy, version::CURRENT_API_VERSION, }; use std::{ @@ -40,14 +37,12 @@ pub fn run( format: ProtocolFormat, ) -> io::Result<()> { match format { - ProtocolFormat::JsonLegacy => run_old::<JsonProtocol>(stdin, stdout), - ProtocolFormat::BidirectionalPostcardPrototype => { - run_new::<PostcardProtocol>(stdin, stdout) - } + ProtocolFormat::JsonLegacy => run_old(stdin, stdout), + ProtocolFormat::BidirectionalPostcardPrototype => run_new(stdin, stdout), } } -fn run_new<C: Codec>( +fn run_new( stdin: &mut (dyn BufRead + Send + Sync), stdout: &mut (dyn Write + Send + Sync), ) -> io::Result<()> { @@ -61,7 +56,7 @@ fn run_new<C: Codec>( } } - let mut buf = C::Buf::default(); + let mut buf = Vec::default(); let env_snapshot = EnvSnapshot::default(); let srv = proc_macro_srv::ProcMacroSrv::new(&env_snapshot); @@ -69,7 +64,7 @@ fn run_new<C: Codec>( let mut span_mode = legacy::SpanMode::Id; 'outer: loop { - let req_opt = bidirectional::BidirectionalMessage::read::<C>(stdin, &mut buf)?; + let req_opt = bidirectional::BidirectionalMessage::read(stdin, &mut buf)?; let Some(req) = req_opt else { break 'outer; }; @@ -84,11 +79,11 @@ fn run_new<C: Codec>( .collect() }); - send_response::<C>(stdout, bidirectional::Response::ListMacros(res))?; + send_response(stdout, bidirectional::Response::ListMacros(res))?; } bidirectional::Request::ApiVersionCheck {} => { - send_response::<C>( + send_response( stdout, bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION), )?; @@ -96,10 +91,10 @@ fn run_new<C: Codec>( bidirectional::Request::SetConfig(config) => { span_mode = config.span_mode; - send_response::<C>(stdout, bidirectional::Response::SetConfig(config))?; + send_response(stdout, bidirectional::Response::SetConfig(config))?; } bidirectional::Request::ExpandMacro(task) => { - handle_expand::<C>(&srv, stdin, stdout, &mut buf, span_mode, *task)?; + handle_expand(&srv, stdin, stdout, &mut buf, span_mode, *task)?; } }, _ => continue, @@ -109,21 +104,21 @@ fn run_new<C: Codec>( Ok(()) } -fn handle_expand<C: Codec>( +fn handle_expand( srv: &proc_macro_srv::ProcMacroSrv<'_>, stdin: &mut (dyn BufRead + Send + Sync), stdout: &mut (dyn Write + Send + Sync), - buf: &mut C::Buf, + buf: &mut Vec<u8>, 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<C: Codec>( +fn handle_expand_id( srv: &proc_macro_srv::ProcMacroSrv<'_>, stdout: &mut dyn Write, task: bidirectional::ExpandMacro, @@ -164,34 +159,34 @@ fn handle_expand_id<C: Codec>( }) .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<'a, C: Codec> { +struct ProcMacroClientHandle<'a> { stdin: &'a mut (dyn BufRead + Send + Sync), stdout: &'a mut (dyn Write + Send + Sync), - buf: &'a mut C::Buf, + buf: &'a mut Vec<u8>, } -impl<'a, C: Codec> ProcMacroClientHandle<'a, C> { +impl<'a> ProcMacroClientHandle<'a> { fn roundtrip( &mut self, req: bidirectional::SubRequest, ) -> Option<bidirectional::BidirectionalMessage> { let msg = bidirectional::BidirectionalMessage::SubRequest(req); - if msg.write::<C>(&mut *self.stdout).is_err() { + if msg.write(&mut *self.stdout).is_err() { return None; } - match bidirectional::BidirectionalMessage::read::<C>(&mut *self.stdin, self.buf) { + match bidirectional::BidirectionalMessage::read(&mut *self.stdin, self.buf) { Ok(Some(msg)) => Some(msg), _ => None, } } } -impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> { +impl proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_> { fn file(&mut self, file_id: proc_macro_srv::span::FileId) -> String { match self.roundtrip(bidirectional::SubRequest::FilePath { file_id: file_id.index() }) { Some(bidirectional::BidirectionalMessage::SubResponse( @@ -260,11 +255,11 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl } } -fn handle_expand_ra<C: Codec>( +fn handle_expand_ra( srv: &proc_macro_srv::ProcMacroSrv<'_>, stdin: &mut (dyn BufRead + Send + Sync), stdout: &mut (dyn Write + Send + Sync), - buf: &mut C::Buf, + buf: &mut Vec<u8>, task: bidirectional::ExpandMacro, ) -> io::Result<()> { let bidirectional::ExpandMacro { @@ -309,7 +304,7 @@ fn handle_expand_ra<C: Codec>( def_site, call_site, mixed_site, - Some(&mut ProcMacroClientHandle::<C> { stdin, stdout, buf }), + Some(&mut ProcMacroClientHandle { stdin, stdout, buf }), ) .map(|it| { ( @@ -325,10 +320,10 @@ fn handle_expand_ra<C: Codec>( .map(|(tree, span_data_table)| bidirectional::ExpandMacroExtended { tree, span_data_table }) .map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default())); - send_response::<C>(stdout, bidirectional::Response::ExpandMacroExtended(res)) + send_response(stdout, bidirectional::Response::ExpandMacroExtended(res)) } -fn run_old<C: Codec>( +fn run_old( stdin: &mut (dyn BufRead + Send + Sync), stdout: &mut (dyn Write + Send + Sync), ) -> io::Result<()> { @@ -342,9 +337,9 @@ fn run_old<C: Codec>( } } - let mut buf = C::Buf::default(); - let mut read_request = || legacy::Request::read::<C>(stdin, &mut buf); - let mut write_response = |msg: legacy::Response| msg.write::<C>(stdout); + let mut buf = String::default(); + let mut read_request = || legacy::Request::read(stdin, &mut buf); + let mut write_response = |msg: legacy::Response| msg.write(stdout); let env = EnvSnapshot::default(); let srv = proc_macro_srv::ProcMacroSrv::new(&env); @@ -473,10 +468,7 @@ fn run_old<C: Codec>( Ok(()) } -fn send_response<C: Codec>( - stdout: &mut dyn Write, - resp: bidirectional::Response, -) -> io::Result<()> { +fn send_response(stdout: &mut dyn Write, resp: bidirectional::Response) -> io::Result<()> { let resp = bidirectional::BidirectionalMessage::Response(resp); - resp.write::<C>(stdout) + resp.write(stdout) } diff --git a/crates/proc-macro-srv-cli/tests/common/utils.rs b/crates/proc-macro-srv-cli/tests/common/utils.rs index 85c394734b..3049e98004 100644 --- a/crates/proc-macro-srv-cli/tests/common/utils.rs +++ b/crates/proc-macro-srv-cli/tests/common/utils.rs @@ -12,7 +12,6 @@ use proc_macro_api::{ BidirectionalMessage, Request as BiRequest, Response as BiResponse, SubRequest, SubResponse, }, legacy_protocol::msg::{FlatTree, Message, Request, Response, SpanDataIndexMap}, - transport::codec::{json::JsonProtocol, postcard::PostcardProtocol}, }; use span::{Edition, EditionedFileId, FileId, Span, SpanAnchor, SyntaxContext, TextRange}; use tt::{Delimiter, DelimiterKind, TopSubtreeBuilder}; @@ -210,12 +209,12 @@ impl TestProtocol for JsonLegacy { type Response = Response; fn request(&self, writer: &mut dyn Write, req: Request) { - req.write::<JsonProtocol>(writer).expect("failed to write request"); + req.write(writer).expect("failed to write request"); } fn receive(&self, reader: &mut dyn BufRead, _writer: &mut dyn Write) -> Response { let mut buf = String::new(); - Response::read::<JsonProtocol>(reader, &mut buf) + Response::read(reader, &mut buf) .expect("failed to read response") .expect("no response received") } @@ -238,14 +237,14 @@ where fn request(&self, writer: &mut dyn Write, req: BiRequest) { let msg = BidirectionalMessage::Request(req); - msg.write::<PostcardProtocol>(writer).expect("failed to write request"); + msg.write(writer).expect("failed to write request"); } fn receive(&self, reader: &mut dyn BufRead, writer: &mut dyn Write) -> BiResponse { let mut buf = Vec::new(); loop { - let msg = BidirectionalMessage::read::<PostcardProtocol>(reader, &mut buf) + let msg = BidirectionalMessage::read(reader, &mut buf) .expect("failed to read message") .expect("no message received"); @@ -254,7 +253,7 @@ where BidirectionalMessage::SubRequest(sr) => { let reply = (self.callback)(sr).expect("subrequest callback failed"); let msg = BidirectionalMessage::SubResponse(reply); - msg.write::<PostcardProtocol>(writer).expect("failed to write subresponse"); + msg.write(writer).expect("failed to write subresponse"); } other => panic!("unexpected message: {other:?}"), } |