Skip to content

Commit be3163a

Browse files
bors[bot]asayerscuviper
committed
Merge #17
17: Derive various num traits for newtypes r=cuviper a=asayers This PR adds deriving macros for the following traits: * FromPrimitive * ToPrimitive * NumOps<Self, Self> * NumCast * Zero * One * Num * Float These macros only work when applied to a newtype where the inner type already implements the trait in question. They produce the obvious "unwrap-call-rewrap" implementation. The `FromPrimative` and `ToPrimative` macros now handle both newtypes and the enums that they used to handle. The odd one out is `NumOps`, for two reasons. First, `NumOps` is just a trait alias for `Add + Sub + Mul + Div + Rem`, so those are the traits which the macro generates impls for. I suppose it's not a great user experience to say `#[derive(Foo)]` and end up with `impl Bar`, but I think in this case it's just about OK. After all, the user needs only to look at the definition of `NumOps` to know that this is only possible behaviour - there's no other way to get an impl for `NumOps`. The other reason this one is strange is that when the user says `#[derive(NumOps)]`, the macro decides that what they want is an impl for `NumOps<Rhs=Self, Output=Self>`. I think this makes sense though; these are the default type parameters, so when you say `NumOps` unqualified this is what it means. As you can probably tell, my objective here was to get to `Float`. That's why this PR only provides macros for a subset of the traits in `num_traits`. Our codebase has a bunch of newtyped `f64`s, and I don't want people unwrapping them all the time to work with them. ### To-do - [x] docs - [x] tests - [x] RELEASES entry Co-authored-by: Alex Sayers <[email protected]> Co-authored-by: Josh Stone <[email protected]>
2 parents 599b9ef + 706ea88 commit be3163a

File tree

5 files changed

+696
-85
lines changed

5 files changed

+696
-85
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ name = "num-derive"
1010
repository = "https://github.com/rust-num/num-derive"
1111
version = "0.2.2"
1212
readme = "README.md"
13+
build = "build.rs"
1314

1415
[dependencies]
1516
num-traits = "0.2"

RELEASES.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Unreleased
2+
3+
- [Added newtype deriving][17] for `FromPrimitive`, `ToPrimitive`,
4+
`NumOps<Self, Self>`, `NumCast`, `Zero`, `One`, `Num`, and `Float`.
5+
6+
[17]: https://github.com/rust-num/num-derive/pull/17
7+
18
# Release 0.2.2 (2018-05-22)
29

310
- [Updated dependencies][14].

build.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::env;
2+
use std::io::Write;
3+
use std::process::{Command, Stdio};
4+
5+
fn main() {
6+
if probe("fn main() { 0i128; }") {
7+
println!("cargo:rustc-cfg=has_i128");
8+
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
9+
panic!("i128 support was not detected!");
10+
}
11+
}
12+
13+
/// Test if a code snippet can be compiled
14+
fn probe(code: &str) -> bool {
15+
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
16+
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");
17+
18+
let mut child = Command::new(rustc)
19+
.arg("--out-dir")
20+
.arg(out_dir)
21+
.arg("--emit=obj")
22+
.arg("-")
23+
.stdin(Stdio::piped())
24+
.spawn()
25+
.expect("rustc probe");
26+
27+
child
28+
.stdin
29+
.as_mut()
30+
.expect("rustc stdin")
31+
.write_all(code.as_bytes())
32+
.expect("write rustc stdin");
33+
34+
child.wait().expect("rustc probe").success()
35+
}

0 commit comments

Comments
 (0)