Skip to content

Commit b7e6407

Browse files
committed
Add new crate num-derive as an new replacement for num-macro
1 parent 0c89b89 commit b7e6407

12 files changed

+330
-75
lines changed

.travis/test_nightly.sh

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -ex
55
cargo bench --verbose
66

77
cargo test --verbose --manifest-path=macros/Cargo.toml
8+
cargo test --verbose --manifest-path=derive/Cargo.toml
89

910
# Build test for the serde feature
1011
cargo build --verbose --features "serde"

derive/Cargo.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
description = "Numeric syntax extensions"
4+
documentation = "http://rust-num.github.io/num"
5+
homepage = "https://github.com/rust-num/num"
6+
keywords = ["mathematics", "numerics"]
7+
license = "MIT/Apache-2.0"
8+
name = "num-derive"
9+
repository = "https://github.com/rust-num/num"
10+
version = "0.1.33"
11+
12+
[dependencies]
13+
quote = "0.1.3"
14+
syn = "0.7.0"
15+
16+
[dev-dependencies]
17+
compiletest_rs = "0.2.2"
18+
19+
[dev-dependencies.num]
20+
path = ".."
21+
version = "0.1"
22+
23+
[lib]
24+
crate-type = ["rustc-macro"]
25+
name = "num_derive"
26+
rustc-macro = true
27+
test = false

derive/src/lib.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rustc-macro"]
12+
#![feature(rustc_macro, rustc_macro_lib)]
13+
14+
extern crate syn;
15+
#[macro_use]
16+
extern crate quote;
17+
extern crate rustc_macro;
18+
19+
use rustc_macro::TokenStream;
20+
21+
use syn::Body::Enum;
22+
use syn::VariantData::Unit;
23+
24+
#[rustc_macro_derive(FromPrimitive)]
25+
pub fn from_primitive(input: TokenStream) -> TokenStream {
26+
let source = input.to_string();
27+
28+
let ast = syn::parse_macro_input(&source).unwrap();
29+
let name = &ast.ident;
30+
31+
let variants = match ast.body {
32+
Enum(ref variants) => variants,
33+
_ => {
34+
panic!("`FromPrimitive` can be applied only to the enums, {} is not an enum",
35+
name)
36+
}
37+
};
38+
39+
let mut idx = 0;
40+
let variants: Vec<_> = variants.iter()
41+
.map(|variant| {
42+
let ident = &variant.ident;
43+
match variant.data {
44+
Unit => (),
45+
_ => {
46+
panic!("`FromPrimitive` can be applied only to unitary enums, {}::{} is either struct or tuple", name, ident)
47+
},
48+
}
49+
if let Some(val) = variant.discriminant {
50+
idx = val.value;
51+
}
52+
let tt = quote!(#idx => Some(#name::#ident));
53+
idx += 1;
54+
tt
55+
})
56+
.collect();
57+
58+
let res = quote! {
59+
#ast
60+
61+
impl ::num::traits::FromPrimitive for #name {
62+
fn from_i64(n: i64) -> Option<Self> {
63+
Self::from_u64(n as u64)
64+
}
65+
66+
fn from_u64(n: u64) -> Option<Self> {
67+
match n {
68+
#(variants,)*
69+
_ => None,
70+
}
71+
}
72+
}
73+
};
74+
75+
res.to_string().parse().unwrap()
76+
}

macros/tests/compile-fail/derive_on_struct.rs derive/tests/compile-fail/derive_on_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
extern crate num;
1515
#[macro_use]
16-
extern crate num_macros;
16+
extern crate num_derive;
1717

1818
#[derive(Debug, PartialEq, FromPrimitive)] //~ ERROR
1919
struct Color {

macros/tests/compile-fail/enum_with_associated_data.rs derive/tests/compile-fail/enum_with_associated_data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
extern crate num;
1515
#[macro_use]
16-
extern crate num_macros;
16+
extern crate num_derive;
1717

1818
#[derive(Debug, PartialEq, FromPrimitive)] //~ ERROR
1919
enum Color {
File renamed without changes.

macros/tests/empty_enum.rs derive/tests/empty_enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
extern crate num;
1515
#[macro_use]
16-
extern crate num_macros;
16+
extern crate num_derive;
1717

1818
#[derive(Debug, PartialEq, FromPrimitive)]
1919
enum Color {}

macros/tests/trivial.rs derive/tests/trivial.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
extern crate num;
1414
#[macro_use]
15-
extern crate num_macros;
15+
extern crate num_derive;
1616

1717
#[derive(Debug, PartialEq, FromPrimitive)]
1818
enum Color {

macros/tests/with_custom_values.rs derive/tests/with_custom_values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
extern crate num;
1414
#[macro_use]
15-
extern crate num_macros;
15+
extern crate num_derive;
1616

1717
#[derive(Debug, PartialEq, FromPrimitive)]
1818
enum Color {

macros/Cargo.toml

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
[package]
2+
name = "num-macros"
3+
version = "0.1.33"
24
authors = ["The Rust Project Developers"]
3-
description = "Numeric syntax extensions"
4-
documentation = "http://rust-num.github.io/num"
5-
homepage = "https://github.com/rust-num/num"
6-
keywords = ["mathematics", "numerics"]
75
license = "MIT/Apache-2.0"
8-
name = "num-macros"
6+
homepage = "https://github.com/rust-num/num"
97
repository = "https://github.com/rust-num/num"
10-
version = "0.1.33"
11-
12-
[dependencies]
13-
quote = "0.1.3"
14-
syn = "0.6.0"
15-
16-
[dev-dependencies]
17-
compiletest_rs = "0.2.2"
18-
19-
[dev-dependencies.num]
20-
path = ".."
21-
version = "0.1"
8+
documentation = "http://rust-num.github.io/num"
9+
keywords = ["mathematics", "numerics"]
10+
description = "Numeric syntax extensions"
2211

2312
[lib]
24-
crate-type = ["rustc-macro"]
2513
name = "num_macros"
26-
rustc-macro = true
27-
test = false
14+
plugin = true
15+
16+
[dev-dependencies]
17+
num = { path = "..", version = "0.1" }

0 commit comments

Comments
 (0)