From 52170351ec80c988172202a5b73ef9eec8a5cafe Mon Sep 17 00:00:00 2001 From: Rowan Hart Date: Sun, 8 Dec 2024 11:49:32 -0800 Subject: [PATCH] Use absolute paths in derive macro generated code to permit not using top-level use statements --- derive/src/serde_bin.rs | 48 ++++++++++++++++++++++++---------------- derive/src/serde_json.rs | 24 ++++++++++---------- derive/src/serde_ron.rs | 24 ++++++++++---------- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/derive/src/serde_bin.rs b/derive/src/serde_bin.rs index 7c775fe..980ceb1 100644 --- a/derive/src/serde_bin.rs +++ b/derive/src/serde_bin.rs @@ -10,7 +10,7 @@ use proc_macro::TokenStream; pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl SerBin for {} {{ + "impl nanoserde::SerBin for {} {{ fn ser_bin(&self, s: &mut Vec) {{ let proxy: {} = self.into(); proxy.ser_bin(s); @@ -24,9 +24,9 @@ pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream { pub fn derive_de_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl DeBin for {} {{ + "impl nanoserde::DeBin for {} {{ fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ - let proxy: {} = DeBin::de_bin(o, d)?; + let proxy: {} = nanoserde::DeBin::de_bin(o, d)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", @@ -58,7 +58,7 @@ pub fn derive_ser_bin_struct(struct_: &Struct) -> TokenStream { } } format!( - "impl{} SerBin for {}{} {{ + "impl{} nanoserde::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ {} }} @@ -88,7 +88,7 @@ pub fn derive_ser_bin_struct_unnamed(struct_: &Struct) -> TokenStream { } } format!( - "impl{} SerBin for {}{} {{ + "impl{} nanoserde::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ {} }} @@ -112,20 +112,24 @@ pub fn derive_de_bin_struct(struct_: &Struct) -> TokenStream { for field in &struct_.fields { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { l!(body, "{}: {{", field.field_name.as_ref().unwrap()); - l!(body, "let proxy: {} = DeBin::de_bin(o, d)?;", proxy); + l!( + body, + "let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;", + proxy + ); l!(body, "Into::into(&proxy)"); l!(body, "},") } else { l!( body, - "{}: DeBin::de_bin(o, d)?,", + "{}: nanoserde::DeBin::de_bin(o, d)?,", field.field_name.as_ref().unwrap() ); } } format!( - "impl{} DeBin for {}{} {{ + "impl{} nanoserde::DeBin for {}{} {{ fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ ::core::result::Result::Ok(Self {{ {} @@ -151,16 +155,20 @@ pub fn derive_de_bin_struct_unnamed(struct_: &Struct) -> TokenStream { for (n, field) in struct_.fields.iter().enumerate() { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { l!(body, "{}: {{", n); - l!(body, "let proxy: {} = DeBin::de_bin(o, d)?;", proxy); + l!( + body, + "let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;", + proxy + ); l!(body, "Into::into(&proxy)"); l!(body, "},") } else { - l!(body, "{}: DeBin::de_bin(o, d)?,", n); + l!(body, "{}: nanoserde::DeBin::de_bin(o, d)?,", n); } } format!( - "impl{} DeBin for {}{} {{ + "impl{} nanoserde::DeBin for {}{} {{ fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ ::core::result::Result::Ok(Self {{ {} @@ -245,7 +253,7 @@ pub fn derive_ser_bin_enum(enum_: &Enum) -> TokenStream { } format!( - "impl{} SerBin for {}{} {{ + "impl{} nanoserde::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ match self {{ {} @@ -290,7 +298,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { variant.field_name.as_ref().unwrap() ); for _ in contents { - l!(r, "DeBin::de_bin(o, d)?,",); + l!(r, "nanoserde::DeBin::de_bin(o, d)?,",); } l!(r, "),") } @@ -307,7 +315,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { for f in contents.fields.iter() { l!( r, - "{}: DeBin::de_bin(o, d)?,", + "{}: nanoserde::DeBin::de_bin(o, d)?,", f.field_name.as_ref().unwrap() ); } @@ -320,15 +328,17 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { } format!( - "impl{} DeBin for {}{} {{ + "impl{} nanoserde::DeBin for {}{} {{ fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ - let id: u16 = DeBin::de_bin(o,d)?; + let id: u16 = nanoserde::DeBin::de_bin(o,d)?; Ok(match id {{ {} _ => return ::core::result::Result::Err(nanoserde::DeBinErr::new(*o, 0, d.len())) }}) }} - }}", generic_w_bounds,enum_.name,generic_no_bounds, r) - .parse() - .unwrap() + }}", + generic_w_bounds, enum_.name, generic_no_bounds, r + ) + .parse() + .unwrap() } diff --git a/derive/src/serde_json.rs b/derive/src/serde_json.rs index 36564ba..7a31ea6 100644 --- a/derive/src/serde_json.rs +++ b/derive/src/serde_json.rs @@ -13,7 +13,7 @@ use proc_macro::TokenStream; pub fn derive_ser_json_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl SerJson for {} {{ + "impl nanoserde::SerJson for {} {{ fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ let proxy: {} = self.into(); proxy.ser_json(d, s); @@ -106,7 +106,7 @@ pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream { format!( " - impl{} SerJson for {}{} {{ + impl{} nanoserde::SerJson for {}{} {{ fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ s.st_pre(); {} @@ -222,7 +222,7 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok for (json_field_name, local_var) in matches.iter() { l!( r, - "\"{}\" => {{s.next_colon(i) ?;{} = Some(DeJson::de_json(s, i) ?)}},", + "\"{}\" => {{s.next_colon(i) ?;{} = Some(nanoserde::DeJson::de_json(s, i) ?)}},", json_field_name, local_var ); @@ -249,10 +249,10 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok pub fn derive_de_json_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl DeJson for {} {{ + "impl nanoserde::DeJson for {} {{ #[allow(clippy::ignored_unit_patterns)] fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ - let proxy: {} = DeJson::de_json(s, i)?; + let proxy: {} = nanoserde::DeJson::de_json(s, i)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", @@ -275,7 +275,7 @@ pub fn derive_de_json_struct(struct_: &Struct) -> TokenStream { let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeJson"); format!( - "impl{} DeJson for {}{} {{ + "impl{} nanoserde::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ @@ -412,7 +412,7 @@ pub fn derive_ser_json_enum(enum_: &Enum) -> TokenStream { format!( " - impl SerJson for {} {{ + impl nanoserde::SerJson for {} {{ fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ match self {{ {} @@ -468,7 +468,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { for _ in contents.iter() { l!( field_names, - "{let r = DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r}," + "{let r = nanoserde::DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r}," ); } l!( @@ -486,7 +486,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { } let mut r = format!( - "impl{} DeJson for {}{} {{ + "impl{} nanoserde::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ match s.tok {{", @@ -568,7 +568,7 @@ pub fn derive_ser_json_struct_unnamed(struct_: &Struct) -> TokenStream { format!( " - impl{} SerJson for {}{} {{ + impl{} nanoserde::SerJson for {}{} {{ fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ {} }} @@ -592,7 +592,7 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream { let transparent = shared::attrs_transparent(&struct_.attributes); for _ in &struct_.fields { - l!(body, "{ let r = DeJson::de_json(s, i)?;"); + l!(body, "{ let r = nanoserde::DeJson::de_json(s, i)?;"); if struct_.fields.len() != 1 { l!(body, " s.eat_comma_block(i)?;"); } @@ -620,7 +620,7 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream { }; format! (" - impl{} DeJson for {}{} {{ + impl{} nanoserde::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ {} diff --git a/derive/src/serde_ron.rs b/derive/src/serde_ron.rs index 87ad9b1..fadb38f 100644 --- a/derive/src/serde_ron.rs +++ b/derive/src/serde_ron.rs @@ -10,7 +10,7 @@ use crate::shared; pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl SerRon for {} {{ + "impl nanoserde::SerRon for {} {{ fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ let proxy: {} = self.into(); proxy.ser_ron(d, s); @@ -24,9 +24,9 @@ pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream { pub fn derive_de_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream { format!( - "impl DeRon for {} {{ + "impl nanoserde::DeRon for {} {{ fn de_ron(_s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ - let proxy: {} = DeRon::deserialize_ron(i)?; + let proxy: {} = nanoserde::DeRon::deserialize_ron(i)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", @@ -72,7 +72,7 @@ pub fn derive_ser_ron_struct(struct_: &Struct) -> TokenStream { format!( " - impl SerRon for {} {{ + impl nanoserde::SerRon for {} {{ fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ s.st_pre(); {} @@ -102,7 +102,7 @@ pub fn derive_ser_ron_struct_unnamed(struct_: &Struct) -> TokenStream { } format!( " - impl SerRon for {} {{ + impl nanoserde::SerRon for {} {{ fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ s.out.push('('); {} @@ -223,7 +223,7 @@ pub fn derive_de_ron_named(name: &String, fields: &Vec, attributes: &[Att inner, "\"{}\" => {{ s.next_colon(i)?; - {} = Some(DeRon::de_ron(s, i)?) + {} = Some(nanoserde::DeRon::de_ron(s, i)?) }},", ron_field_name, local_var @@ -274,7 +274,7 @@ pub fn derive_de_ron_struct(struct_: &Struct) -> TokenStream { ); format!( - "impl DeRon for {} {{ + "impl nanoserde::DeRon for {} {{ fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ ::core::result::Result::Ok({}) }} @@ -290,7 +290,7 @@ pub fn derive_de_ron_struct_unnamed(struct_: &Struct) -> TokenStream { l!( body, "{{ - let r = DeRon::de_ron(s, i)?; + let r = nanoserde::DeRon::de_ron(s, i)?; s.eat_comma_paren(i)?; r }}," @@ -298,7 +298,7 @@ pub fn derive_de_ron_struct_unnamed(struct_: &Struct) -> TokenStream { } format! (" - impl DeRon for {} {{ + impl nanoserde::DeRon for {} {{ fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ s.paren_open(i)?; let r = Self({}); @@ -404,7 +404,7 @@ pub fn derive_ser_ron_enum(enum_: &Enum) -> TokenStream { } format!( " - impl SerRon for {} {{ + impl nanoserde::SerRon for {} {{ fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ match self {{ {} @@ -448,7 +448,7 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { l!( inner, "{ - let r = DeRon::de_ron(s, i)?; + let r = nanoserde::DeRon::de_ron(s, i)?; s.eat_comma_paren(i)?; r }, " @@ -475,7 +475,7 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { } format! (" - impl DeRon for {} {{ + impl nanoserde::DeRon for {} {{ fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ // we are expecting an identifier s.ident(i)?;