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

Macros for numeric trait impls #162

Merged
merged 1 commit into from May 1, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -15,33 +15,22 @@ pub trait ApproxEq<Eps> {
fn approx_eq_eps(&self, other: &Self, approx_epsilon: &Eps) -> bool;
}

impl ApproxEq<f32> for f32 {
#[inline]
fn approx_epsilon() -> f32 { 1.0e-6 }

#[inline]
fn approx_eq(&self, other: &f32) -> bool {
self.approx_eq_eps(other, &1.0e-6)
}

#[inline]
fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool {
(*self - *other).abs() < *approx_epsilon
}
macro_rules! approx_eq {
($ty:ty, $eps:expr) => (
impl ApproxEq<$ty> for $ty {
#[inline]
fn approx_epsilon() -> $ty { $eps }
#[inline]
fn approx_eq(&self, other: &$ty) -> bool {
self.approx_eq_eps(other, &$eps)
}
#[inline]
fn approx_eq_eps(&self, other: &$ty, approx_epsilon: &$ty) -> bool {
(*self - *other).abs() < *approx_epsilon
}
}
)
}


impl ApproxEq<f64> for f64 {
#[inline]
fn approx_epsilon() -> f64 { 1.0e-6 }

#[inline]
fn approx_eq(&self, other: &f64) -> bool {
self.approx_eq_eps(other, &1.0e-6)
}

#[inline]
fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
(*self - *other).abs() < *approx_epsilon
}
}
approx_eq!(f32, 1.0e-6);
approx_eq!(f64, 1.0e-6);
@@ -27,40 +27,51 @@ impl<T: num_traits::One> One for T {
fn one() -> T { num_traits::One::one() }
}


pub trait Round : Copy { fn round(self) -> Self; }
pub trait Floor : Copy { fn floor(self) -> Self; }
pub trait Ceil : Copy { fn ceil(self) -> Self; }

impl Round for f32 { fn round(self) -> Self { self.round() } }
impl Round for f64 { fn round(self) -> Self { self.round() } }
impl Round for i16 { fn round(self) -> Self { self } }
impl Round for u16 { fn round(self) -> Self { self } }
impl Round for i32 { fn round(self) -> Self { self } }
impl Round for i64 { fn round(self) -> Self { self } }
impl Round for u32 { fn round(self) -> Self { self } }
impl Round for u64 { fn round(self) -> Self { self } }
impl Round for usize { fn round(self) -> Self { self } }
impl Round for isize { fn round(self) -> Self { self } }

impl Floor for f32 { fn floor(self) -> Self { self.floor() } }
impl Floor for f64 { fn floor(self) -> Self { self.floor() } }
impl Floor for i16 { fn floor(self) -> Self { self } }
impl Floor for u16 { fn floor(self) -> Self { self } }
impl Floor for i32 { fn floor(self) -> Self { self } }
impl Floor for i64 { fn floor(self) -> Self { self } }
impl Floor for u32 { fn floor(self) -> Self { self } }
impl Floor for u64 { fn floor(self) -> Self { self } }
impl Floor for usize { fn floor(self) -> Self { self } }
impl Floor for isize { fn floor(self) -> Self { self } }

impl Ceil for f32 { fn ceil(self) -> Self { self.ceil() } }
impl Ceil for f64 { fn ceil(self) -> Self { self.ceil() } }
impl Ceil for i16 { fn ceil(self) -> Self { self } }
impl Ceil for u16 { fn ceil(self) -> Self { self } }
impl Ceil for i32 { fn ceil(self) -> Self { self } }
impl Ceil for i64 { fn ceil(self) -> Self { self } }
impl Ceil for u32 { fn ceil(self) -> Self { self } }
impl Ceil for u64 { fn ceil(self) -> Self { self } }
impl Ceil for usize { fn ceil(self) -> Self { self } }
impl Ceil for isize { fn ceil(self) -> Self { self } }
macro_rules! num_int {
($ty:ty) => (
impl Round for $ty {
#[inline]
fn round(self) -> $ty { self }
}
impl Floor for $ty {
#[inline]
fn floor(self) -> $ty { self }
}
impl Ceil for $ty {
#[inline]
fn ceil(self) -> $ty { self }
}
)
}
macro_rules! num_float {
($ty:ty) => (
impl Round for $ty {
#[inline]
fn round(self) -> $ty { self.round() }
}
impl Floor for $ty {
#[inline]
fn floor(self) -> $ty { self.floor() }
}
impl Ceil for $ty {
#[inline]
fn ceil(self) -> $ty { self.ceil() }
}
)
}

num_int!(i16);
num_int!(u16);
num_int!(i32);
num_int!(u32);
num_int!(i64);
num_int!(u64);
num_int!(isize);
num_int!(usize);
num_float!(f32);
num_float!(f64);
@@ -15,36 +15,18 @@ pub trait Trig {
fn tan(self) -> Self;
}

impl Trig for f32 {
#[inline]
fn sin(self) -> f32 {
self.sin()
}

#[inline]
fn cos(self) -> f32 {
self.cos()
}

#[inline]
fn tan(self) -> f32 {
self.tan()
}
macro_rules! trig {
($ty:ty) => (
impl Trig for $ty {
#[inline]
fn sin(self) -> $ty { self.sin() }
#[inline]
fn cos(self) -> $ty { self.cos() }
#[inline]
fn tan(self) -> $ty { self.tan() }
}
)
}

impl Trig for f64 {
#[inline]
fn sin(self) -> f64 {
self.sin()
}

#[inline]
fn cos(self) -> f64 {
self.cos()
}

#[inline]
fn tan(self) -> f64 {
self.tan()
}
}
trig!(f32);
trig!(f64);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.