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 as isize + dim.len as isize - 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
pub fn from_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.len as usize),
64+
data.len() as i32 == 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-
fn shift_idx(&self, indices: &[isize]) -> usize {
123+
fn shift_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.len as usize * 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-
fn index<T>(&self, array: &Array<T>) -> usize;
164+
fn index<T>(&self, array: &Array<T>) -> i32;
165165
}
166166

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

173-
impl ArrayIndex for isize {
174-
fn index<T>(&self, array: &Array<T>) -> usize {
175-
let slice: &[isize] = &[*self];
173+
impl ArrayIndex for i32 {
174+
fn index<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
impl ArrayIndex for ($($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
type Output = T;
229229
fn index(&self, idx: I) -> &T {
230230
let idx = idx.index(self);
231-
&self.data[idx]
231+
&self.data[idx as usize]
232232
}
233233
}
234234

235235
impl<T, I: ArrayIndex> IndexMut<I> for Array<T> {
236236
fn index_mut(&mut self, idx: I) -> &mut T {
237237
let idx = idx.index(self);
238-
&mut self.data[idx]
238+
&mut self.data[idx as usize]
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
impl Dimension {
25-
fn shift(&self, idx: isize) -> usize {
26-
let offset = self.lower_bound as isize;
25+
fn shift(&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) as usize >= idx as usize,
28+
assert!(offset >= 0 || idx <= 0 || i32::max_value() - (-offset) >= idx,
2929
"out of bounds array access");
30-
let shifted = idx.wrapping_sub(offset) as usize;
31-
assert!(shifted < self.len as usize, "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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy