@@ -2,8 +2,31 @@ use super::*;
2
2
3
3
// Some helper macros, simplifying the macros for the actual data types
4
4
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
+
5
26
macro_rules! coordinate_set_impl_2d_subset {
6
- ( $dim: expr) => {
27
+ ( $dim: expr, $len: ident) => {
28
+ length!( $len) ;
29
+
7
30
fn dim( & self ) -> usize {
8
31
$dim
9
32
}
@@ -19,8 +42,8 @@ macro_rules! coordinate_set_impl_2d_subset {
19
42
}
20
43
21
44
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 ) ;
24
47
25
48
fn xyz( & self , index: usize ) -> ( f64 , f64 , f64 ) {
26
49
self [ index] . xyz( )
@@ -31,6 +54,7 @@ macro_rules! coordinate_set_impl_3d_subset {
31
54
} ;
32
55
}
33
56
57
+
34
58
// ----- CoordinateSet implementations for some Coor2D containers ------------
35
59
36
60
/// By default, the CoordinateSet implementations for Coor2D return `0` and `f64::NAN`
@@ -52,8 +76,8 @@ macro_rules! coordinate_set_impl_3d_subset {
52
76
/// coordinate dimension.
53
77
54
78
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 ) ;
57
81
58
82
fn get_coord( & self , index: usize ) -> Coor4D {
59
83
Coor4D ( [ self [ index] [ 0 ] , self [ index] [ 1 ] , 0. , f64 :: NAN ] )
@@ -66,31 +90,22 @@ macro_rules! coordinate_set_impl_for_coor2d {
66
90
}
67
91
68
92
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) ;
73
94
}
74
95
75
96
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) ;
80
98
}
81
99
82
100
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) ;
87
102
}
88
103
89
104
// ----- CoordinateSet implementations for some Coor32 containers ------------
90
105
91
106
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 ) ;
94
109
95
110
fn get_coord( & self , index: usize ) -> Coor4D {
96
111
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 {
103
118
}
104
119
105
120
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) ;
110
122
}
111
123
112
124
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) ;
117
126
}
118
127
119
128
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) ;
124
130
}
125
131
126
132
// ----- CoordinateSet implementations for some Coor3D containers ------------
127
133
128
134
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 ) ;
131
137
132
138
fn get_coord( & self , index: usize ) -> Coor4D {
133
139
Coor4D ( [ self [ index] [ 0 ] , self [ index] [ 1 ] , self [ index] [ 2 ] , f64 :: NAN ] )
@@ -140,31 +146,22 @@ macro_rules! coordinate_set_impl_for_coor3d {
140
146
}
141
147
142
148
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) ;
147
150
}
148
151
149
152
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) ;
154
154
}
155
155
156
156
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) ;
161
158
}
162
159
163
160
// ----- CoordinateSet implementations for some Coor4D containers ------------
164
161
165
162
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 ) ;
168
165
169
166
fn get_coord( & self , index: usize ) -> Coor4D {
170
167
self [ index]
@@ -184,25 +181,16 @@ macro_rules! coordinate_set_impl_for_coor4d {
184
181
} ;
185
182
}
186
183
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) ;
192
186
}
193
187
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) ;
199
190
}
200
191
201
192
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) ;
206
194
}
207
195
208
196
/// User defined values for third and fourth coordinate dimension.
0 commit comments