Skip to content

Commit 7cc3351

Browse files
authored
Merge pull request #62 from cuviper/anon-const
Replace dummy-const with anon-const
2 parents 9fbf1f2 + b5b7ee9 commit 7cc3351

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ categories = [ "science" ]
88
license = "MIT OR Apache-2.0"
99
name = "num-derive"
1010
repository = "https://github.com/rust-num/num-derive"
11-
version = "0.4.1"
11+
version = "0.4.2"
1212
readme = "README.md"
1313
exclude = ["/ci/*", "/.github/*"]
1414
edition = "2021"

RELEASES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Release 0.4.2 (2024-02-06)
2+
3+
- [Use anon-const to avoid RFC 3373 warnings.][62]
4+
5+
[62]: https://github.com/rust-num/num-derive/pull/62
6+
17
# Release 0.4.1 (2023-10-07)
28

39
- [Make `Float` work with `no_std`][56] -- thanks @vkahl!

src/lib.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,12 @@ macro_rules! parse {
9797
// we're deriving for a newtype, where the inner type is defined in the same module, but not
9898
// exported.
9999
//
100-
// Solution: use the dummy const trick. For some reason, `extern crate` statements are allowed
100+
// Solution: use the anonymous const trick. For some reason, `extern crate` statements are allowed
101101
// here, but everything from the surrounding module is in scope. This trick is taken from serde.
102-
fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStream2 {
103-
let dummy_const = Ident::new(
104-
&format!("_IMPL_NUM_{}_FOR_{}", trait_, unraw(name)),
105-
Span::call_site(),
106-
);
102+
fn anon_const_trick(exp: TokenStream2) -> TokenStream2 {
107103
quote! {
108104
#[allow(non_upper_case_globals, unused_qualifications)]
109-
const #dummy_const: () = {
105+
const _: () = {
110106
#[allow(clippy::useless_attribute)]
111107
#[allow(rust_2018_idioms)]
112108
extern crate num_traits as _num_traits;
@@ -115,10 +111,6 @@ fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStre
115111
}
116112
}
117113

118-
fn unraw(ident: &Ident) -> String {
119-
ident.to_string().trim_start_matches("r#").to_owned()
120-
}
121-
122114
// If `data` is a newtype, return the type it's wrapping.
123115
fn newtype_inner(data: &syn::Data) -> Option<syn::Type> {
124116
match *data {
@@ -189,11 +181,11 @@ impl NumTraits {
189181
}
190182
}
191183

192-
fn wrap(&self, trait_: &str, name: &Ident, output: TokenStream2) -> TokenStream2 {
184+
fn wrap(&self, output: TokenStream2) -> TokenStream2 {
193185
if self.explicit {
194186
output
195187
} else {
196-
dummy_const_trick(trait_, name, output)
188+
anon_const_trick(output)
197189
}
198190
}
199191
}
@@ -369,7 +361,7 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
369361
}
370362
};
371363

372-
import.wrap("FromPrimitive", name, impl_).into()
364+
import.wrap(impl_).into()
373365
}
374366

375367
/// Derives [`num_traits::ToPrimitive`][to] for simple enums and newtypes.
@@ -544,7 +536,7 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
544536
}
545537
};
546538

547-
import.wrap("ToPrimitive", name, impl_).into()
539+
import.wrap(impl_).into()
548540
}
549541

550542
const NEWTYPE_ONLY: &str = "This trait can only be derived for newtypes";
@@ -623,7 +615,7 @@ pub fn num_cast(input: TokenStream) -> TokenStream {
623615
}
624616
};
625617

626-
import.wrap("NumCast", name, impl_).into()
618+
import.wrap(impl_).into()
627619
}
628620

629621
/// Derives [`num_traits::Zero`][zero] for newtypes. The inner type must already implement `Zero`.
@@ -650,7 +642,7 @@ pub fn zero(input: TokenStream) -> TokenStream {
650642
}
651643
};
652644

653-
import.wrap("Zero", name, impl_).into()
645+
import.wrap(impl_).into()
654646
}
655647

656648
/// Derives [`num_traits::One`][one] for newtypes. The inner type must already implement `One`.
@@ -677,7 +669,7 @@ pub fn one(input: TokenStream) -> TokenStream {
677669
}
678670
};
679671

680-
import.wrap("One", name, impl_).into()
672+
import.wrap(impl_).into()
681673
}
682674

683675
/// Derives [`num_traits::Num`][num] for newtypes. The inner type must already implement `Num`.
@@ -701,7 +693,7 @@ pub fn num(input: TokenStream) -> TokenStream {
701693
}
702694
};
703695

704-
import.wrap("Num", name, impl_).into()
696+
import.wrap(impl_).into()
705697
}
706698

707699
/// Derives [`num_traits::Float`][float] for newtypes. The inner type must already implement
@@ -950,7 +942,7 @@ pub fn float(input: TokenStream) -> TokenStream {
950942
}
951943
};
952944

953-
import.wrap("Float", name, impl_).into()
945+
import.wrap(impl_).into()
954946
}
955947

956948
/// Derives [`num_traits::Signed`][signed] for newtypes. The inner type must already implement
@@ -990,7 +982,7 @@ pub fn signed(input: TokenStream) -> TokenStream {
990982
}
991983
};
992984

993-
import.wrap("Signed", &name, impl_).into()
985+
import.wrap(impl_).into()
994986
}
995987

996988
/// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement
@@ -1008,5 +1000,5 @@ pub fn unsigned(input: TokenStream) -> TokenStream {
10081000
impl #import::Unsigned for #name {}
10091001
};
10101002

1011-
import.wrap("Unsigned", &name, impl_).into()
1003+
import.wrap(impl_).into()
10121004
}

0 commit comments

Comments
 (0)