Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-api/src/legacy_protocol/msg.rs')
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol/msg.rs | 56 |
1 files changed, 22 insertions, 34 deletions
diff --git a/crates/proc-macro-api/src/legacy_protocol/msg.rs b/crates/proc-macro-api/src/legacy_protocol/msg.rs index b795c45589..680372210a 100644 --- a/crates/proc-macro-api/src/legacy_protocol/msg.rs +++ b/crates/proc-macro-api/src/legacy_protocol/msg.rs @@ -8,11 +8,18 @@ use paths::Utf8PathBuf; use serde::de::DeserializeOwned; use serde_derive::{Deserialize, Serialize}; -use crate::ProcMacroKind; +use crate::{ProcMacroKind, codec::Codec}; /// Represents requests sent from the client to the proc-macro-srv. #[derive(Debug, Serialize, Deserialize)] pub enum Request { + // IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format + // As such, this is the only request that needs to be supported across all protocol versions + // and by keeping it first, we ensure it always has the same discriminant encoding in postcard + /// Performs an API version check between the client and the server. + /// Since [`VERSION_CHECK_VERSION`] + ApiVersionCheck {}, + /// Retrieves a list of macros from a given dynamic library. /// Since [`NO_VERSION_CHECK_VERSION`] ListMacros { dylib_path: Utf8PathBuf }, @@ -21,10 +28,6 @@ pub enum Request { /// Since [`NO_VERSION_CHECK_VERSION`] ExpandMacro(Box<ExpandMacro>), - /// Performs an API version check between the client and the server. - /// Since [`VERSION_CHECK_VERSION`] - ApiVersionCheck {}, - /// Sets server-specific configurations. /// Since [`RUST_ANALYZER_SPAN_SUPPORT`] SetConfig(ServerConfig), @@ -44,6 +47,13 @@ pub enum SpanMode { /// Represents responses sent from the proc-macro-srv to the client. #[derive(Debug, Serialize, Deserialize)] pub enum Response { + // IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format + // As such, this is the only request that needs to be supported across all protocol versions + // and by keeping it first, we ensure it always has the same discriminant encoding in postcard + /// Returns the API version supported by the server. + /// Since [`NO_VERSION_CHECK_VERSION`] + ApiVersionCheck(u32), + /// Returns a list of available macros in a dynamic library. /// Since [`NO_VERSION_CHECK_VERSION`] ListMacros(Result<Vec<(String, ProcMacroKind)>, String>), @@ -52,10 +62,6 @@ pub enum Response { /// Since [`NO_VERSION_CHECK_VERSION`] ExpandMacro(Result<FlatTree, PanicMessage>), - /// Returns the API version supported by the server. - /// Since [`NO_VERSION_CHECK_VERSION`] - ApiVersionCheck(u32), - /// Confirms the application of a configuration update. /// Since [`RUST_ANALYZER_SPAN_SUPPORT`] SetConfig(ServerConfig), @@ -149,39 +155,21 @@ impl ExpnGlobals { } pub trait Message: serde::Serialize + DeserializeOwned { - fn read<R: BufRead>( - from_proto: ProtocolRead<R>, - inp: &mut R, - buf: &mut String, - ) -> io::Result<Option<Self>> { - Ok(match from_proto(inp, buf)? { + fn read<R: BufRead, C: Codec>(inp: &mut R, buf: &mut C::Buf) -> io::Result<Option<Self>> { + Ok(match C::read(inp, buf)? { None => None, - Some(text) => { - let mut deserializer = serde_json::Deserializer::from_str(text); - // Note that some proc-macro generate very deep syntax tree - // We have to disable the current limit of serde here - deserializer.disable_recursion_limit(); - Some(Self::deserialize(&mut deserializer)?) - } + Some(buf) => Some(C::decode(buf)?), }) } - fn write<W: Write>(self, to_proto: ProtocolWrite<W>, out: &mut W) -> io::Result<()> { - let text = serde_json::to_string(&self)?; - to_proto(out, &text) + fn write<W: Write, C: Codec>(self, out: &mut W) -> io::Result<()> { + let value = C::encode(&self)?; + C::write(out, &value) } } impl Message for Request {} impl Message for Response {} -/// Type alias for a function that reads protocol messages from a buffered input stream. -#[allow(type_alias_bounds)] -type ProtocolRead<R: BufRead> = - for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut String) -> io::Result<Option<&'buf String>>; -/// Type alias for a function that writes protocol messages to an output stream. -#[allow(type_alias_bounds)] -type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str) -> io::Result<()>; - #[cfg(test)] mod tests { use intern::{Symbol, sym}; @@ -297,7 +285,7 @@ mod tests { let mut span_data_table = Default::default(); let task = ExpandMacro { data: ExpandMacroData { - macro_body: FlatTree::new(tt.view(), v, &mut span_data_table), + macro_body: FlatTree::from_subtree(tt.view(), v, &mut span_data_table), macro_name: Default::default(), attributes: None, has_global_spans: ExpnGlobals { |