Skip to content

Commit

Permalink
Merge pull request #62 from cuviper/anon-const
Browse files Browse the repository at this point in the history
Replace dummy-const with anon-const
  • Loading branch information
cuviper authored Feb 6, 2024
2 parents 9fbf1f2 + b5b7ee9 commit 7cc3351
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ categories = [ "science" ]
license = "MIT OR Apache-2.0"
name = "num-derive"
repository = "https://github.com/rust-num/num-derive"
version = "0.4.1"
version = "0.4.2"
readme = "README.md"
exclude = ["/ci/*", "/.github/*"]
edition = "2021"
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Release 0.4.2 (2024-02-06)

- [Use anon-const to avoid RFC 3373 warnings.][62]

[62]: https://github.com/rust-num/num-derive/pull/62

# Release 0.4.1 (2023-10-07)

- [Make `Float` work with `no_std`][56] -- thanks @vkahl!
Expand Down
36 changes: 14 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<syn::Type> {
match *data {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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`.
Expand All @@ -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`.
Expand All @@ -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`.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
}

0 comments on commit 7cc3351

Please sign in to comment.