@@ -12,54 +12,88 @@ use super::{Dimension, Ixs};
1212
1313/// A slice (range with step size).
1414///
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+ ///
1518/// ## Examples
1619///
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 `[:]`.
1922///
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]`.
2326///
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]`.
2730#[ 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+ }
2936
3037impl 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+
3149 /// Returns a new `Slice` with the given step size.
3250 #[ 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 }
3553 }
3654}
3755
3856impl From < Range < Ixs > > for Slice {
3957 #[ inline]
4058 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+ }
4264 }
4365}
4466
4567impl From < RangeFrom < Ixs > > for Slice {
4668 #[ inline]
4769 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+ }
4975 }
5076}
5177
5278impl From < RangeTo < Ixs > > for Slice {
5379 #[ inline]
5480 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+ }
5686 }
5787}
5888
5989impl From < RangeFull > for Slice {
6090 #[ inline]
6191 fn from ( _: RangeFull ) -> Slice {
62- Slice ( 0 , None , 1 )
92+ Slice {
93+ start : 0 ,
94+ end : None ,
95+ step : 1 ,
96+ }
6397 }
6498}
6599
@@ -74,24 +108,29 @@ impl From<RangeFull> for Slice {
74108/// `SliceOrIndex::from(a)`. The Python equivalent is `[a]`. The macro
75109/// equivalent is `s![a]`.
76110///
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![..]`.
80114///
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]`.
84119///
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]`.
89124#[ derive( Debug , PartialEq , Eq , Hash ) ]
90125pub 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+ } ,
95134 /// A single index.
96135 Index ( Ixs ) ,
97136}
@@ -102,7 +141,7 @@ impl SliceOrIndex {
102141 /// Returns `true` if `self` is a `Slice` value.
103142 pub fn is_slice ( & self ) -> bool {
104143 match self {
105- & SliceOrIndex :: Slice ( .. ) => true ,
144+ & SliceOrIndex :: Slice { .. } => true ,
106145 _ => false ,
107146 }
108147 }
@@ -117,9 +156,9 @@ impl SliceOrIndex {
117156
118157 /// Returns a new `SliceOrIndex` with the given step size.
119158 #[ inline]
120- pub fn step ( self , step : Ixs ) -> Self {
159+ pub fn step_by ( self , step : Ixs ) -> Self {
121160 match self {
122- SliceOrIndex :: Slice ( start, end, _ ) => SliceOrIndex :: Slice ( start, end, step) ,
161+ SliceOrIndex :: Slice { start, end, .. } => SliceOrIndex :: Slice { start, end, step } ,
123162 SliceOrIndex :: Index ( s) => SliceOrIndex :: Index ( s) ,
124163 }
125164 }
@@ -129,7 +168,7 @@ impl fmt::Display for SliceOrIndex {
129168 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
130169 match * self {
131170 SliceOrIndex :: Index ( index) => write ! ( f, "{}" , index) ?,
132- SliceOrIndex :: Slice ( start, end, step) => {
171+ SliceOrIndex :: Slice { start, end, step } => {
133172 if start != 0 {
134173 write ! ( f, "{}" , start) ?;
135174 }
@@ -149,14 +188,22 @@ impl fmt::Display for SliceOrIndex {
149188impl From < Slice > for SliceOrIndex {
150189 #[ inline]
151190 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+ }
153196 }
154197}
155198
156199impl From < Range < Ixs > > for SliceOrIndex {
157200 #[ inline]
158201 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+ }
160207 }
161208}
162209
@@ -170,21 +217,33 @@ impl From<Ixs> for SliceOrIndex {
170217impl From < RangeFrom < Ixs > > for SliceOrIndex {
171218 #[ inline]
172219 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+ }
174225 }
175226}
176227
177228impl From < RangeTo < Ixs > > for SliceOrIndex {
178229 #[ inline]
179230 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+ }
181236 }
182237}
183238
184239impl From < RangeFull > for SliceOrIndex {
185240 #[ inline]
186241 fn from ( _: RangeFull ) -> SliceOrIndex {
187- SliceOrIndex :: Slice ( 0 , None , 1 )
242+ SliceOrIndex :: Slice {
243+ start : 0 ,
244+ end : None ,
245+ step : 1 ,
246+ }
188247 }
189248}
190249
@@ -483,7 +542,7 @@ macro_rules! s(
483542 } ;
484543 // convert range/index and step into SliceOrIndex
485544 ( @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)
487546 } ;
488547 ( $( $t: tt) * ) => {
489548 s![ @parse :: std:: marker:: PhantomData :: <$crate:: Ix0 >, [ ] $( $t) * ]
0 commit comments