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

Use num-traits, and release num-derive 0.2.0 #11

Merged
merged 3 commits into from
Feb 21, 2018
Merged
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
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ categories = [ "science" ]
license = "MIT/Apache-2.0"
name = "num-derive"
repository = "https://github.com/rust-num/num-derive"
version = "0.1.44"
version = "0.2.0"
readme = "README.md"

[dependencies]
num-traits = "0.2"
proc-macro2 = "0.2.1"
quote = "0.4.2"
syn = "0.12.7"

[dev-dependencies]
compiletest_rs = "0.3.5"

[dev-dependencies.num]
version = "0.1"
num = "0.1"

[features]
full-syntax = ["syn/full"]
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
num = "0.1"
num-derive = "0.1"
num-traits = "0.2"
num-derive = "0.2"
```

and this to your crate root:
Expand All @@ -23,6 +23,17 @@ and this to your crate root:
extern crate num_derive;
```

Then you can derive traits on your own types:

```rust
#[derive(FromPrimitive, ToPrimitive)]
enum Color {
Red,
Blue,
Green,
}
```

## Optional features

- **`full-syntax`** — Enables `num-derive` to handle enum discriminants
Expand Down
14 changes: 14 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Release 0.2.0

- [Discriminant matching is now simplified][10], casting values directly by
name, rather than trying to compute offsets from known values manually.
- **breaking change**: [Derivations now import the traits from `num-traits`][11]
instead of the full `num` crate. These are still compatible, but users need
to have an explicit `num-traits = "0.2"` dependency in their `Cargo.toml`.

[10]: https://github.com/rust-num/num-derive/pull/10
[11]: https://github.com/rust-num/num-derive/pull/11


# Release 0.1.44

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

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


# Release 0.1.43

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

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


# Release 0.1.42

- [num-derive now has its own source repository][num-356] at [rust-num/num-derive][home].
Expand Down
9 changes: 1 addition & 8 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ set -ex

echo Testing num-derive on rustc ${TRAVIS_RUST_VERSION}

# num-derive should build everywhere.
# num-derive should build and test everywhere.
cargo build --verbose --features="$FEATURES"

# We have no features to test...


if [ "$TRAVIS_RUST_VERSION" != nightly ]; then exit; fi

# num-derive testing requires compiletest_rs, which requires nightly
cargo test --verbose --features="$FEATURES"
133 changes: 128 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,34 @@
// except according to those terms.

#![crate_type = "proc-macro"]
#![doc(html_root_url = "https://docs.rs/num-derive/0.1")]
#![doc(html_root_url = "https://docs.rs/num-derive/0.2")]

//! Procedural macros to derive numeric traits in Rust.
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! num-traits = "0.2"
//! num-derive = "0.2"
//! ```
//!
//! Then you can derive traits on your own types:
//!
//! ```rust
//! #[macro_use]
//! extern crate num_derive;
//!
//! #[derive(FromPrimitive, ToPrimitive)]
//! enum Color {
//! Red,
//! Blue,
//! Green,
//! }
//! # fn main() {}
//! ```

extern crate proc_macro;

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

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

/// Derives [`num_traits::FromPrimitive`][from] for simple enums.
///
/// [from]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.FromPrimitive.html
///
/// # Examples
///
/// Simple enums can be derived:
///
/// ```rust
/// # #[macro_use]
/// # extern crate num_derive;
///
/// #[derive(FromPrimitive)]
/// enum Color {
/// Red,
/// Blue,
/// Green = 42,
/// }
/// # fn main() {}
/// ```
///
/// Enums that contain data are not allowed:
///
/// ```compile_fail
/// # #[macro_use]
/// # extern crate num_derive;
///
/// #[derive(FromPrimitive)]
/// enum Color {
/// Rgb(u8, u8, u8),
/// Hsv(u8, u8, u8),
/// }
/// # fn main() {}
/// ```
///
/// Structs are not allowed:
///
/// ```compile_fail
/// # #[macro_use]
/// # extern crate num_derive;
/// #[derive(FromPrimitive)]
/// struct Color {
/// r: u8,
/// g: u8,
/// b: u8,
/// }
/// # fn main() {}
/// ```
#[proc_macro_derive(FromPrimitive)]
pub fn from_primitive(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
Expand Down Expand Up @@ -59,9 +134,9 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
#[allow(non_upper_case_globals)]
#[allow(unused_qualifications)]
const #dummy_const: () = {
extern crate num as _num;
extern crate num_traits as _num_traits;

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

/// Derives [`num_traits::ToPrimitive`][to] for simple enums.
///
/// [to]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.ToPrimitive.html
///
/// # Examples
///
/// Simple enums can be derived:
///
/// ```rust
/// # #[macro_use]
/// # extern crate num_derive;
///
/// #[derive(ToPrimitive)]
/// enum Color {
/// Red,
/// Blue,
/// Green = 42,
/// }
/// # fn main() {}
/// ```
///
/// Enums that contain data are not allowed:
///
/// ```compile_fail
/// # #[macro_use]
/// # extern crate num_derive;
///
/// #[derive(ToPrimitive)]
/// enum Color {
/// Rgb(u8, u8, u8),
/// Hsv(u8, u8, u8),
/// }
/// # fn main() {}
/// ```
///
/// Structs are not allowed:
///
/// ```compile_fail
/// # #[macro_use]
/// # extern crate num_derive;
/// #[derive(ToPrimitive)]
/// struct Color {
/// r: u8,
/// g: u8,
/// b: u8,
/// }
/// # fn main() {}
/// ```
#[proc_macro_derive(ToPrimitive)]
pub fn to_primitive(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
Expand Down Expand Up @@ -124,9 +247,9 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
#[allow(non_upper_case_globals)]
#[allow(unused_qualifications)]
const #dummy_const: () = {
extern crate num as _num;
extern crate num_traits as _num_traits;

impl _num::traits::ToPrimitive for #name {
impl _num_traits::ToPrimitive for #name {
#[allow(trivial_numeric_casts)]
fn to_i64(&self) -> Option<i64> {
#match_expr
Expand Down
22 changes: 0 additions & 22 deletions tests/compile-fail/from-primitive/derive_on_struct.rs

This file was deleted.

21 changes: 0 additions & 21 deletions tests/compile-fail/from-primitive/enum_with_associated_data.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/compile-fail/to-primitive/derive_on_struct.rs

This file was deleted.

21 changes: 0 additions & 21 deletions tests/compile-fail/to-primitive/enum_with_associated_data.rs

This file was deleted.

27 changes: 0 additions & 27 deletions tests/compiletest.rs

This file was deleted.