Skip to content

Commit 8711561

Browse files
committed
Merge #11
11: Use num-traits, and release num-derive 0.2.0 r=cuviper a=cuviper This updates the implementation to use `num-traits` instead of the full `num` dependency. However, this is a breaking change, because this crate dependency will usually not be found unless it is explicitly listed in the user's `Cargo.toml`.
2 parents cb67715 + 11d7890 commit 8711561

File tree

10 files changed

+159
-133
lines changed

10 files changed

+159
-133
lines changed

Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ categories = [ "science" ]
88
license = "MIT/Apache-2.0"
99
name = "num-derive"
1010
repository = "https://github.com/rust-num/num-derive"
11-
version = "0.1.44"
11+
version = "0.2.0"
1212
readme = "README.md"
1313

1414
[dependencies]
15+
num-traits = "0.2"
1516
proc-macro2 = "0.2.1"
1617
quote = "0.4.2"
1718
syn = "0.12.7"
1819

1920
[dev-dependencies]
20-
compiletest_rs = "0.3.5"
21-
22-
[dev-dependencies.num]
23-
version = "0.1"
21+
num = "0.1"
2422

2523
[features]
2624
full-syntax = ["syn/full"]

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Add this to your `Cargo.toml`:
1212

1313
```toml
1414
[dependencies]
15-
num = "0.1"
16-
num-derive = "0.1"
15+
num-traits = "0.2"
16+
num-derive = "0.2"
1717
```
1818

1919
and this to your crate root:
@@ -23,6 +23,17 @@ and this to your crate root:
2323
extern crate num_derive;
2424
```
2525

26+
Then you can derive traits on your own types:
27+
28+
```rust
29+
#[derive(FromPrimitive, ToPrimitive)]
30+
enum Color {
31+
Red,
32+
Blue,
33+
Green,
34+
}
35+
```
36+
2637
## Optional features
2738

2839
- **`full-syntax`** — Enables `num-derive` to handle enum discriminants

RELEASES.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
# Release 0.2.0
2+
3+
- [Discriminant matching is now simplified][10], casting values directly by
4+
name, rather than trying to compute offsets from known values manually.
5+
- **breaking change**: [Derivations now import the traits from `num-traits`][11]
6+
instead of the full `num` crate. These are still compatible, but users need
7+
to have an explicit `num-traits = "0.2"` dependency in their `Cargo.toml`.
8+
9+
[10]: https://github.com/rust-num/num-derive/pull/10
10+
[11]: https://github.com/rust-num/num-derive/pull/11
11+
12+
113
# Release 0.1.44
214

315
- [The derived code now explicitly allows `unused_qualifications`][9], so users
416
that globally deny that lint don't encounter an error.
517

618
[9]: https://github.com/rust-num/num-derive/pull/9
719

20+
821
# Release 0.1.43
922

1023
- [The derived code now explicitly allows `trivial_numeric_casts`][7], so users
1124
that globally deny that lint don't encounter an error.
1225

1326
[7]: https://github.com/rust-num/num-derive/pull/7
1427

28+
1529
# Release 0.1.42
1630

1731
- [num-derive now has its own source repository][num-356] at [rust-num/num-derive][home].

ci/test_full.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ set -ex
44

55
echo Testing num-derive on rustc ${TRAVIS_RUST_VERSION}
66

7-
# num-derive should build everywhere.
7+
# num-derive should build and test everywhere.
88
cargo build --verbose --features="$FEATURES"
9-
10-
# We have no features to test...
11-
12-
13-
if [ "$TRAVIS_RUST_VERSION" != nightly ]; then exit; fi
14-
15-
# num-derive testing requires compiletest_rs, which requires nightly
169
cargo test --verbose --features="$FEATURES"

src/lib.rs

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,34 @@
99
// except according to those terms.
1010

1111
#![crate_type = "proc-macro"]
12-
#![doc(html_root_url = "https://docs.rs/num-derive/0.1")]
12+
#![doc(html_root_url = "https://docs.rs/num-derive/0.2")]
13+
14+
//! Procedural macros to derive numeric traits in Rust.
15+
//!
16+
//! ## Usage
17+
//!
18+
//! Add this to your `Cargo.toml`:
19+
//!
20+
//! ```toml
21+
//! [dependencies]
22+
//! num-traits = "0.2"
23+
//! num-derive = "0.2"
24+
//! ```
25+
//!
26+
//! Then you can derive traits on your own types:
27+
//!
28+
//! ```rust
29+
//! #[macro_use]
30+
//! extern crate num_derive;
31+
//!
32+
//! #[derive(FromPrimitive, ToPrimitive)]
33+
//! enum Color {
34+
//! Red,
35+
//! Blue,
36+
//! Green,
37+
//! }
38+
//! # fn main() {}
39+
//! ```
1340
1441
extern crate proc_macro;
1542

@@ -23,6 +50,54 @@ use proc_macro2::Span;
2350

2451
use syn::{Data, Fields, Ident};
2552

53+
/// Derives [`num_traits::FromPrimitive`][from] for simple enums.
54+
///
55+
/// [from]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.FromPrimitive.html
56+
///
57+
/// # Examples
58+
///
59+
/// Simple enums can be derived:
60+
///
61+
/// ```rust
62+
/// # #[macro_use]
63+
/// # extern crate num_derive;
64+
///
65+
/// #[derive(FromPrimitive)]
66+
/// enum Color {
67+
/// Red,
68+
/// Blue,
69+
/// Green = 42,
70+
/// }
71+
/// # fn main() {}
72+
/// ```
73+
///
74+
/// Enums that contain data are not allowed:
75+
///
76+
/// ```compile_fail
77+
/// # #[macro_use]
78+
/// # extern crate num_derive;
79+
///
80+
/// #[derive(FromPrimitive)]
81+
/// enum Color {
82+
/// Rgb(u8, u8, u8),
83+
/// Hsv(u8, u8, u8),
84+
/// }
85+
/// # fn main() {}
86+
/// ```
87+
///
88+
/// Structs are not allowed:
89+
///
90+
/// ```compile_fail
91+
/// # #[macro_use]
92+
/// # extern crate num_derive;
93+
/// #[derive(FromPrimitive)]
94+
/// struct Color {
95+
/// r: u8,
96+
/// g: u8,
97+
/// b: u8,
98+
/// }
99+
/// # fn main() {}
100+
/// ```
26101
#[proc_macro_derive(FromPrimitive)]
27102
pub fn from_primitive(input: TokenStream) -> TokenStream {
28103
let ast: syn::DeriveInput = syn::parse(input).unwrap();
@@ -59,9 +134,9 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
59134
#[allow(non_upper_case_globals)]
60135
#[allow(unused_qualifications)]
61136
const #dummy_const: () = {
62-
extern crate num as _num;
137+
extern crate num_traits as _num_traits;
63138

64-
impl _num::traits::FromPrimitive for #name {
139+
impl _num_traits::FromPrimitive for #name {
65140
#[allow(trivial_numeric_casts)]
66141
fn from_i64(#from_i64_var: i64) -> Option<Self> {
67142
#(#clauses else)* {
@@ -79,6 +154,54 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
79154
res.into()
80155
}
81156

157+
/// Derives [`num_traits::ToPrimitive`][to] for simple enums.
158+
///
159+
/// [to]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.ToPrimitive.html
160+
///
161+
/// # Examples
162+
///
163+
/// Simple enums can be derived:
164+
///
165+
/// ```rust
166+
/// # #[macro_use]
167+
/// # extern crate num_derive;
168+
///
169+
/// #[derive(ToPrimitive)]
170+
/// enum Color {
171+
/// Red,
172+
/// Blue,
173+
/// Green = 42,
174+
/// }
175+
/// # fn main() {}
176+
/// ```
177+
///
178+
/// Enums that contain data are not allowed:
179+
///
180+
/// ```compile_fail
181+
/// # #[macro_use]
182+
/// # extern crate num_derive;
183+
///
184+
/// #[derive(ToPrimitive)]
185+
/// enum Color {
186+
/// Rgb(u8, u8, u8),
187+
/// Hsv(u8, u8, u8),
188+
/// }
189+
/// # fn main() {}
190+
/// ```
191+
///
192+
/// Structs are not allowed:
193+
///
194+
/// ```compile_fail
195+
/// # #[macro_use]
196+
/// # extern crate num_derive;
197+
/// #[derive(ToPrimitive)]
198+
/// struct Color {
199+
/// r: u8,
200+
/// g: u8,
201+
/// b: u8,
202+
/// }
203+
/// # fn main() {}
204+
/// ```
82205
#[proc_macro_derive(ToPrimitive)]
83206
pub fn to_primitive(input: TokenStream) -> TokenStream {
84207
let ast: syn::DeriveInput = syn::parse(input).unwrap();
@@ -124,9 +247,9 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
124247
#[allow(non_upper_case_globals)]
125248
#[allow(unused_qualifications)]
126249
const #dummy_const: () = {
127-
extern crate num as _num;
250+
extern crate num_traits as _num_traits;
128251

129-
impl _num::traits::ToPrimitive for #name {
252+
impl _num_traits::ToPrimitive for #name {
130253
#[allow(trivial_numeric_casts)]
131254
fn to_i64(&self) -> Option<i64> {
132255
#match_expr

tests/compile-fail/from-primitive/derive_on_struct.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/compile-fail/from-primitive/enum_with_associated_data.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/compile-fail/to-primitive/derive_on_struct.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/compile-fail/to-primitive/enum_with_associated_data.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/compiletest.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)