Skip to content

Commit a6410f8

Browse files
committed
CoordinateSet: Better macro orthogonality
1 parent 76dd01c commit a6410f8

File tree

1 file changed

+49
-61
lines changed

1 file changed

+49
-61
lines changed

src/coordinate/set.rs

+49-61
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,31 @@ use super::*;
22

33
// Some helper macros, simplifying the macros for the actual data types
44

5+
// Produce the correct len() method for arrays, slices, and vecs
6+
macro_rules! length {
7+
(array) => {
8+
fn len(&self) -> usize {
9+
N
10+
}
11+
};
12+
13+
(slice) => {
14+
fn len(&self) -> usize {
15+
(**self).len()
16+
}
17+
};
18+
19+
(vec) => {
20+
fn len(&self) -> usize {
21+
self.len()
22+
}
23+
}
24+
}
25+
526
macro_rules! coordinate_set_impl_2d_subset {
6-
($dim:expr) => {
27+
($dim:expr, $len:ident) => {
28+
length!($len);
29+
730
fn dim(&self) -> usize {
831
$dim
932
}
@@ -19,8 +42,8 @@ macro_rules! coordinate_set_impl_2d_subset {
1942
}
2043

2144
macro_rules! coordinate_set_impl_3d_subset {
22-
($dim:expr) => {
23-
coordinate_set_impl_2d_subset!($dim);
45+
($dim:expr, $len:ident) => {
46+
coordinate_set_impl_2d_subset!($dim, $len);
2447

2548
fn xyz(&self, index: usize) -> (f64, f64, f64) {
2649
self[index].xyz()
@@ -31,6 +54,7 @@ macro_rules! coordinate_set_impl_3d_subset {
3154
};
3255
}
3356

57+
3458
// ----- CoordinateSet implementations for some Coor2D containers ------------
3559

3660
/// By default, the CoordinateSet implementations for Coor2D return `0` and `f64::NAN`
@@ -52,8 +76,8 @@ macro_rules! coordinate_set_impl_3d_subset {
5276
/// coordinate dimension.
5377
5478
macro_rules! coordinate_set_impl_for_coor2d {
55-
() => {
56-
coordinate_set_impl_2d_subset!(2);
79+
($kind:ident) => {
80+
coordinate_set_impl_2d_subset!(2, $kind);
5781

5882
fn get_coord(&self, index: usize) -> Coor4D {
5983
Coor4D([self[index][0], self[index][1], 0., f64::NAN])
@@ -66,31 +90,22 @@ macro_rules! coordinate_set_impl_for_coor2d {
6690
}
6791

6892
impl<const N: usize> CoordinateSet for [Coor2D; N] {
69-
fn len(&self) -> usize {
70-
N
71-
}
72-
coordinate_set_impl_for_coor2d!();
93+
coordinate_set_impl_for_coor2d!(array);
7394
}
7495

7596
impl CoordinateSet for &mut [Coor2D] {
76-
fn len(&self) -> usize {
77-
(**self).len()
78-
}
79-
coordinate_set_impl_for_coor2d!();
97+
coordinate_set_impl_for_coor2d!(slice);
8098
}
8199

82100
impl CoordinateSet for Vec<Coor2D> {
83-
fn len(&self) -> usize {
84-
self.len()
85-
}
86-
coordinate_set_impl_for_coor2d!();
101+
coordinate_set_impl_for_coor2d!(vec);
87102
}
88103

89104
// ----- CoordinateSet implementations for some Coor32 containers ------------
90105

91106
macro_rules! coordinate_set_impl_for_coor32 {
92-
() => {
93-
coordinate_set_impl_2d_subset!(2);
107+
($kind:ident) => {
108+
coordinate_set_impl_2d_subset!(2, $kind);
94109

95110
fn get_coord(&self, index: usize) -> Coor4D {
96111
Coor4D([self[index][0] as f64, self[index][1] as f64, 0., f64::NAN])
@@ -103,31 +118,22 @@ macro_rules! coordinate_set_impl_for_coor32 {
103118
}
104119

105120
impl<const N: usize> CoordinateSet for [Coor32; N] {
106-
fn len(&self) -> usize {
107-
N
108-
}
109-
coordinate_set_impl_for_coor32!();
121+
coordinate_set_impl_for_coor32!(array);
110122
}
111123

112124
impl CoordinateSet for &mut [Coor32] {
113-
fn len(&self) -> usize {
114-
(**self).len()
115-
}
116-
coordinate_set_impl_for_coor32!();
125+
coordinate_set_impl_for_coor32!(slice);
117126
}
118127

119128
impl CoordinateSet for Vec<Coor32> {
120-
fn len(&self) -> usize {
121-
self.len()
122-
}
123-
coordinate_set_impl_for_coor32!();
129+
coordinate_set_impl_for_coor32!(vec);
124130
}
125131

126132
// ----- CoordinateSet implementations for some Coor3D containers ------------
127133

128134
macro_rules! coordinate_set_impl_for_coor3d {
129-
() => {
130-
coordinate_set_impl_3d_subset!(3);
135+
($kind:ident) => {
136+
coordinate_set_impl_3d_subset!(3, $kind);
131137

132138
fn get_coord(&self, index: usize) -> Coor4D {
133139
Coor4D([self[index][0], self[index][1], self[index][2], f64::NAN])
@@ -140,31 +146,22 @@ macro_rules! coordinate_set_impl_for_coor3d {
140146
}
141147

142148
impl<const N: usize> CoordinateSet for [Coor3D; N] {
143-
fn len(&self) -> usize {
144-
N
145-
}
146-
coordinate_set_impl_for_coor3d!();
149+
coordinate_set_impl_for_coor3d!(array);
147150
}
148151

149152
impl CoordinateSet for &mut [Coor3D] {
150-
fn len(&self) -> usize {
151-
(**self).len()
152-
}
153-
coordinate_set_impl_for_coor3d!();
153+
coordinate_set_impl_for_coor3d!(slice);
154154
}
155155

156156
impl CoordinateSet for Vec<Coor3D> {
157-
fn len(&self) -> usize {
158-
self.len()
159-
}
160-
coordinate_set_impl_for_coor3d!();
157+
coordinate_set_impl_for_coor3d!(vec);
161158
}
162159

163160
// ----- CoordinateSet implementations for some Coor4D containers ------------
164161

165162
macro_rules! coordinate_set_impl_for_coor4d {
166-
() => {
167-
coordinate_set_impl_3d_subset!(4);
163+
($kind:ident) => {
164+
coordinate_set_impl_3d_subset!(4, $kind);
168165

169166
fn get_coord(&self, index: usize) -> Coor4D {
170167
self[index]
@@ -184,25 +181,16 @@ macro_rules! coordinate_set_impl_for_coor4d {
184181
};
185182
}
186183

187-
impl CoordinateSet for &mut [Coor4D] {
188-
fn len(&self) -> usize {
189-
(**self).len()
190-
}
191-
coordinate_set_impl_for_coor4d!();
184+
impl<const N: usize> CoordinateSet for [Coor4D; N] {
185+
coordinate_set_impl_for_coor4d!(array);
192186
}
193187

194-
impl<const N: usize> CoordinateSet for [Coor4D; N] {
195-
fn len(&self) -> usize {
196-
N
197-
}
198-
coordinate_set_impl_for_coor4d!();
188+
impl CoordinateSet for &mut [Coor4D] {
189+
coordinate_set_impl_for_coor4d!(slice);
199190
}
200191

201192
impl CoordinateSet for Vec<Coor4D> {
202-
fn len(&self) -> usize {
203-
self.len()
204-
}
205-
coordinate_set_impl_for_coor4d!();
193+
coordinate_set_impl_for_coor4d!(vec);
206194
}
207195

208196
/// User defined values for third and fourth coordinate dimension.

0 commit comments

Comments
 (0)