8
8
use std:: ops:: { Deref , Range , RangeFrom , RangeFull , RangeTo } ;
9
9
use std:: fmt;
10
10
use std:: marker:: PhantomData ;
11
- use super :: { Dimension , Ixs } ;
11
+ use super :: Dimension ;
12
12
13
13
/// A slice (range with step size).
14
14
///
@@ -45,44 +45,52 @@ impl Slice {
45
45
46
46
/// Returns a new `Slice` with the given step size.
47
47
#[ inline]
48
- pub fn step_by ( self , step : Ixs ) -> Self {
48
+ pub fn step_by ( self , step : isize ) -> Self {
49
49
Slice { step, ..self }
50
50
}
51
51
}
52
52
53
- impl From < Range < Ixs > > for Slice {
54
- #[ inline]
55
- fn from ( r : Range < Ixs > ) -> Slice {
56
- Slice {
57
- start : r. start ,
58
- end : Some ( r. end ) ,
59
- step : 1 ,
53
+ macro_rules! impl_slice_from_index_type {
54
+ ( $index: ty) => {
55
+ impl From <Range <$index>> for Slice {
56
+ #[ inline]
57
+ fn from( r: Range <$index>) -> Slice {
58
+ Slice {
59
+ start: r. start as isize ,
60
+ end: Some ( r. end as isize ) ,
61
+ step: 1 ,
62
+ }
63
+ }
60
64
}
61
- }
62
- }
63
65
64
- impl From < RangeFrom < Ixs > > for Slice {
65
- #[ inline]
66
- fn from ( r : RangeFrom < Ixs > ) -> Slice {
67
- Slice {
68
- start : r. start ,
69
- end : None ,
70
- step : 1 ,
66
+ impl From <RangeFrom <$index>> for Slice {
67
+ #[ inline]
68
+ fn from( r: RangeFrom <$index>) -> Slice {
69
+ Slice {
70
+ start: r. start as isize ,
71
+ end: None ,
72
+ step: 1 ,
73
+ }
74
+ }
71
75
}
72
- }
73
- }
74
76
75
- impl From < RangeTo < Ixs > > for Slice {
76
- #[ inline]
77
- fn from ( r : RangeTo < Ixs > ) -> Slice {
78
- Slice {
79
- start : 0 ,
80
- end : Some ( r. end ) ,
81
- step : 1 ,
77
+ impl From <RangeTo <$index>> for Slice {
78
+ #[ inline]
79
+ fn from( r: RangeTo <$index>) -> Slice {
80
+ Slice {
81
+ start: 0 ,
82
+ end: Some ( r. end as isize ) ,
83
+ step: 1 ,
84
+ }
85
+ }
82
86
}
83
87
}
84
88
}
85
89
90
+ impl_slice_from_index_type ! ( isize ) ;
91
+ impl_slice_from_index_type ! ( usize ) ;
92
+ impl_slice_from_index_type ! ( i32 ) ;
93
+
86
94
impl From < RangeFull > for Slice {
87
95
#[ inline]
88
96
fn from ( _: RangeFull ) -> Slice {
@@ -124,12 +132,12 @@ pub enum SliceOrIndex {
124
132
/// from the back of the axis. If `end` is `None`, the slice extends to the
125
133
/// end of the axis.
126
134
Slice {
127
- start : Ixs ,
128
- end : Option < Ixs > ,
129
- step : Ixs ,
135
+ start : isize ,
136
+ end : Option < isize > ,
137
+ step : isize ,
130
138
} ,
131
139
/// A single index.
132
- Index ( Ixs ) ,
140
+ Index ( isize ) ,
133
141
}
134
142
135
143
copy_and_clone ! { SliceOrIndex }
@@ -153,7 +161,7 @@ impl SliceOrIndex {
153
161
154
162
/// Returns a new `SliceOrIndex` with the given step size.
155
163
#[ inline]
156
- pub fn step_by ( self , step : Ixs ) -> Self {
164
+ pub fn step_by ( self , step : isize ) -> Self {
157
165
match self {
158
166
SliceOrIndex :: Slice { start, end, .. } => SliceOrIndex :: Slice { start, end, step } ,
159
167
SliceOrIndex :: Index ( s) => SliceOrIndex :: Index ( s) ,
@@ -193,46 +201,54 @@ impl From<Slice> for SliceOrIndex {
193
201
}
194
202
}
195
203
196
- impl From < Range < Ixs > > for SliceOrIndex {
197
- # [ inline ]
198
- fn from ( r : Range < Ixs > ) -> SliceOrIndex {
199
- SliceOrIndex :: Slice {
200
- start : r . start ,
201
- end : Some ( r . end ) ,
202
- step : 1 ,
204
+ macro_rules! impl_sliceorindex_from_index_type {
205
+ ( $index : ty ) => {
206
+ impl From <$index> for SliceOrIndex {
207
+ # [ inline ]
208
+ fn from ( r : $index ) -> SliceOrIndex {
209
+ SliceOrIndex :: Index ( r as isize )
210
+ }
203
211
}
204
- }
205
- }
206
212
207
- impl From < Ixs > for SliceOrIndex {
208
- #[ inline]
209
- fn from ( r : Ixs ) -> SliceOrIndex {
210
- SliceOrIndex :: Index ( r)
211
- }
212
- }
213
+ impl From <Range <$index>> for SliceOrIndex {
214
+ #[ inline]
215
+ fn from( r: Range <$index>) -> SliceOrIndex {
216
+ SliceOrIndex :: Slice {
217
+ start: r. start as isize ,
218
+ end: Some ( r. end as isize ) ,
219
+ step: 1 ,
220
+ }
221
+ }
222
+ }
213
223
214
- impl From < RangeFrom < Ixs > > for SliceOrIndex {
215
- #[ inline]
216
- fn from ( r : RangeFrom < Ixs > ) -> SliceOrIndex {
217
- SliceOrIndex :: Slice {
218
- start : r. start ,
219
- end : None ,
220
- step : 1 ,
224
+ impl From <RangeFrom <$index>> for SliceOrIndex {
225
+ #[ inline]
226
+ fn from( r: RangeFrom <$index>) -> SliceOrIndex {
227
+ SliceOrIndex :: Slice {
228
+ start: r. start as isize ,
229
+ end: None ,
230
+ step: 1 ,
231
+ }
232
+ }
221
233
}
222
- }
223
- }
224
234
225
- impl From < RangeTo < Ixs > > for SliceOrIndex {
226
- #[ inline]
227
- fn from ( r : RangeTo < Ixs > ) -> SliceOrIndex {
228
- SliceOrIndex :: Slice {
229
- start : 0 ,
230
- end : Some ( r. end ) ,
231
- step : 1 ,
235
+ impl From <RangeTo <$index>> for SliceOrIndex {
236
+ #[ inline]
237
+ fn from( r: RangeTo <$index>) -> SliceOrIndex {
238
+ SliceOrIndex :: Slice {
239
+ start: 0 ,
240
+ end: Some ( r. end as isize ) ,
241
+ step: 1 ,
242
+ }
243
+ }
232
244
}
233
245
}
234
246
}
235
247
248
+ impl_sliceorindex_from_index_type ! ( isize ) ;
249
+ impl_sliceorindex_from_index_type ! ( usize ) ;
250
+ impl_sliceorindex_from_index_type ! ( i32 ) ;
251
+
236
252
impl From < RangeFull > for SliceOrIndex {
237
253
#[ inline]
238
254
fn from ( _: RangeFull ) -> SliceOrIndex {
@@ -387,36 +403,44 @@ impl<D1: Dimension> SliceNextDim<D1, D1::Larger> for Slice {
387
403
}
388
404
}
389
405
390
- impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for Range < Ixs > {
391
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
392
- PhantomData
393
- }
394
- }
406
+ macro_rules! impl_slicenextdim_for_index_type {
407
+ ( $index: ty) => {
408
+ impl <D1 : Dimension > SliceNextDim <D1 , D1 > for $index {
409
+ fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 > {
410
+ PhantomData
411
+ }
412
+ }
395
413
396
- impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for RangeFrom < Ixs > {
397
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
398
- PhantomData
399
- }
400
- }
414
+ impl <D1 : Dimension > SliceNextDim <D1 , D1 :: Larger > for Range <$index > {
415
+ fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
416
+ PhantomData
417
+ }
418
+ }
401
419
402
- impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for RangeTo < Ixs > {
403
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
404
- PhantomData
420
+ impl <D1 : Dimension > SliceNextDim <D1 , D1 :: Larger > for RangeFrom <$index> {
421
+ fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
422
+ PhantomData
423
+ }
424
+ }
425
+
426
+ impl <D1 : Dimension > SliceNextDim <D1 , D1 :: Larger > for RangeTo <$index> {
427
+ fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
428
+ PhantomData
429
+ }
430
+ }
405
431
}
406
432
}
407
433
434
+ impl_slicenextdim_for_index_type ! ( isize ) ;
435
+ impl_slicenextdim_for_index_type ! ( usize ) ;
436
+ impl_slicenextdim_for_index_type ! ( i32 ) ;
437
+
408
438
impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for RangeFull {
409
439
fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
410
440
PhantomData
411
441
}
412
442
}
413
443
414
- impl < D1 : Dimension > SliceNextDim < D1 , D1 > for Ixs {
415
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 > {
416
- PhantomData
417
- }
418
- }
419
-
420
444
/// Slice argument constructor.
421
445
///
422
446
/// `s![]` takes a list of ranges/slices/indices, separated by comma, with
@@ -443,10 +467,10 @@ impl<D1: Dimension> SliceNextDim<D1, D1> for Ixs {
443
467
///
444
468
/// The number of *axis-slice-or-index* must match the number of axes in the
445
469
/// array. *index*, *range*, *slice*, and *step* can be expressions. *index*
446
- /// and *step* must be of type [`Ixs`] . *range* can be of type `Range<Ixs>`,
447
- /// `RangeTo<Ixs >`, `RangeFrom<Ixs >`, or `RangeFull`.
448
- ///
449
- /// [`Ixs`]: type.Ixs.html
470
+ /// must be of type `isize`, `usize`, or `i32` . *range* must be of type
471
+ /// `Range<I>`, ` RangeTo<I >`, `RangeFrom<I >`, or `RangeFull` where `I` is
472
+ /// `isize`, `usize`, or `i32`. *step* must be a type that can be converted to
473
+ /// `isize` with the `as` keyword.
450
474
///
451
475
/// For example `s![0..4;2, 6, 1..5]` is a slice of the first axis for 0..4
452
476
/// with step size 2, a subview of the second axis at index 6, and a slice of
@@ -539,7 +563,7 @@ macro_rules! s(
539
563
} ;
540
564
// convert range/index and step into SliceOrIndex
541
565
( @convert $r: expr, $s: expr) => {
542
- <$crate:: SliceOrIndex as :: std:: convert:: From <_>>:: from( $r) . step_by( $s)
566
+ <$crate:: SliceOrIndex as :: std:: convert:: From <_>>:: from( $r) . step_by( $s as isize )
543
567
} ;
544
568
( $( $t: tt) * ) => {
545
569
s![ @parse :: std:: marker:: PhantomData :: <$crate:: Ix0 >, [ ] $( $t) * ]
0 commit comments