66// option. This file may not be copied, modified, or distributed
77// except according to those terms.
88use error:: { ShapeError , ErrorKind } ;
9- use std:: ops:: { Deref , Range , RangeFrom , RangeFull , RangeTo } ;
9+ use std:: ops:: { Deref , Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive } ;
1010use std:: fmt;
1111use std:: marker:: PhantomData ;
1212use super :: Dimension ;
1313
1414/// A slice (range with step size).
1515///
16- /// Negative `begin` or `end` indexes are counted from the back of the axis. If
17- /// `end` is `None`, the slice extends to the end of the axis.
16+ /// `end` is an exclusive index. Negative `begin` or `end` indexes are counted
17+ /// from the back of the axis. If `end` is `None`, the slice extends to the end
18+ /// of the axis.
1819///
1920/// See also the [`s![]`](macro.s.html) macro.
2021///
@@ -79,6 +80,18 @@ macro_rules! impl_slice_from_index_type {
7980 }
8081 }
8182
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+
8295 impl From <RangeFrom <$index>> for Slice {
8396 #[ inline]
8497 fn from( r: RangeFrom <$index>) -> Slice {
@@ -100,7 +113,19 @@ macro_rules! impl_slice_from_index_type {
100113 }
101114 }
102115 }
103- }
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+ } ;
104129}
105130
106131impl_slice_from_index_type ! ( isize ) ;
@@ -144,9 +169,9 @@ impl From<RangeFull> for Slice {
144169/// The macro equivalent is `s![a..;-1]`.
145170#[ derive( Debug , PartialEq , Eq , Hash ) ]
146171pub enum SliceOrIndex {
147- /// A range with step size. Negative `begin` or `end` indexes are counted
148- /// from the back of the axis. If `end` is `None`, the slice extends to the
149- /// end of the axis.
172+ /// A range with step size. `end` is an exclusive index. Negative `begin`
173+ /// or `end` indexes are counted from the back of the axis. If `end` is
174+ /// `None`, the slice extends to the end of the axis.
150175 Slice {
151176 start : isize ,
152177 end : Option < isize > ,
@@ -250,6 +275,18 @@ macro_rules! impl_sliceorindex_from_index_type {
250275 }
251276 }
252277
278+ impl From <RangeInclusive <$index>> for SliceOrIndex {
279+ #[ inline]
280+ fn from( r: RangeInclusive <$index>) -> SliceOrIndex {
281+ let end = * r. end( ) as isize ;
282+ SliceOrIndex :: Slice {
283+ start: * r. start( ) as isize ,
284+ end: if end == -1 { None } else { Some ( end + 1 ) } ,
285+ step: 1 ,
286+ }
287+ }
288+ }
289+
253290 impl From <RangeFrom <$index>> for SliceOrIndex {
254291 #[ inline]
255292 fn from( r: RangeFrom <$index>) -> SliceOrIndex {
@@ -271,7 +308,19 @@ macro_rules! impl_sliceorindex_from_index_type {
271308 }
272309 }
273310 }
274- }
311+
312+ impl From <RangeToInclusive <$index>> for SliceOrIndex {
313+ #[ inline]
314+ fn from( r: RangeToInclusive <$index>) -> SliceOrIndex {
315+ let end = r. end as isize ;
316+ SliceOrIndex :: Slice {
317+ start: 0 ,
318+ end: if end == -1 { None } else { Some ( end + 1 ) } ,
319+ step: 1 ,
320+ }
321+ }
322+ }
323+ } ;
275324}
276325
277326impl_sliceorindex_from_index_type ! ( isize ) ;
@@ -453,6 +502,12 @@ impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for Range<T> {
453502 }
454503}
455504
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+
456511impl < D1 : Dimension , T > SliceNextDim < D1 , D1 :: Larger > for RangeFrom < T > {
457512 fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
458513 PhantomData
@@ -465,6 +520,12 @@ impl<D1: Dimension, T> SliceNextDim<D1, D1::Larger> for RangeTo<T> {
465520 }
466521}
467522
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+
468529impl < D1 : Dimension > SliceNextDim < D1 , D1 :: Larger > for RangeFull {
469530 fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 :: Larger > {
470531 PhantomData
0 commit comments