-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[derive(FromPrimitive)] panics on pub(crate) #2
Comments
From @hcpl on December 14, 2017 16:3 Applying this patch to diff --git a/derive/Cargo.toml b/derive/Cargo.toml
index 2a17ce0..94cf68a 100644
--- a/derive/Cargo.toml
+++ b/derive/Cargo.toml
@@ -11,8 +11,8 @@ repository = "https://github.com/rust-num/num"
version = "0.1.41"
[dependencies]
-quote = "0.1.3"
-syn = "0.7.0"
+quote = "0.3.15"
+syn = "0.11.11"
[dev-dependencies]
compiletest_rs = "0.2.5"
diff --git a/derive/src/lib.rs b/derive/src/lib.rs
index e75fa06..8c0f420 100644
--- a/derive/src/lib.rs
+++ b/derive/src/lib.rs
@@ -42,8 +42,10 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
panic!("`FromPrimitive` can be applied only to unitary enums, {}::{} is either struct or tuple", name, ident)
},
}
- if let Some(val) = variant.discriminant {
- idx = val.value;
+ if let Some(ref val) = variant.discriminant {
+ if let syn::ConstExpr::Lit(syn::Lit::Int(value, _)) = *val {
+ idx = value;
+ }
}
let tt = quote!(#idx => Some(#name::#ident));
idx += 1;
@@ -91,8 +93,10 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
panic!("`ToPrimitive` can be applied only to unitary enums, {}::{} is either struct or tuple", name, ident)
},
}
- if let Some(val) = variant.discriminant {
- idx = val.value;
+ if let Some(ref val) = variant.discriminant {
+ if let syn::ConstExpr::Lit(syn::Lit::Int(value, _)) = *val {
+ idx = value;
+ }
}
let tt = quote!(#name::#ident => #idx);
idx += 1; @cuviper, what do you think about this code and related compatibility issues? If everything is ok, I'll send a PR containing this patch. |
From @hcpl on December 14, 2017 16:50 And I'd like to mention that #[macro_use]
extern crate num_derive;
#[derive(Debug, FromPrimitive)]
pub(crate) enum Foo {
A = 1,
B = 2,
} doesn't work |
The code is fine with me if it works. What exactly are the compatibility issues? I think bumping semver on
I don't think But I think it would be OK to make this depend on |
From @hcpl on December 14, 2017 17:28
Well, my question was more like "is there any possibility for breaking changes to happen" if this patch gets accepted. Though you already said this is a non-issue, so we're good.
Yeah, that was my mistake (funny thing, I crossed my part out right after you posted the comment 😄). The thing is, we could make const _IMPL_NUM_FROM_PRIMITIVE_FOR_Foo: () = {
extern crate num_traits as _num_traits;
/* ... */
}; like Replacing |
It probably just depends on what the rustc minimum is for the newer syn/quote, but it looks like they both have CI for 1.15.1, so that should be fine. We could still bump for num-traits though.
I'm happy to follow serde's lead on this. |
3: Update deps and derivation algorithm r=cuviper a=hcpl Fixes #2. An updated version of rust-num/num#353 which includes suggestions outlined [here](rust-num/num#353 (review)) and [here](https://github.com/rust-num/num/pull/353/files/76b5b2189f2b45e864e14c38c7856be578125931#r157100221): - Update `quote` and `syn` to parse new Rust syntax; - Update `compiletest_rs` to solve Manishearth/compiletest-rs#86; - Add support for arbitrary constant expressions as enum discriminants; - Remove the need to `extern crate num` just for deriving. Some notes: - `#[derive(FromPrimitive)]` now uses if-else to do its job because non-literal expressions are not allowed for pattern matching. - I added tests for self-containment of `#[derive]` alongside the code testing derivation functionality to keep the tests small. Would it be better to separate concerns? - `with_custom_value` should have all three tests like `trivial`.
From @hmvp on December 14, 2017 15:6
This does not compile:
removing
(crate)
makes it workCopied from original issue: rust-num/num#352
The text was updated successfully, but these errors were encountered: