1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
use crate::*;

pub(crate) trait Splat<T> {
    fn splat(val: T) -> Self;
}

impl Splat<f32> for f32 {
    #[inline(always)]
    fn splat(val: f32) -> Self {
        val
    }
}

impl Splat<f64> for f64 {
    #[inline(always)]
    fn splat(val: f64) -> Self {
        val
    }
}

pub trait EqualsEps {
    fn eq_eps(self, other: Self) -> bool;
}

macro_rules! impl_eq_eps_wide {
    ($($t:ident),+) => {
        $(impl EqualsEps for $t {
            fn eq_eps(self, other: Self) -> bool {
                let r = (self - other).abs();
                let eps = $t::splat(0.01);

                r.cmp_ge(eps).none()
            }
        })+
    };
}

impl_eq_eps_wide!(f32x4, f32x8, f64x2, f64x4);

impl EqualsEps for f32 {
    fn eq_eps(self, other: Self) -> bool {
        let diff = (self - other).abs();
        if diff <= 0.01 {
            true
        } else {
            println!(
                "{} should equal {} with epsilon 0.01 but doesn't.",
                self, other
            );
            false
        }
    }
}

impl EqualsEps for f64 {
    fn eq_eps(self, other: Self) -> bool {
        let diff = (self - other).abs();
        if diff <= 0.01 {
            true
        } else {
            println!(
                "{} should equal {} with epsilon 0.01 but doesn't.",
                self, other
            );
            false
        }
    }
}

#[macro_export]
macro_rules! derive_default_identity {
    ($t:ident) => {
        impl Default for $t {
            #[inline]
            fn default() -> Self {
                Self::identity()
            }
        }
    };
}

/// A simple trait extension to simulate `TryFrom` for types that are not from this crate.
pub trait TryFromExt<Source>: Sized {
    type Error;

    fn try_from(source: Source) -> Result<Self, Self::Error>;
}

/// A simple trait extension to simulate `TryInto` for types that are not from this crate.
pub trait TryIntoExt<Target> {
    type Error;

    fn try_into(self) -> Result<Target, Self::Error>;
}

impl<Source, Target, E> TryIntoExt<Target> for Source
where
    Target: TryFromExt<Source, Error = E>,
{
    type Error = E;

    fn try_into(self) -> Result<Target, Self::Error> {
        Target::try_from(self)
    }
}