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

Derive support for custom types #5

Closed
4 tasks done
jtempest opened this issue May 16, 2020 · 1 comment
Closed
4 tasks done

Derive support for custom types #5

jtempest opened this issue May 16, 2020 · 1 comment

Comments

@jtempest
Copy link
Owner

jtempest commented May 16, 2020

Traits:

It would be useful to be able to derive implementations for simple types that are composed of already comparable types, for example:

#[derive(FloatUlps, FloatDiff, FloatEq, FloatEqDebug)]
struct MyComplex32 {
    re: f32,
    im: f32,
}

Would generate the following:

struct MyComplex32Ulps {
    re: Ulps<f32>,
    im: Ulps<f32>,
}

impl FloatUlps for MyComplex32 {
    type Ulps = MyComplex32Ulps;
}

impl FloatDiff for MyComplex32 {
    type Output = Self;

    fn abs_diff(self, other: Self) -> Self::Output{
        Self::Output {
            re: self.re.abs_diff(other.re),
            im: self.im.abs_diff(other.im),
        }
    }

    fn ulps_diff(self, other: Self) -> Option<Self::UlpsDiff> {
        Some(MyComplex32Ulps {
            re: self.re.ulps_diff(other.re)?,
            im: self.im.ulps_diff(other.im)?,
        })
    }
}

impl FloatEq for MyComplex32 {
    type Epsilon = <f32 as FloatEq>::DiffEpsilon;

    fn eq_abs(&self, other: &Self, max_diff: &Self::DiffEpsilon) -> bool {
        self.re.eq_abs(&other.re, max_diff) && self.im.eq_abs(&other.im, max_diff)
    }

    fn eq_rel(&self, other: &Self, max_diff: &Self::DiffEpsilon) -> bool {
        self.re.eq_rel(&other.re, max_diff) && self.im.eq_rel(&other.im, max_diff)
    }

    fn eq_ulps(&self, other: &Self, max_diff: &Ulps<Self::Epsilon>) -> bool {
        self.re.eq_ulps(&other.re, max_diff) && self.im.eq_ulps(&other.im, max_diff)
    }
}

impl FloatEqDebug for MyComplex32 {
    type DebugEpsilon = Self;

    fn debug_abs_epsilon(&self, other: &Self, max_diff: &Self::DiffEpsilon) -> Self::DebugEpsilon {
        MyComplex32 {
            re: self.re.debug_abs_epsilon(&other.re, max_diff),
            im: self.im.debug_abs_epsilon(&other.im, max_diff),
        }
    }

    fn debug_rel_epsilon(&self, other: &Self, max_diff: &Self::DiffEpsilon) -> Self::DebugEpsilon {
        MyComplex32 {
            re: self.re.debug_rel_epsilon(&other.re, max_diff),
            im: self.im.debug_rel_epsilon(&other.im, max_diff),
        }
    }

    fn debug_ulps_epsilon(
        &self,
        other: &Self,
        max_diff: &Ulps<Self::Epsilon>,
    ) -> Ulps<Self::DebugEpsilon> {
        MyComplex32Ulps {
            re: self.re.debug_ulps_epsilon(&other.re, max_diff),
            im: self.im.debug_ulps_epsilon(&other.im, max_diff),
        }
    }
}
@jtempest jtempest changed the title Derive support for homogeneous custom types Derive support for custom types May 28, 2020
@jtempest jtempest added enhancement New feature or request and removed enhancement New feature or request labels Jun 12, 2020
@jtempest
Copy link
Owner Author

Implemented for all float_eq traits in 4cfdcad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant