Skip to content

Commit

Permalink
Derive Float for newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
asayers committed Sep 29, 2018
1 parent 8b47c68 commit ea052f2
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 2 deletions.
186 changes: 185 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

//! Procedural macros to derive numeric traits in Rust.
//!
Expand Down Expand Up @@ -515,3 +515,187 @@ pub fn num(input: TokenStream) -> TokenStream {
}
}).into()
}

#[proc_macro_derive(Float)]
pub fn float(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let name = &ast.ident;
let inner_ty = newtype_inner(&ast.data).expect(NEWTYPE_ONLY);
dummy_const_trick("Float", &name, quote! {
extern crate num_traits as _num_traits;
impl _num_traits::Float for #name {
fn nan() -> Self {
#name(<#inner_ty as _num_traits::Float>::nan())
}
fn infinity() -> Self {
#name(<#inner_ty as _num_traits::Float>::infinity())
}
fn neg_infinity() -> Self {
#name(<#inner_ty as _num_traits::Float>::neg_infinity())
}
fn neg_zero() -> Self {
#name(<#inner_ty as _num_traits::Float>::neg_zero())
}
fn min_value() -> Self {
#name(<#inner_ty as _num_traits::Float>::min_value())
}
fn min_positive_value() -> Self {
#name(<#inner_ty as _num_traits::Float>::min_positive_value())
}
fn max_value() -> Self {
#name(<#inner_ty as _num_traits::Float>::max_value())
}
fn is_nan(self) -> bool {
<#inner_ty as _num_traits::Float>::is_nan(self.0)
}
fn is_infinite(self) -> bool {
<#inner_ty as _num_traits::Float>::is_infinite(self.0)
}
fn is_finite(self) -> bool {
<#inner_ty as _num_traits::Float>::is_finite(self.0)
}
fn is_normal(self) -> bool {
<#inner_ty as _num_traits::Float>::is_normal(self.0)
}
fn classify(self) -> ::std::num::FpCategory {
<#inner_ty as _num_traits::Float>::classify(self.0)
}
fn floor(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::floor(self.0))
}
fn ceil(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::ceil(self.0))
}
fn round(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::round(self.0))
}
fn trunc(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::trunc(self.0))
}
fn fract(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::fract(self.0))
}
fn abs(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::abs(self.0))
}
fn signum(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::signum(self.0))
}
fn is_sign_positive(self) -> bool {
<#inner_ty as _num_traits::Float>::is_sign_positive(self.0)
}
fn is_sign_negative(self) -> bool {
<#inner_ty as _num_traits::Float>::is_sign_negative(self.0)
}
fn mul_add(self, a: Self, b: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::mul_add(self.0, a.0, b.0))
}
fn recip(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::recip(self.0))
}
fn powi(self, n: i32) -> Self {
#name(<#inner_ty as _num_traits::Float>::powi(self.0, n))
}
fn powf(self, n: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::powf(self.0, n.0))
}
fn sqrt(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::sqrt(self.0))
}
fn exp(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::exp(self.0))
}
fn exp2(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::exp2(self.0))
}
fn ln(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::ln(self.0))
}
fn log(self, base: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::log(self.0, base.0))
}
fn log2(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::log2(self.0))
}
fn log10(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::log10(self.0))
}
fn max(self, other: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::max(self.0, other.0))
}
fn min(self, other: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::min(self.0, other.0))
}
fn abs_sub(self, other: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::abs_sub(self.0, other.0))
}
fn cbrt(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::cbrt(self.0))
}
fn hypot(self, other: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::hypot(self.0, other.0))
}
fn sin(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::sin(self.0))
}
fn cos(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::cos(self.0))
}
fn tan(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::tan(self.0))
}
fn asin(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::asin(self.0))
}
fn acos(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::acos(self.0))
}
fn atan(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::atan(self.0))
}
fn atan2(self, other: Self) -> Self {
#name(<#inner_ty as _num_traits::Float>::atan2(self.0, other.0))
}
fn sin_cos(self) -> (Self, Self) {
let (x, y) = <#inner_ty as _num_traits::Float>::sin_cos(self.0);
(#name(x), #name(y))
}
fn exp_m1(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::exp_m1(self.0))
}
fn ln_1p(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::ln_1p(self.0))
}
fn sinh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::sinh(self.0))
}
fn cosh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::cosh(self.0))
}
fn tanh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::tanh(self.0))
}
fn asinh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::asinh(self.0))
}
fn acosh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::acosh(self.0))
}
fn atanh(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::atanh(self.0))
}
fn integer_decode(self) -> (u64, i16, i8) {
<#inner_ty as _num_traits::Float>::integer_decode(self.0)
}
fn epsilon() -> Self {
#name(<#inner_ty as _num_traits::Float>::epsilon())
}
fn to_degrees(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::to_degrees(self.0))
}
fn to_radians(self) -> Self {
#name(<#inner_ty as _num_traits::Float>::to_radians(self.0))
}
}
}).into()
}
16 changes: 15 additions & 1 deletion tests/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ extern crate num as num_renamed;
#[macro_use]
extern crate num_derive;

use num_renamed::{FromPrimitive, ToPrimitive, NumCast, One, Zero, Num};
use num_renamed::{FromPrimitive, ToPrimitive, NumCast, One, Zero, Num, Float};
use std::ops::Neg;

#[derive(
Debug,
Expand All @@ -17,9 +18,17 @@ use num_renamed::{FromPrimitive, ToPrimitive, NumCast, One, Zero, Num};
One,
Zero,
Num,
Float,
)]
struct MyFloat(f64);

impl Neg for MyFloat {
type Output = MyFloat;
fn neg(self) -> Self {
MyFloat(self.0.neg())
}
}

#[test]
fn test_from_primitive() {
assert_eq!(MyFloat::from_u32(25), Some(MyFloat(25.0)));
Expand Down Expand Up @@ -58,3 +67,8 @@ fn test_one() {
fn test_num() {
assert_eq!(MyFloat::from_str_radix("25", 10).ok(), Some(MyFloat(25.0)));
}

#[test]
fn test_float() {
assert_eq!(MyFloat(4.0).log(MyFloat(2.0)), MyFloat(2.0));
}

0 comments on commit ea052f2

Please sign in to comment.