Skip to content

Commit 749df7a

Browse files
committed
Use i32 throughout for indexing
1 parent a229973 commit 749df7a

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

‎src/array.rs‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<T: fmt::Display> fmt::Display for Array<T>{
1919
try!(write!(fmt,
2020
"[{}:{}]",
2121
dim.lower_bound,
22-
dim.lower_bound asisize+ dim.lenasisize - 1));
22+
dim.lower_bound + dim.len - 1));
2323
}
2424
try!(write!(fmt,"="));
2525
}
@@ -61,7 +61,7 @@ impl<T> Array<T>{
6161
/// elements specified by the dimensions.
6262
pubfnfrom_parts(data:Vec<T>,dimensions:Vec<Dimension>) -> Array<T>{
6363
assert!((data.is_empty() && dimensions.is_empty()) ||
64-
data.len() == dimensions.iter().fold(1, |acc, i| acc * i.lenasusize),
64+
data.len()asi32== dimensions.iter().fold(1, |acc, i| acc * i.len),
6565
"size mismatch");
6666
Array{
6767
dims: dimensions,
@@ -120,15 +120,15 @@ impl<T> Array<T>{
120120
&self.dims
121121
}
122122

123-
fnshift_idx(&self,indices:&[isize]) -> usize{
123+
fnshift_idx(&self,indices:&[i32]) -> i32{
124124
assert_eq!(self.dims.len(), indices.len());
125125
self.dims
126126
.iter()
127127
.zip(indices.iter().cloned())
128128
.rev()
129129
.fold((0,1), |(acc, stride),(dim, idx)| {
130130
let shifted = dim.shift(idx);
131-
(acc + shifted * stride, dim.lenasusize* stride)
131+
(acc + shifted * stride, dim.len* stride)
132132
})
133133
.0
134134
}
@@ -161,51 +161,51 @@ pub trait ArrayIndex{
161161
///
162162
/// Panics if the value of `self` does not correspond to an in-bounds
163163
/// element of the `Array`.
164-
fnindex<T>(&self,array:&Array<T>) -> usize;
164+
fnindex<T>(&self,array:&Array<T>) -> i32;
165165
}
166166

167-
impl<'a>ArrayIndexfor&'a[isize]{
168-
fnindex<T>(&self,array:&Array<T>) -> usize{
167+
impl<'a>ArrayIndexfor&'a[i32]{
168+
fnindex<T>(&self,array:&Array<T>) -> i32{
169169
array.shift_idx(*self)
170170
}
171171
}
172172

173-
implArrayIndexforisize{
174-
fnindex<T>(&self,array:&Array<T>) -> usize{
175-
let slice:&[isize] = &[*self];
173+
implArrayIndexfori32{
174+
fnindex<T>(&self,array:&Array<T>) -> i32{
175+
let slice:&[i32] = &[*self];
176176
ArrayIndex::index(&slice, array)
177177
}
178178
}
179179

180180
macro_rules! tuple_impl {
181181
($($name:ident : $t:ty),+) => {
182182
implArrayIndexfor($($t,)+){
183-
fn index<T>(&self, array:&Array<T>) -> usize{
183+
fn index<T>(&self, array:&Array<T>) -> i32{
184184
let($($name,)+) = *self;
185-
let slice:&[isize] = &[$($name),+];
185+
let slice:&[i32] = &[$($name),+];
186186
ArrayIndex::index(&slice, array)
187187
}
188188
}
189189
}
190190
}
191191

192-
tuple_impl!(a:isize);
193-
tuple_impl!(a:isize, b:isize);
194-
tuple_impl!(a:isize, b:isize, c:isize);
195-
tuple_impl!(a:isize, b:isize, c:isize, d:isize);
196-
tuple_impl!(a:isize, b:isize, c:isize, d:isize, e:isize);
197-
tuple_impl!(a:isize, b:isize, c:isize, d:isize, e:isize, f:isize);
198-
tuple_impl!(a:isize, b:isize, c:isize, d:isize, e:isize, f:isize, g:isize);
199-
tuple_impl!(a:isize, b:isize, c:isize, d:isize, e:isize, f:isize, g:isize, h:isize);
200-
tuple_impl!(a:isize, b:isize, c:isize, d:isize, e:isize, f:isize, g:isize, h:isize, i:isize);
192+
tuple_impl!(a:i32);
193+
tuple_impl!(a:i32, b:i32);
194+
tuple_impl!(a:i32, b:i32, c:i32);
195+
tuple_impl!(a:i32, b:i32, c:i32, d:i32);
196+
tuple_impl!(a:i32, b:i32, c:i32, d:i32, e:i32);
197+
tuple_impl!(a:i32, b:i32, c:i32, d:i32, e:i32, f:i32);
198+
tuple_impl!(a:i32, b:i32, c:i32, d:i32, e:i32, f:i32, g:i32);
199+
tuple_impl!(a:i32, b:i32, c:i32, d:i32, e:i32, f:i32, g:i32, h:i32);
200+
tuple_impl!(a:i32, b:i32, c:i32, d:i32, e:i32, f:i32, g:i32, h:i32, i:i32);
201201

202202
/// Indexes into the `Array`, retrieving a reference to the contained
203203
/// value.
204204
///
205205
/// Since `Array`s can be multi-dimensional, the `Index` trait is
206206
/// implemented for a variety of index types. In the most generic case, a
207-
/// `&[isize]` can be used. In addition, a bare `isize` as well as tuples
208-
/// of up to 10 `isize` values may be used for convenience.
207+
/// `&[i32]` can be used. In addition, a bare `i32` as well as tuples
208+
/// of up to 10 `i32` values may be used for convenience.
209209
///
210210
/// # Panics
211211
///
@@ -228,14 +228,14 @@ impl<T, I: ArrayIndex> Index<I> for Array<T>{
228228
typeOutput = T;
229229
fnindex(&self,idx:I) -> &T{
230230
let idx = idx.index(self);
231-
&self.data[idx]
231+
&self.data[idxasusize]
232232
}
233233
}
234234

235235
impl<T,I:ArrayIndex>IndexMut<I>forArray<T>{
236236
fnindex_mut(&mutself,idx:I) -> &mutT{
237237
let idx = idx.index(self);
238-
&mutself.data[idx]
238+
&mutself.data[idxasusize]
239239
}
240240
}
241241

‎src/lib.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ pub struct Dimension{
2222
}
2323

2424
implDimension{
25-
fnshift(&self,idx:isize) -> usize{
26-
let offset = self.lower_boundasisize;
25+
fnshift(&self,idx:i32) -> i32{
26+
let offset = self.lower_bound;
2727
assert!(idx >= offset,"out of bounds array access");
28-
assert!(offset >= 0 || idx <= 0 || usize::max_value() - (-offset)asusize>= idxasusize,
28+
assert!(offset >= 0 || idx <= 0 || i32::max_value() - (-offset) >= idx,
2929
"out of bounds array access");
30-
let shifted = idx.wrapping_sub(offset)asusize;
31-
assert!(shifted < self.len asusize,"out of bounds array access");
32-
shifted
30+
match idx.checked_sub(offset){
31+
Some(shifted) => shifted,
32+
None => panic!("out of bounds array access"),
33+
}
3334
}
3435
}
3536

0 commit comments

Comments
(0)