Skip to content
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: update dependencies #353

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extern crate proc_macro;
use proc_macro::TokenStream;

use syn::Body::Enum;
use syn::ConstExpr::Lit;
use syn::Lit::Int;
use syn::VariantData::Unit;

#[proc_macro_derive(FromPrimitive)]
Expand All @@ -42,8 +44,8 @@ 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(Lit(Int(value, _))) = variant.discriminant {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we support the full ConstExpr, instead of picking it apart? This will fail on even simple values like -100 which will map to something like Unary(Neg, Lit(Int(100))). Nevermind more complicated expressions. (It seems the old syn just failed to parse it at all.)

I'm thinking instead of idx, we store the actual expr and an offset 0, which we encode as quote!(... #expr + #offset ...). If the next variant has a discriminant, update expr and set offset back to 0, otherwise reuse the former expr with an incremented offset.

idx = value;
}
let tt = quote!(#idx => Some(#name::#ident));
idx += 1;
Expand Down Expand Up @@ -91,8 +93,8 @@ 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(Lit(Int(value, _))) = variant.discriminant {
idx = value;
}
let tt = quote!(#name::#ident => #idx);
idx += 1;
Expand Down