@@ -12,54 +12,88 @@ use super::{Dimension, Ixs};
12
12
13
13
/// A slice (range with step size).
14
14
///
15
+ /// Negative `begin` or `end` indexes are counted from the back of the axis. If
16
+ /// `end` is `None`, the slice extends to the end of the axis.
17
+ ///
15
18
/// ## Examples
16
19
///
17
- /// `Slice(0, None, 1)` is the full range of an axis. It can also be created
18
- /// with `Slice::from(..)`. The Python equivalent is `[:]`.
20
+ /// `Slice::new (0, None, 1)` is the full range of an axis. It can also be
21
+ /// created with `Slice::from(..)`. The Python equivalent is `[:]`.
19
22
///
20
- /// `Slice(a, Some(b) , 2)` is every second element from `a` until `b`. It can
21
- /// also be created with `Slice::from(a..b).step (2)`. The Python equivalent is
22
- /// `[a:b:2]`.
23
+ /// `Slice::new (a, b , 2)` is every second element from `a` until `b`. It can
24
+ /// also be created with `Slice::from(a..b).step_by (2)`. The Python equivalent
25
+ /// is `[a:b:2]`.
23
26
///
24
- /// `Slice(a, None, -1)` is every element, from `a` until the end, in reverse
25
- /// order. It can also be created with `Slice::from(a..).step (-1)`. The Python
26
- /// equivalent is `[a::-1]`.
27
+ /// `Slice::new (a, None, -1)` is every element, from `a` until the end, in
28
+ /// reverse order. It can also be created with `Slice::from(a..).step_by (-1)`.
29
+ /// The Python equivalent is `[a::-1]`.
27
30
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
28
- pub struct Slice ( pub Ixs , pub Option < Ixs > , pub Ixs ) ;
31
+ pub struct Slice {
32
+ pub start : Ixs ,
33
+ pub end : Option < Ixs > ,
34
+ pub step : Ixs ,
35
+ }
29
36
30
37
impl Slice {
38
+ pub fn new < I > ( start : Ixs , end : I , step : Ixs ) -> Slice
39
+ where
40
+ I : Into < Option < Ixs > > ,
41
+ {
42
+ Slice {
43
+ start,
44
+ end : end. into ( ) ,
45
+ step,
46
+ }
47
+ }
48
+
31
49
/// Returns a new `Slice` with the given step size.
32
50
#[ inline]
33
- pub fn step ( self , step : Ixs ) -> Self {
34
- Slice ( self . 0 , self . 1 , step )
51
+ pub fn step_by ( self , step : Ixs ) -> Self {
52
+ Slice { step , .. self }
35
53
}
36
54
}
37
55
38
56
impl From < Range < Ixs > > for Slice {
39
57
#[ inline]
40
58
fn from ( r : Range < Ixs > ) -> Slice {
41
- Slice ( r. start , Some ( r. end ) , 1 )
59
+ Slice {
60
+ start : r. start ,
61
+ end : Some ( r. end ) ,
62
+ step : 1 ,
63
+ }
42
64
}
43
65
}
44
66
45
67
impl From < RangeFrom < Ixs > > for Slice {
46
68
#[ inline]
47
69
fn from ( r : RangeFrom < Ixs > ) -> Slice {
48
- Slice ( r. start , None , 1 )
70
+ Slice {
71
+ start : r. start ,
72
+ end : None ,
73
+ step : 1 ,
74
+ }
49
75
}
50
76
}
51
77
52
78
impl From < RangeTo < Ixs > > for Slice {
53
79
#[ inline]
54
80
fn from ( r : RangeTo < Ixs > ) -> Slice {
55
- Slice ( 0 , Some ( r. end ) , 1 )
81
+ Slice {
82
+ start : 0 ,
83
+ end : Some ( r. end ) ,
84
+ step : 1 ,
85
+ }
56
86
}
57
87
}
58
88
59
89
impl From < RangeFull > for Slice {
60
90
#[ inline]
61
91
fn from ( _: RangeFull ) -> Slice {
62
- Slice ( 0 , None , 1 )
92
+ Slice {
93
+ start : 0 ,
94
+ end : None ,
95
+ step : 1 ,
96
+ }
63
97
}
64
98
}
65
99
@@ -74,24 +108,29 @@ impl From<RangeFull> for Slice {
74
108
/// `SliceOrIndex::from(a)`. The Python equivalent is `[a]`. The macro
75
109
/// equivalent is `s![a]`.
76
110
///
77
- /// `SliceOrIndex::Slice( 0, None, 1) ` is the full range of an axis. It can also
78
- /// be created with `SliceOrIndex::from(..)`. The Python equivalent is `[:]`.
79
- /// The macro equivalent is `s![..]`.
111
+ /// `SliceOrIndex::Slice { start: 0, end: None, step: 1} ` is the full range of
112
+ /// an axis. It can also be created with `SliceOrIndex::from(..)`. The Python
113
+ /// equivalent is `[:]`. The macro equivalent is `s![..]`.
80
114
///
81
- /// `SliceOrIndex::Slice(a, Some(b), 2)` is every second element from `a` until
82
- /// `b`. It can also be created with `SliceOrIndex::from(a..b).step(2)`. The
83
- /// Python equivalent is `[a:b:2]`. The macro equivalent is `s![a..b;2]`.
115
+ /// `SliceOrIndex::Slice { start: a, end: Some(b), step: 2 }` is every second
116
+ /// element from `a` until `b`. It can also be created with
117
+ /// `SliceOrIndex::from(a..b).step_by(2)`. The Python equivalent is `[a:b:2]`.
118
+ /// The macro equivalent is `s![a..b;2]`.
84
119
///
85
- /// `SliceOrIndex::Slice( a, None, -1) ` is every element, from `a` until the
86
- /// end, in reverse order. It can also be created with
87
- /// `SliceOrIndex::from(a..).step (-1)`. The Python equivalent is `[a::-1]`. The
88
- /// macro equivalent is `s![a..;-1]`.
120
+ /// `SliceOrIndex::Slice { start: a, end: None, step: -1 } ` is every element,
121
+ /// from `a` until the end, in reverse order. It can also be created with
122
+ /// `SliceOrIndex::from(a..).step_by (-1)`. The Python equivalent is `[a::-1]`.
123
+ /// The macro equivalent is `s![a..;-1]`.
89
124
#[ derive( Debug , PartialEq , Eq , Hash ) ]
90
125
pub enum SliceOrIndex {
91
- /// A range with step size. The fields are `begin`, `end`, and `step`,
92
- /// where negative `begin` or `end` indexes are counted from the back of
93
- /// the axis. If `end` is `None`, the slice extends to the end of the axis.
94
- Slice ( Ixs , Option < Ixs > , Ixs ) ,
126
+ /// A range with step size. Negative `begin` or `end` indexes are counted
127
+ /// from the back of the axis. If `end` is `None`, the slice extends to the
128
+ /// end of the axis.
129
+ Slice {
130
+ start : Ixs ,
131
+ end : Option < Ixs > ,
132
+ step : Ixs ,
133
+ } ,
95
134
/// A single index.
96
135
Index ( Ixs ) ,
97
136
}
@@ -102,7 +141,7 @@ impl SliceOrIndex {
102
141
/// Returns `true` if `self` is a `Slice` value.
103
142
pub fn is_slice ( & self ) -> bool {
104
143
match self {
105
- & SliceOrIndex :: Slice ( .. ) => true ,
144
+ & SliceOrIndex :: Slice { .. } => true ,
106
145
_ => false ,
107
146
}
108
147
}
@@ -117,9 +156,9 @@ impl SliceOrIndex {
117
156
118
157
/// Returns a new `SliceOrIndex` with the given step size.
119
158
#[ inline]
120
- pub fn step ( self , step : Ixs ) -> Self {
159
+ pub fn step_by ( self , step : Ixs ) -> Self {
121
160
match self {
122
- SliceOrIndex :: Slice ( start, end, _ ) => SliceOrIndex :: Slice ( start, end, step) ,
161
+ SliceOrIndex :: Slice { start, end, .. } => SliceOrIndex :: Slice { start, end, step } ,
123
162
SliceOrIndex :: Index ( s) => SliceOrIndex :: Index ( s) ,
124
163
}
125
164
}
@@ -129,7 +168,7 @@ impl fmt::Display for SliceOrIndex {
129
168
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
130
169
match * self {
131
170
SliceOrIndex :: Index ( index) => write ! ( f, "{}" , index) ?,
132
- SliceOrIndex :: Slice ( start, end, step) => {
171
+ SliceOrIndex :: Slice { start, end, step } => {
133
172
if start != 0 {
134
173
write ! ( f, "{}" , start) ?;
135
174
}
@@ -149,14 +188,22 @@ impl fmt::Display for SliceOrIndex {
149
188
impl From < Slice > for SliceOrIndex {
150
189
#[ inline]
151
190
fn from ( s : Slice ) -> SliceOrIndex {
152
- SliceOrIndex :: Slice ( s. 0 , s. 1 , s. 2 )
191
+ SliceOrIndex :: Slice {
192
+ start : s. start ,
193
+ end : s. end ,
194
+ step : s. step ,
195
+ }
153
196
}
154
197
}
155
198
156
199
impl From < Range < Ixs > > for SliceOrIndex {
157
200
#[ inline]
158
201
fn from ( r : Range < Ixs > ) -> SliceOrIndex {
159
- SliceOrIndex :: Slice ( r. start , Some ( r. end ) , 1 )
202
+ SliceOrIndex :: Slice {
203
+ start : r. start ,
204
+ end : Some ( r. end ) ,
205
+ step : 1 ,
206
+ }
160
207
}
161
208
}
162
209
@@ -170,21 +217,33 @@ impl From<Ixs> for SliceOrIndex {
170
217
impl From < RangeFrom < Ixs > > for SliceOrIndex {
171
218
#[ inline]
172
219
fn from ( r : RangeFrom < Ixs > ) -> SliceOrIndex {
173
- SliceOrIndex :: Slice ( r. start , None , 1 )
220
+ SliceOrIndex :: Slice {
221
+ start : r. start ,
222
+ end : None ,
223
+ step : 1 ,
224
+ }
174
225
}
175
226
}
176
227
177
228
impl From < RangeTo < Ixs > > for SliceOrIndex {
178
229
#[ inline]
179
230
fn from ( r : RangeTo < Ixs > ) -> SliceOrIndex {
180
- SliceOrIndex :: Slice ( 0 , Some ( r. end ) , 1 )
231
+ SliceOrIndex :: Slice {
232
+ start : 0 ,
233
+ end : Some ( r. end ) ,
234
+ step : 1 ,
235
+ }
181
236
}
182
237
}
183
238
184
239
impl From < RangeFull > for SliceOrIndex {
185
240
#[ inline]
186
241
fn from ( _: RangeFull ) -> SliceOrIndex {
187
- SliceOrIndex :: Slice ( 0 , None , 1 )
242
+ SliceOrIndex :: Slice {
243
+ start : 0 ,
244
+ end : None ,
245
+ step : 1 ,
246
+ }
188
247
}
189
248
}
190
249
@@ -483,7 +542,7 @@ macro_rules! s(
483
542
} ;
484
543
// convert range/index and step into SliceOrIndex
485
544
( @convert $r: expr, $s: expr) => {
486
- <$crate:: SliceOrIndex as :: std:: convert:: From <_>>:: from( $r) . step ( $s)
545
+ <$crate:: SliceOrIndex as :: std:: convert:: From <_>>:: from( $r) . step_by ( $s)
487
546
} ;
488
547
( $( $t: tt) * ) => {
489
548
s![ @parse :: std:: marker:: PhantomData :: <$crate:: Ix0 >, [ ] $( $t) * ]
0 commit comments