@@ -67,82 +67,6 @@ impl Slice {
67
67
}
68
68
}
69
69
70
- macro_rules! impl_slice_from_index_type {
71
- ( $index: ty) => {
72
- impl From <Range <$index>> for Slice {
73
- #[ inline]
74
- fn from( r: Range <$index>) -> Slice {
75
- Slice {
76
- start: r. start as isize ,
77
- end: Some ( r. end as isize ) ,
78
- step: 1 ,
79
- }
80
- }
81
- }
82
-
83
- impl From <RangeInclusive <$index>> for Slice {
84
- #[ inline]
85
- fn from( r: RangeInclusive <$index>) -> Slice {
86
- let end = * r. end( ) as isize ;
87
- Slice {
88
- start: * r. start( ) as isize ,
89
- end: if end == -1 { None } else { Some ( end + 1 ) } ,
90
- step: 1 ,
91
- }
92
- }
93
- }
94
-
95
- impl From <RangeFrom <$index>> for Slice {
96
- #[ inline]
97
- fn from( r: RangeFrom <$index>) -> Slice {
98
- Slice {
99
- start: r. start as isize ,
100
- end: None ,
101
- step: 1 ,
102
- }
103
- }
104
- }
105
-
106
- impl From <RangeTo <$index>> for Slice {
107
- #[ inline]
108
- fn from( r: RangeTo <$index>) -> Slice {
109
- Slice {
110
- start: 0 ,
111
- end: Some ( r. end as isize ) ,
112
- step: 1 ,
113
- }
114
- }
115
- }
116
-
117
- impl From <RangeToInclusive <$index>> for Slice {
118
- #[ inline]
119
- fn from( r: RangeToInclusive <$index>) -> Slice {
120
- let end = r. end as isize ;
121
- Slice {
122
- start: 0 ,
123
- end: if end == -1 { None } else { Some ( end + 1 ) } ,
124
- step: 1 ,
125
- }
126
- }
127
- }
128
- } ;
129
- }
130
-
131
- impl_slice_from_index_type ! ( isize ) ;
132
- impl_slice_from_index_type ! ( usize ) ;
133
- impl_slice_from_index_type ! ( i32 ) ;
134
-
135
- impl From < RangeFull > for Slice {
136
- #[ inline]
137
- fn from ( _: RangeFull ) -> Slice {
138
- Slice {
139
- start : 0 ,
140
- end : None ,
141
- step : 1 ,
142
- }
143
- }
144
- }
145
-
146
70
/// A slice (range with step) or an index.
147
71
///
148
72
/// See also the [`s![]`](macro.s!.html) macro for a convenient way to create a
@@ -244,76 +168,58 @@ impl fmt::Display for SliceOrIndex {
244
168
}
245
169
}
246
170
247
- impl From < Slice > for SliceOrIndex {
248
- #[ inline]
249
- fn from ( s : Slice ) -> SliceOrIndex {
250
- SliceOrIndex :: Slice {
251
- start : s. start ,
252
- end : s. end ,
253
- step : s. step ,
254
- }
255
- }
256
- }
257
-
258
- macro_rules! impl_sliceorindex_from_index_type {
259
- ( $index: ty) => {
260
- impl From <$index> for SliceOrIndex {
171
+ macro_rules! impl_slice_variant_from_range {
172
+ ( $self: ty, $constructor: path, $index: ty) => {
173
+ impl From <Range <$index>> for $self {
261
174
#[ inline]
262
- fn from( r: $index) -> SliceOrIndex {
263
- SliceOrIndex :: Index ( r as isize )
264
- }
265
- }
266
-
267
- impl From <Range <$index>> for SliceOrIndex {
268
- #[ inline]
269
- fn from( r: Range <$index>) -> SliceOrIndex {
270
- SliceOrIndex :: Slice {
175
+ fn from( r: Range <$index>) -> $self {
176
+ $constructor {
271
177
start: r. start as isize ,
272
178
end: Some ( r. end as isize ) ,
273
179
step: 1 ,
274
180
}
275
181
}
276
182
}
277
183
278
- impl From <RangeInclusive <$index>> for SliceOrIndex {
184
+ impl From <RangeInclusive <$index>> for $self {
279
185
#[ inline]
280
- fn from( r: RangeInclusive <$index>) -> SliceOrIndex {
186
+ fn from( r: RangeInclusive <$index>) -> $self {
281
187
let end = * r. end( ) as isize ;
282
- SliceOrIndex :: Slice {
188
+ $constructor {
283
189
start: * r. start( ) as isize ,
284
190
end: if end == -1 { None } else { Some ( end + 1 ) } ,
285
191
step: 1 ,
286
192
}
287
193
}
288
194
}
289
195
290
- impl From <RangeFrom <$index>> for SliceOrIndex {
196
+ impl From <RangeFrom <$index>> for $self {
291
197
#[ inline]
292
- fn from( r: RangeFrom <$index>) -> SliceOrIndex {
293
- SliceOrIndex :: Slice {
198
+ fn from( r: RangeFrom <$index>) -> $self {
199
+ $constructor {
294
200
start: r. start as isize ,
295
201
end: None ,
296
202
step: 1 ,
297
203
}
298
204
}
299
205
}
300
206
301
- impl From <RangeTo <$index>> for SliceOrIndex {
207
+ impl From <RangeTo <$index>> for $self {
302
208
#[ inline]
303
- fn from( r: RangeTo <$index>) -> SliceOrIndex {
304
- SliceOrIndex :: Slice {
209
+ fn from( r: RangeTo <$index>) -> $self {
210
+ $constructor {
305
211
start: 0 ,
306
212
end: Some ( r. end as isize ) ,
307
213
step: 1 ,
308
214
}
309
215
}
310
216
}
311
217
312
- impl From <RangeToInclusive <$index>> for SliceOrIndex {
218
+ impl From <RangeToInclusive <$index>> for $self {
313
219
#[ inline]
314
- fn from( r: RangeToInclusive <$index>) -> SliceOrIndex {
220
+ fn from( r: RangeToInclusive <$index>) -> $self {
315
221
let end = r. end as isize ;
316
- SliceOrIndex :: Slice {
222
+ $constructor {
317
223
start: 0 ,
318
224
end: if end == -1 { None } else { Some ( end + 1 ) } ,
319
225
step: 1 ,
@@ -322,10 +228,23 @@ macro_rules! impl_sliceorindex_from_index_type {
322
228
}
323
229
} ;
324
230
}
231
+ impl_slice_variant_from_range ! ( Slice , Slice , isize ) ;
232
+ impl_slice_variant_from_range ! ( Slice , Slice , usize ) ;
233
+ impl_slice_variant_from_range ! ( Slice , Slice , i32 ) ;
234
+ impl_slice_variant_from_range ! ( SliceOrIndex , SliceOrIndex :: Slice , isize ) ;
235
+ impl_slice_variant_from_range ! ( SliceOrIndex , SliceOrIndex :: Slice , usize ) ;
236
+ impl_slice_variant_from_range ! ( SliceOrIndex , SliceOrIndex :: Slice , i32 ) ;
325
237
326
- impl_sliceorindex_from_index_type ! ( isize ) ;
327
- impl_sliceorindex_from_index_type ! ( usize ) ;
328
- impl_sliceorindex_from_index_type ! ( i32 ) ;
238
+ impl From < RangeFull > for Slice {
239
+ #[ inline]
240
+ fn from ( _: RangeFull ) -> Slice {
241
+ Slice {
242
+ start : 0 ,
243
+ end : None ,
244
+ step : 1 ,
245
+ }
246
+ }
247
+ }
329
248
330
249
impl From < RangeFull > for SliceOrIndex {
331
250
#[ inline]
@@ -338,6 +257,31 @@ impl From<RangeFull> for SliceOrIndex {
338
257
}
339
258
}
340
259
260
+ impl From < Slice > for SliceOrIndex {
261
+ #[ inline]
262
+ fn from ( s : Slice ) -> SliceOrIndex {
263
+ SliceOrIndex :: Slice {
264
+ start : s. start ,
265
+ end : s. end ,
266
+ step : s. step ,
267
+ }
268
+ }
269
+ }
270
+
271
+ macro_rules! impl_sliceorindex_from_index {
272
+ ( $index: ty) => {
273
+ impl From <$index> for SliceOrIndex {
274
+ #[ inline]
275
+ fn from( r: $index) -> SliceOrIndex {
276
+ SliceOrIndex :: Index ( r as isize )
277
+ }
278
+ }
279
+ } ;
280
+ }
281
+ impl_sliceorindex_from_index ! ( isize ) ;
282
+ impl_sliceorindex_from_index ! ( usize ) ;
283
+ impl_sliceorindex_from_index ! ( i32 ) ;
284
+
341
285
/// Represents all of the necessary information to perform a slice.
342
286
///
343
287
/// The type `T` is typically `[SliceOrIndex; n]`, `[SliceOrIndex]`, or
@@ -476,61 +420,35 @@ pub trait SliceNextDim<D1, D2> {
476
420
fn next_dim ( & self , PhantomData < D1 > ) -> PhantomData < D2 > ;
477
421
}
478
422
479
- impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for Slice {
480
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
481
- PhantomData
482
- }
483
- }
484
-
485
- macro_rules! impl_slicenextdim_for_index_type {
486
- ( $index: ty) => {
487
- impl <D1 : Dimension > SliceNextDim <D1 , D1 > for $index {
423
+ macro_rules! impl_slicenextdim_equal {
424
+ ( $self: ty) => {
425
+ impl <D1 : Dimension > SliceNextDim <D1 , D1 > for $self {
488
426
fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 > {
489
427
PhantomData
490
428
}
491
429
}
492
430
}
493
431
}
494
-
495
- impl_slicenextdim_for_index_type ! ( isize ) ;
496
- impl_slicenextdim_for_index_type ! ( usize ) ;
497
- impl_slicenextdim_for_index_type ! ( i32 ) ;
498
-
499
- impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for Range < T > {
500
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
501
- PhantomData
502
- }
503
- }
504
-
505
- impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for RangeInclusive < T > {
506
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
507
- PhantomData
508
- }
509
- }
510
-
511
- impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for RangeFrom < T > {
512
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
513
- PhantomData
514
- }
515
- }
516
-
517
- impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for RangeTo < T > {
518
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
519
- PhantomData
520
- }
521
- }
522
-
523
- impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for RangeToInclusive < T > {
524
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
525
- PhantomData
526
- }
527
- }
528
-
529
- impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for RangeFull {
530
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
531
- PhantomData
432
+ impl_slicenextdim_equal ! ( isize ) ;
433
+ impl_slicenextdim_equal ! ( usize ) ;
434
+ impl_slicenextdim_equal ! ( i32 ) ;
435
+
436
+ macro_rules! impl_slicenextdim_larger {
437
+ ( ( $( $generics: tt) * ) , $self: ty) => {
438
+ impl <D1 : Dimension , $( $generics) ,* > SliceNextDim <D1 , D1 :: Larger > for $self {
439
+ fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
440
+ PhantomData
441
+ }
442
+ }
532
443
}
533
444
}
445
+ impl_slicenextdim_larger ! ( ( T ) , Range <T >) ;
446
+ impl_slicenextdim_larger ! ( ( T ) , RangeInclusive <T >) ;
447
+ impl_slicenextdim_larger ! ( ( T ) , RangeFrom <T >) ;
448
+ impl_slicenextdim_larger ! ( ( T ) , RangeTo <T >) ;
449
+ impl_slicenextdim_larger ! ( ( T ) , RangeToInclusive <T >) ;
450
+ impl_slicenextdim_larger ! ( ( ) , RangeFull ) ;
451
+ impl_slicenextdim_larger ! ( ( ) , Slice ) ;
534
452
535
453
/// Slice argument constructor.
536
454
///
0 commit comments