From 5ffc2906bf58452166262bfa61f1751a179feefd Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 6 Feb 2024 12:50:12 -0800 Subject: [PATCH] Replace dummy-const with anon-const This has been supported since Rust 1.37, and the old way is about to start raising a warning from [RFC 3373][1]. [1]: https://rust-lang.github.io/rfcs/3373-avoid-nonlocal-definitions-in-fns.html --- src/lib.rs | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b6f7785..207602b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,16 +97,12 @@ macro_rules! parse { // we're deriving for a newtype, where the inner type is defined in the same module, but not // exported. // -// Solution: use the dummy const trick. For some reason, `extern crate` statements are allowed +// Solution: use the anonymous const trick. For some reason, `extern crate` statements are allowed // here, but everything from the surrounding module is in scope. This trick is taken from serde. -fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStream2 { - let dummy_const = Ident::new( - &format!("_IMPL_NUM_{}_FOR_{}", trait_, unraw(name)), - Span::call_site(), - ); +fn anon_const_trick(exp: TokenStream2) -> TokenStream2 { quote! { #[allow(non_upper_case_globals, unused_qualifications)] - const #dummy_const: () = { + const _: () = { #[allow(clippy::useless_attribute)] #[allow(rust_2018_idioms)] extern crate num_traits as _num_traits; @@ -115,10 +111,6 @@ fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStre } } -fn unraw(ident: &Ident) -> String { - ident.to_string().trim_start_matches("r#").to_owned() -} - // If `data` is a newtype, return the type it's wrapping. fn newtype_inner(data: &syn::Data) -> Option { match *data { @@ -189,11 +181,11 @@ impl NumTraits { } } - fn wrap(&self, trait_: &str, name: &Ident, output: TokenStream2) -> TokenStream2 { + fn wrap(&self, output: TokenStream2) -> TokenStream2 { if self.explicit { output } else { - dummy_const_trick(trait_, name, output) + anon_const_trick(output) } } } @@ -369,7 +361,7 @@ pub fn from_primitive(input: TokenStream) -> TokenStream { } }; - import.wrap("FromPrimitive", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::ToPrimitive`][to] for simple enums and newtypes. @@ -544,7 +536,7 @@ pub fn to_primitive(input: TokenStream) -> TokenStream { } }; - import.wrap("ToPrimitive", name, impl_).into() + import.wrap(impl_).into() } const NEWTYPE_ONLY: &str = "This trait can only be derived for newtypes"; @@ -623,7 +615,7 @@ pub fn num_cast(input: TokenStream) -> TokenStream { } }; - import.wrap("NumCast", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Zero`][zero] for newtypes. The inner type must already implement `Zero`. @@ -650,7 +642,7 @@ pub fn zero(input: TokenStream) -> TokenStream { } }; - import.wrap("Zero", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::One`][one] for newtypes. The inner type must already implement `One`. @@ -677,7 +669,7 @@ pub fn one(input: TokenStream) -> TokenStream { } }; - import.wrap("One", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Num`][num] for newtypes. The inner type must already implement `Num`. @@ -701,7 +693,7 @@ pub fn num(input: TokenStream) -> TokenStream { } }; - import.wrap("Num", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Float`][float] for newtypes. The inner type must already implement @@ -950,7 +942,7 @@ pub fn float(input: TokenStream) -> TokenStream { } }; - import.wrap("Float", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Signed`][signed] for newtypes. The inner type must already implement @@ -990,7 +982,7 @@ pub fn signed(input: TokenStream) -> TokenStream { } }; - import.wrap("Signed", &name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement @@ -1008,5 +1000,5 @@ pub fn unsigned(input: TokenStream) -> TokenStream { impl #import::Unsigned for #name {} }; - import.wrap("Unsigned", &name, impl_).into() + import.wrap(impl_).into() }