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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use num::One;
use num_lib::NumCast;
#[cfg(feature = "plugins")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::ops::{Add, Mul, Sub, Div};
use std::marker::PhantomData;
#[derive(Copy, RustcDecodable, RustcEncodable, Debug)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct ScaleFactor<Src, Dst, T>(pub T, PhantomData<(Src, Dst)>);
#[cfg(feature = "plugins")]
impl<Src,Dst,T> Deserialize for ScaleFactor<Src,Dst,T> where T: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<ScaleFactor<Src,Dst,T>,D::Error>
where D: Deserializer {
Ok(ScaleFactor(try!(Deserialize::deserialize(deserializer)), PhantomData))
}
}
#[cfg(feature = "plugins")]
impl<Src,Dst,T> Serialize for ScaleFactor<Src,Dst,T> where T: Serialize {
fn serialize<S>(&self, serializer: &mut S) -> Result<(),S::Error> where S: Serializer {
self.0.serialize(serializer)
}
}
impl<Src, Dst, T> ScaleFactor<Src, Dst, T> {
pub fn new(x: T) -> ScaleFactor<Src, Dst, T> {
ScaleFactor(x, PhantomData)
}
}
impl<Src, Dst, T: Clone> ScaleFactor<Src, Dst, T> {
pub fn get(&self) -> T {
self.0.clone()
}
}
impl<Src, Dst, T: Clone + One + Div<T, Output=T>> ScaleFactor<Src, Dst, T> {
pub fn inv(&self) -> ScaleFactor<Dst, Src, T> {
let one: T = One::one();
ScaleFactor::new(one / self.get())
}
}
impl<A, B, C, T: Clone + Mul<T, Output=T>>
Mul<ScaleFactor<B, C, T>> for ScaleFactor<A, B, T> {
type Output = ScaleFactor<A, C, T>;
#[inline]
fn mul(self, other: ScaleFactor<B, C, T>) -> ScaleFactor<A, C, T> {
ScaleFactor::new(self.get() * other.get())
}
}
impl<Src, Dst, T: Clone + Add<T, Output=T>> Add for ScaleFactor<Src, Dst, T> {
type Output = ScaleFactor<Src, Dst, T>;
#[inline]
fn add(self, other: ScaleFactor<Src, Dst, T>) -> ScaleFactor<Src, Dst, T> {
ScaleFactor::new(self.get() + other.get())
}
}
impl<Src, Dst, T: Clone + Sub<T, Output=T>> Sub for ScaleFactor<Src, Dst, T> {
type Output = ScaleFactor<Src, Dst, T>;
#[inline]
fn sub(self, other: ScaleFactor<Src, Dst, T>) -> ScaleFactor<Src, Dst, T> {
ScaleFactor::new(self.get() - other.get())
}
}
impl<Src, Dst, T0: NumCast + Clone> ScaleFactor<Src, Dst, T0> {
pub fn cast<T1: NumCast + Clone>(&self) -> Option<ScaleFactor<Src, Dst, T1>> {
NumCast::from(self.get()).map(ScaleFactor::new)
}
}
impl<Src, Dst, T: Clone + PartialEq> PartialEq for ScaleFactor<Src, Dst, T> {
fn eq(&self, other: &ScaleFactor<Src, Dst, T>) -> bool {
self.get().eq(&other.get())
}
}
impl<Src, Dst, T: Clone> Clone for ScaleFactor<Src, Dst, T> {
fn clone(&self) -> ScaleFactor<Src, Dst, T> {
ScaleFactor::new(self.get())
}
}
#[cfg(test)]
mod tests {
use super::ScaleFactor;
#[derive(Debug)]
enum Inch {}
#[derive(Debug)]
enum Cm {}
#[derive(Debug)]
enum Mm {}
#[test]
fn test_scale_factor() {
let mm_per_inch: ScaleFactor<Inch, Mm, f32> = ScaleFactor::new(25.4);
let cm_per_mm: ScaleFactor<Mm, Cm, f32> = ScaleFactor::new(0.1);
let mm_per_cm: ScaleFactor<Cm, Mm, f32> = cm_per_mm.inv();
assert_eq!(mm_per_cm.get(), 10.0);
let cm_per_inch: ScaleFactor<Inch, Cm, f32> = mm_per_inch * cm_per_mm;
assert_eq!(cm_per_inch, ScaleFactor::new(2.54));
let a: ScaleFactor<Inch, Inch, isize> = ScaleFactor::new(2);
let b: ScaleFactor<Inch, Inch, isize> = ScaleFactor::new(3);
assert!(a != b);
assert_eq!(a, a.clone());
assert_eq!(a.clone() + b.clone(), ScaleFactor::new(5));
assert_eq!(a - b, ScaleFactor::new(-1));
}
}