Skip to content

Commit 14ff223

Browse files
committed
Add support for more index types in s![] macro
This commit also removes use of the `Ixs` type alias in the `slice` module.
1 parent 3d9134b commit 14ff223

File tree

1 file changed

+111
-87
lines changed

1 file changed

+111
-87
lines changed

src/slice.rs

Lines changed: 111 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::ops::{Deref, Range, RangeFrom, RangeFull, RangeTo};
99
use std::fmt;
1010
use std::marker::PhantomData;
11-
use super::{Dimension, Ixs};
11+
use super::Dimension;
1212

1313
/// A slice (range with step size).
1414
///
@@ -45,44 +45,52 @@ impl Slice {
4545

4646
/// Returns a new `Slice` with the given step size.
4747
#[inline]
48-
pub fn step_by(self, step: Ixs) -> Self {
48+
pub fn step_by(self, step: isize) -> Self {
4949
Slice { step, ..self }
5050
}
5151
}
5252

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+
}
6064
}
61-
}
62-
}
6365

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+
}
7175
}
72-
}
73-
}
7476

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+
}
8286
}
8387
}
8488
}
8589

90+
impl_slice_from_index_type!(isize);
91+
impl_slice_from_index_type!(usize);
92+
impl_slice_from_index_type!(i32);
93+
8694
impl From<RangeFull> for Slice {
8795
#[inline]
8896
fn from(_: RangeFull) -> Slice {
@@ -124,12 +132,12 @@ pub enum SliceOrIndex {
124132
/// from the back of the axis. If `end` is `None`, the slice extends to the
125133
/// end of the axis.
126134
Slice {
127-
start: Ixs,
128-
end: Option<Ixs>,
129-
step: Ixs,
135+
start: isize,
136+
end: Option<isize>,
137+
step: isize,
130138
},
131139
/// A single index.
132-
Index(Ixs),
140+
Index(isize),
133141
}
134142

135143
copy_and_clone!{SliceOrIndex}
@@ -153,7 +161,7 @@ impl SliceOrIndex {
153161

154162
/// Returns a new `SliceOrIndex` with the given step size.
155163
#[inline]
156-
pub fn step_by(self, step: Ixs) -> Self {
164+
pub fn step_by(self, step: isize) -> Self {
157165
match self {
158166
SliceOrIndex::Slice { start, end, .. } => SliceOrIndex::Slice { start, end, step },
159167
SliceOrIndex::Index(s) => SliceOrIndex::Index(s),
@@ -193,46 +201,54 @@ impl From<Slice> for SliceOrIndex {
193201
}
194202
}
195203

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+
}
203211
}
204-
}
205-
}
206212

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+
}
213223

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+
}
221233
}
222-
}
223-
}
224234

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+
}
232244
}
233245
}
234246
}
235247

248+
impl_sliceorindex_from_index_type!(isize);
249+
impl_sliceorindex_from_index_type!(usize);
250+
impl_sliceorindex_from_index_type!(i32);
251+
236252
impl From<RangeFull> for SliceOrIndex {
237253
#[inline]
238254
fn from(_: RangeFull) -> SliceOrIndex {
@@ -387,36 +403,44 @@ impl<D1: Dimension> SliceNextDim<D1, D1::Larger> for Slice {
387403
}
388404
}
389405

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+
}
395413

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+
}
401419

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+
}
405431
}
406432
}
407433

434+
impl_slicenextdim_for_index_type!(isize);
435+
impl_slicenextdim_for_index_type!(usize);
436+
impl_slicenextdim_for_index_type!(i32);
437+
408438
impl<D1: Dimension> SliceNextDim<D1, D1::Larger> for RangeFull {
409439
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1::Larger> {
410440
PhantomData
411441
}
412442
}
413443

414-
impl<D1: Dimension> SliceNextDim<D1, D1> for Ixs {
415-
fn next_dim(&self, _: PhantomData<D1>) -> PhantomData<D1> {
416-
PhantomData
417-
}
418-
}
419-
420444
/// Slice argument constructor.
421445
///
422446
/// `s![]` takes a list of ranges/slices/indices, separated by comma, with
@@ -443,10 +467,10 @@ impl<D1: Dimension> SliceNextDim<D1, D1> for Ixs {
443467
///
444468
/// The number of *axis-slice-or-index* must match the number of axes in the
445469
/// 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.
450474
///
451475
/// For example `s![0..4;2, 6, 1..5]` is a slice of the first axis for 0..4
452476
/// with step size 2, a subview of the second axis at index 6, and a slice of
@@ -539,7 +563,7 @@ macro_rules! s(
539563
};
540564
// convert range/index and step into SliceOrIndex
541565
(@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)
543567
};
544568
($($t:tt)*) => {
545569
s![@parse ::std::marker::PhantomData::<$crate::Ix0>, [] $($t)*]

0 commit comments

Comments
 (0)