dssim_core/
val.rs

1#![allow(dead_code)]
2
3use std::cmp::Ordering;
4use std::fmt;
5use std::ops::{Add, Div, Mul, Sub};
6
7/// Result of comparison as `f64`
8#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
9pub struct Dssim(f64);
10
11impl Dssim {
12    #[must_use]
13    pub fn new(v: f64) -> Self {
14        debug_assert!(v.is_finite());
15        Self(v)
16    }
17}
18
19impl fmt::Display for Dssim {
20    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
21        write!(fmt, "{:.*}", fmt.precision().unwrap_or(6), self.0)
22    }
23}
24
25impl Eq for Dssim {
26    // Result never has NaN
27}
28
29impl PartialEq<Dssim> for f64 {
30    fn eq(&self, other: &Dssim) -> bool {
31        *self == other.0
32    }
33}
34
35impl PartialEq<f64> for Dssim {
36    fn eq(&self, other: &f64) -> bool {
37        self.0 == *other
38    }
39}
40
41impl From<Dssim> for f64 {
42    fn from(s: Dssim) -> f64 {
43        s.0
44    }
45}
46
47impl From<f64> for Dssim {
48    fn from(s: f64) -> Self {
49        Self(s)
50    }
51}
52
53impl<RHS: Into<f64>> Sub<RHS> for Dssim {
54    type Output = f64;
55
56    fn sub(self, r: RHS) -> Self::Output {
57        let rval = r.into();
58        debug_assert!(rval.is_finite());
59        self.0.sub(rval)
60    }
61}
62
63impl Sub<Dssim> for f64 {
64    type Output = f64;
65
66    fn sub(self, r: Dssim) -> Self::Output {
67        self.sub(r.0)
68    }
69}
70
71impl<RHS: Into<f64>> Add<RHS> for Dssim {
72    type Output = f64;
73
74    fn add(self, r: RHS) -> Self::Output {
75        let rval = r.into();
76        debug_assert!(rval.is_finite());
77        self.0.add(rval)
78    }
79}
80
81impl Add<Dssim> for f64 {
82    type Output = f64;
83
84    fn add(self, r: Dssim) -> Self::Output {
85        self.add(r.0)
86    }
87}
88
89impl<RHS: Into<f64>> Mul<RHS> for Dssim {
90    type Output = Dssim;
91
92    fn mul(self, r: RHS) -> Self::Output {
93        let rval = r.into();
94        debug_assert!(rval.is_finite());
95        self.0.mul(rval).into()
96    }
97}
98
99impl Mul<Dssim> for f64 {
100    type Output = Dssim;
101
102    fn mul(self, r: Dssim) -> Self::Output {
103        self.mul(r.0).into()
104    }
105}
106
107impl<RHS: Into<f64>> Div<RHS> for Dssim {
108    type Output = f64;
109
110    fn div(self, r: RHS) -> Self::Output {
111        let rval = r.into();
112        debug_assert!(rval.is_finite() && rval != 0.);
113        self.0.div(rval)
114    }
115}
116
117impl Div<Dssim> for f64 {
118    type Output = f64;
119
120    fn div(self, r: Dssim) -> Self::Output {
121        debug_assert!(r.0 != 0.);
122        self.div(r.0)
123    }
124}
125
126impl PartialOrd<f64> for Dssim {
127    fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
128        self.0.partial_cmp(other)
129    }
130
131    fn lt(&self, other: &f64) -> bool { self.0.lt(other) }
132    fn le(&self, other: &f64) -> bool { self.0.le(other) }
133    fn gt(&self, other: &f64) -> bool { self.0.gt(other) }
134    fn ge(&self, other: &f64) -> bool { self.0.ge(other) }
135}
136
137impl PartialOrd<Dssim> for f64 {
138    fn partial_cmp(&self, other: &Dssim) -> Option<Ordering> {
139        self.partial_cmp(&other.0)
140    }
141
142    fn lt(&self, other: &Dssim) -> bool { self.lt(&other.0) }
143    fn le(&self, other: &Dssim) -> bool { self.le(&other.0) }
144    fn gt(&self, other: &Dssim) -> bool { self.gt(&other.0) }
145    fn ge(&self, other: &Dssim) -> bool { self.ge(&other.0) }
146}