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
#![allow(unused)]
use nalgebra::{Point3, Vector3};
use std::fmt::{Display, Formatter, Result};
use std::ops::{Index, IndexMut};
struct MyType<T>(T);
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
}
impl Display for Axis {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
Axis::X => "x",
Axis::Y => "y",
Axis::Z => "z",
}
)
}
}
impl Index<Axis> for [f32] {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
&self[axis as usize]
}
}
impl Index<Axis> for Point3<f32> {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
match axis {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
impl Index<Axis> for MyType<Vector3<f32>> {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
match axis {
Axis::X => &self.0.x,
Axis::Y => &self.0.y,
Axis::Z => &self.0.z,
}
}
}
impl IndexMut<Axis> for [f32] {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
&mut self[axis as usize]
}
}
impl IndexMut<Axis> for Point3<f32> {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.x,
Axis::Y => &mut self.y,
Axis::Z => &mut self.z,
}
}
}
impl IndexMut<Axis> for MyType<Vector3<f32>> {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.0.x,
Axis::Y => &mut self.0.y,
Axis::Z => &mut self.0.z,
}
}
}
#[cfg(test)]
mod test {
use axis::Axis;
quickcheck!{
fn test_index_by_axis(tpl: (f32, f32, f32)) -> bool {
let a = [tpl.0, tpl.1, tpl.2];
a[0] == a[Axis::X] && a[1] == a[Axis::Y] && a[2] == a[Axis::Z]
}
}
quickcheck!{
fn test_set_by_axis(tpl: (f32, f32, f32)) -> bool {
let mut a = [0.0, 0.0, 0.0];
a[Axis::X] = tpl.0;
a[Axis::Y] = tpl.1;
a[Axis::Z] = tpl.2;
a[0] == tpl.0 && a[1] == tpl.1 && a[2] == tpl.2
}
}
}