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
type UnsignedCoordPart = u32;
#[cfg(not(feature = "nalgebra_support"))]
mod internal_unsigned_coord {
use super::UnsignedCoordPart;
use core::ops::{Add, AddAssign, Index, Sub, SubAssign};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct UnsignedCoord(pub UnsignedCoordPart, pub UnsignedCoordPart);
impl UnsignedCoord {
pub fn new(x: UnsignedCoordPart, y: UnsignedCoordPart) -> Self {
UnsignedCoord(x, y)
}
}
impl Add for UnsignedCoord {
type Output = UnsignedCoord;
fn add(self, other: UnsignedCoord) -> UnsignedCoord {
UnsignedCoord::new(self.0 + other.0, self.1 + other.1)
}
}
impl AddAssign for UnsignedCoord {
fn add_assign(&mut self, other: UnsignedCoord) {
self.0 += other.0;
self.1 += other.1;
}
}
impl Sub for UnsignedCoord {
type Output = UnsignedCoord;
fn sub(self, other: UnsignedCoord) -> UnsignedCoord {
UnsignedCoord::new(self.0 - other.0, self.1 - other.1)
}
}
impl SubAssign for UnsignedCoord {
fn sub_assign(&mut self, other: UnsignedCoord) {
self.0 -= other.0;
self.1 -= other.1;
}
}
impl Index<usize> for UnsignedCoord {
type Output = UnsignedCoordPart;
fn index(&self, idx: usize) -> &UnsignedCoordPart {
match idx {
0 => &self.0,
1 => &self.1,
_ => panic!("Unreachable index {}", idx),
}
}
}
}
#[cfg(not(feature = "nalgebra_support"))]
pub use self::internal_unsigned_coord::UnsignedCoord;
#[cfg(feature = "nalgebra_support")]
use nalgebra;
#[cfg(feature = "nalgebra_support")]
pub type UnsignedCoord = nalgebra::Vector2<UnsignedCoordPart>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coords_can_be_added() {
let left = UnsignedCoord::new(10, 20);
let right = UnsignedCoord::new(30, 40);
assert_eq!(left + right, UnsignedCoord::new(40, 60));
}
#[test]
fn coords_can_be_subtracted() {
let left = UnsignedCoord::new(30, 40);
let right = UnsignedCoord::new(10, 20);
assert_eq!(left - right, UnsignedCoord::new(20, 20));
}
#[test]
#[cfg(feature = "nalgebra_support")]
fn nalgebra_support() {
let left = nalgebra::Vector2::new(30, 40);
let right = nalgebra::Vector2::new(10, 20);
assert_eq!(left - right, UnsignedCoord::new(20, 20));
}
}