@@ -105,229 +105,130 @@ pub trait CoordinateSet: CoordinateMetadata {
105
105
}
106
106
}
107
107
108
- // An experiment with an extended version of Kyle Barron's CoordTrait<DIMENSION, MEASURE> PR
108
+ //-----------------------------------------------------------------------
109
+ // An experiment with an extended version of Kyle Barron's CoordTrait PR
109
110
// over at https://github.com/georust/geo/pull/1157
110
-
111
- pub trait CoordNum { }
112
- impl CoordNum for f32 { }
113
- impl CoordNum for f64 { }
114
-
115
- /// A trait for accessing data from a generic Coord.
111
+ //-----------------------------------------------------------------------
112
+
113
+ // The next 8 lines are mostly copied from georust/geo/geo-types/src/lib.rs
114
+ // Although I added ToPrimitive in the first line
115
+ use core:: fmt:: Debug ;
116
+ use num_traits:: { Float , Num , NumCast , ToPrimitive } ;
117
+ pub trait CoordinateType : Num + Copy + NumCast + PartialOrd + Debug { }
118
+ impl < T : Num + Copy + NumCast + PartialOrd + Debug > CoordinateType for T { }
119
+ pub trait CoordNum : CoordinateType + Debug { }
120
+ impl < T : CoordinateType + Debug > CoordNum for T { }
121
+ pub trait CoordFloat : CoordNum + Float { }
122
+ impl < T : CoordNum + Float > CoordFloat for T { }
123
+
124
+ // And here is Kyle Barron's CoordTrait from https://github.com/georust/geo/pull/1157
125
+ // extended with z(), t(), m(), and associated consts DIMENSION and MEASURE
116
126
pub trait CoordTrait {
117
127
type T : CoordNum ;
118
128
const DIMENSION : usize ;
119
129
const MEASURE : bool ;
120
130
121
- // Required implementations
122
-
123
131
/// Accessors for the coordinate tuple components
124
132
fn x ( & self ) -> Self :: T ;
125
133
fn y ( & self ) -> Self :: T ;
126
134
fn z ( & self ) -> Self :: T ;
127
135
fn t ( & self ) -> Self :: T ;
128
136
fn m ( & self ) -> Self :: T ;
129
137
130
- /// Accessors for the coordinate tuple components converted to f64
131
- fn x_as_f64 ( & self ) -> f64 ;
132
- fn y_as_f64 ( & self ) -> f64 ;
133
- fn z_as_f64 ( & self ) -> f64 ;
134
- fn t_as_f64 ( & self ) -> f64 ;
135
- fn m_as_f64 ( & self ) -> f64 ;
136
-
137
- // Provided implementations
138
-
139
138
/// Returns a tuple that contains the two first components of the coord.
140
- fn xy ( & self ) -> ( Self :: T , Self :: T ) {
139
+ fn x_y ( & self ) -> ( Self :: T , Self :: T ) {
141
140
( self . x ( ) , self . y ( ) )
142
141
}
142
+ }
143
143
144
- /// Returns a tuple that contains the three first components of the coord.
145
- fn xyz ( & self ) -> ( Self :: T , Self :: T , Self :: T ) {
146
- ( self . x ( ) , self . y ( ) , self . z ( ) )
147
- }
144
+ // The CoordTuples trait is blanket-implemented for anything that
145
+ // CoordTrait is implemented for
146
+ impl < C : CoordTrait > CoordTuples for C { }
148
147
149
- /// Returns a tuple that contains the three first components of the coord.
150
- fn xyzt ( & self ) -> ( Self :: T , Self :: T , Self :: T , Self :: T ) {
151
- ( self . x ( ) , self . y ( ) , self . z ( ) , self . t ( ) )
152
- }
148
+ // And here the actual implementation, which takes any CoordTrait implementing
149
+ // data type, and lets us access the contents as geodesy-compatible f64 tuples
150
+ #[ rustfmt:: skip]
151
+ pub trait CoordTuples : CoordTrait {
152
+ /// Accessors for the coordinate tuple components converted to f64
153
+ fn x_as_f64 ( & self ) -> f64 { self . x ( ) . to_f64 ( ) . unwrap_or ( f64:: NAN ) }
154
+ fn y_as_f64 ( & self ) -> f64 { self . y ( ) . to_f64 ( ) . unwrap_or ( f64:: NAN ) }
155
+ fn z_as_f64 ( & self ) -> f64 { self . z ( ) . to_f64 ( ) . unwrap_or ( f64:: NAN ) }
156
+ fn t_as_f64 ( & self ) -> f64 { self . t ( ) . to_f64 ( ) . unwrap_or ( f64:: NAN ) }
157
+ fn m_as_f64 ( & self ) -> f64 { self . m ( ) . to_f64 ( ) . unwrap_or ( f64:: NAN ) }
153
158
154
- /// Returns a tuple that contains the two first components of the coord converted to f64.
155
159
fn xy_as_f64 ( & self ) -> ( f64 , f64 ) {
156
160
( self . x_as_f64 ( ) , self . y_as_f64 ( ) )
157
161
}
158
162
159
- /// Returns a tuple that contains the three first components of the coord converted to f64.
160
163
fn xyz_as_f64 ( & self ) -> ( f64 , f64 , f64 ) {
161
164
( self . x_as_f64 ( ) , self . y_as_f64 ( ) , self . z_as_f64 ( ) )
162
165
}
163
166
164
- /// Returns a tuple that contains the three first components of the coord converted to f64.
165
167
fn xyzt_as_f64 ( & self ) -> ( f64 , f64 , f64 , f64 ) {
166
- (
167
- self . x_as_f64 ( ) ,
168
- self . y_as_f64 ( ) ,
169
- self . z_as_f64 ( ) ,
170
- self . t_as_f64 ( ) ,
171
- )
168
+ ( self . x_as_f64 ( ) , self . y_as_f64 ( ) , self . z_as_f64 ( ) , self . t_as_f64 ( ) )
172
169
}
173
170
}
174
171
172
+ // We must still implement the foundational CoordTrait trait for
173
+ // the Geodesy data types Coor2D, Coor32, Coor3D, Coor4D
174
+
175
+ #[ rustfmt:: skip]
175
176
impl CoordTrait for Coor2D {
176
177
type T = f64 ;
177
178
const DIMENSION : usize = 2 ;
178
179
const MEASURE : bool = false ;
179
-
180
- /// Accessors for the coordinate tuple components
181
- fn x ( & self ) -> Self :: T {
182
- self . 0 [ 0 ]
183
- }
184
- fn y ( & self ) -> Self :: T {
185
- self . 0 [ 1 ]
186
- }
187
- fn z ( & self ) -> Self :: T {
188
- f64:: NAN
189
- }
190
- fn t ( & self ) -> Self :: T {
191
- f64:: NAN
192
- }
193
- fn m ( & self ) -> Self :: T {
194
- f64:: NAN
195
- }
196
-
197
- /// Accessors for the coordinate tuple components converted to f64
198
- fn x_as_f64 ( & self ) -> f64 {
199
- self . 0 [ 0 ]
200
- }
201
- fn y_as_f64 ( & self ) -> f64 {
202
- self . 0 [ 1 ]
203
- }
204
- fn z_as_f64 ( & self ) -> f64 {
205
- f64:: NAN
206
- }
207
- fn t_as_f64 ( & self ) -> f64 {
208
- f64:: NAN
209
- }
210
- fn m_as_f64 ( & self ) -> f64 {
211
- f64:: NAN
212
- }
180
+ fn x ( & self ) -> Self :: T { self . 0 [ 0 ] }
181
+ fn y ( & self ) -> Self :: T { self . 0 [ 1 ] }
182
+ fn z ( & self ) -> Self :: T { f64:: NAN }
183
+ fn t ( & self ) -> Self :: T { f64:: NAN }
184
+ fn m ( & self ) -> Self :: T { f64:: NAN }
213
185
}
214
186
187
+ #[ rustfmt:: skip]
215
188
impl CoordTrait for Coor32 {
216
189
type T = f32 ;
217
190
const DIMENSION : usize = 2 ;
218
191
const MEASURE : bool = false ;
219
-
220
- /// Accessors for the coordinate tuple components
221
- fn x ( & self ) -> Self :: T {
222
- self . 0 [ 0 ]
223
- }
224
- fn y ( & self ) -> Self :: T {
225
- self . 0 [ 1 ]
226
- }
227
- fn z ( & self ) -> Self :: T {
228
- f32:: NAN
229
- }
230
- fn t ( & self ) -> Self :: T {
231
- f32:: NAN
232
- }
233
- fn m ( & self ) -> Self :: T {
234
- f32:: NAN
235
- }
236
-
237
- /// Accessors for the coordinate tuple components converted to f64
238
- fn x_as_f64 ( & self ) -> f64 {
239
- self . 0 [ 0 ] as f64
240
- }
241
- fn y_as_f64 ( & self ) -> f64 {
242
- self . 0 [ 1 ] as f64
243
- }
244
- fn z_as_f64 ( & self ) -> f64 {
245
- f64:: NAN
246
- }
247
- fn t_as_f64 ( & self ) -> f64 {
248
- f64:: NAN
249
- }
250
- fn m_as_f64 ( & self ) -> f64 {
251
- f64:: NAN
252
- }
192
+ fn x ( & self ) -> Self :: T { self . 0 [ 0 ] }
193
+ fn y ( & self ) -> Self :: T { self . 0 [ 1 ] }
194
+ fn z ( & self ) -> Self :: T { f32:: NAN }
195
+ fn t ( & self ) -> Self :: T { f32:: NAN }
196
+ fn m ( & self ) -> Self :: T { f32:: NAN }
253
197
}
254
198
199
+ #[ rustfmt:: skip]
255
200
impl CoordTrait for Coor3D {
256
201
type T = f64 ;
257
202
const DIMENSION : usize = 3 ;
258
203
const MEASURE : bool = false ;
259
-
260
- /// Accessors for the coordinate tuple components
261
- fn x ( & self ) -> Self :: T {
262
- self . 0 [ 0 ]
263
- }
264
- fn y ( & self ) -> Self :: T {
265
- self . 0 [ 1 ]
266
- }
267
- fn z ( & self ) -> Self :: T {
268
- self . 0 [ 2 ]
269
- }
270
- fn t ( & self ) -> Self :: T {
271
- f64:: NAN
272
- }
273
- fn m ( & self ) -> Self :: T {
274
- f64:: NAN
275
- }
276
-
277
- /// Accessors for the coordinate tuple components converted to f64
278
- fn x_as_f64 ( & self ) -> f64 {
279
- self . 0 [ 0 ]
280
- }
281
- fn y_as_f64 ( & self ) -> f64 {
282
- self . 0 [ 1 ]
283
- }
284
- fn z_as_f64 ( & self ) -> f64 {
285
- self . 0 [ 2 ]
286
- }
287
- fn t_as_f64 ( & self ) -> f64 {
288
- f64:: NAN
289
- }
290
- fn m_as_f64 ( & self ) -> f64 {
291
- f64:: NAN
292
- }
204
+ fn x ( & self ) -> Self :: T { self . 0 [ 0 ] }
205
+ fn y ( & self ) -> Self :: T { self . 0 [ 1 ] }
206
+ fn z ( & self ) -> Self :: T { self . 0 [ 2 ] }
207
+ fn t ( & self ) -> Self :: T { f64:: NAN }
208
+ fn m ( & self ) -> Self :: T { f64:: NAN }
293
209
}
294
210
211
+ #[ rustfmt:: skip]
295
212
impl CoordTrait for Coor4D {
296
213
type T = f64 ;
297
214
const DIMENSION : usize = 4 ;
298
215
const MEASURE : bool = false ;
216
+ fn x ( & self ) -> Self :: T { self . 0 [ 0 ] }
217
+ fn y ( & self ) -> Self :: T { self . 0 [ 1 ] }
218
+ fn z ( & self ) -> Self :: T { self . 0 [ 2 ] }
219
+ fn t ( & self ) -> Self :: T { self . 0 [ 3 ] }
220
+ fn m ( & self ) -> Self :: T { f64:: NAN }
221
+ }
299
222
300
- /// Accessors for the coordinate tuple components
301
- fn x ( & self ) -> Self :: T {
302
- self . 0 [ 0 ]
303
- }
304
- fn y ( & self ) -> Self :: T {
305
- self . 0 [ 1 ]
306
- }
307
- fn z ( & self ) -> Self :: T {
308
- self . 0 [ 2 ]
309
- }
310
- fn t ( & self ) -> Self :: T {
311
- self . 0 [ 3 ]
312
- }
313
- fn m ( & self ) -> Self :: T {
314
- f64:: NAN
315
- }
316
-
317
- /// Accessors for the coordinate tuple components converted to f64
318
- fn x_as_f64 ( & self ) -> f64 {
319
- self . 0 [ 0 ]
320
- }
321
- fn y_as_f64 ( & self ) -> f64 {
322
- self . 0 [ 1 ]
323
- }
324
- fn z_as_f64 ( & self ) -> f64 {
325
- self . 0 [ 2 ]
326
- }
327
- fn t_as_f64 ( & self ) -> f64 {
328
- self . 0 [ 3 ]
329
- }
330
- fn m_as_f64 ( & self ) -> f64 {
331
- f64:: NAN
332
- }
223
+ // And let's also implement it for a plain 2D f64 tuple
224
+ #[ rustfmt:: skip]
225
+ impl CoordTrait for ( f64 , f64 ) {
226
+ type T = f64 ;
227
+ const DIMENSION : usize = 2 ;
228
+ const MEASURE : bool = false ;
229
+ fn x ( & self ) -> Self :: T { self . 0 }
230
+ fn y ( & self ) -> Self :: T { self . 1 }
231
+ fn z ( & self ) -> Self :: T { f64:: NAN }
232
+ fn t ( & self ) -> Self :: T { f64:: NAN }
233
+ fn m ( & self ) -> Self :: T { f64:: NAN }
333
234
}
0 commit comments