Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow any int type for indexing #57

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,11 +1555,13 @@ impl<T: hash::Hash> hash::Hash for Grid<T> {
}
}

impl<T> Index<(usize, usize)> for Grid<T> {
impl<T, U: TryInto<usize>, V: TryInto<usize>> Index<(U, V)> for Grid<T> {
type Output = T;

#[inline]
fn index(&self, (row, col): (usize, usize)) -> &T {
fn index(&self, (row, col): (U, V)) -> &T {
let row = row.try_into().ok().expect("row index less than zero");
let col = col.try_into().ok().expect("col index less than zero");
assert!(
!(row >= self.rows || col >= self.cols),
"grid index out of bounds: ({row},{col}) out of ({},{})",
Expand All @@ -1571,9 +1573,11 @@ impl<T> Index<(usize, usize)> for Grid<T> {
}
}

impl<T> IndexMut<(usize, usize)> for Grid<T> {
impl<T, U: TryInto<usize>, V: TryInto<usize>> IndexMut<(U, V)> for Grid<T> {
#[inline]
fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut T {
fn index_mut(&mut self, (row, col): (U, V)) -> &mut T {
let row = row.try_into().ok().expect("row index less than zero");
let col = col.try_into().ok().expect("col index less than zero");
assert!(
!(row >= self.rows || col >= self.cols),
"grid index out of bounds: ({row},{col}) out of ({},{})",
Expand Down Expand Up @@ -3169,10 +3173,19 @@ mod test {
let grid: Grid<u8> = Grid::from_vec(vec![1, 2, 3, 4], 2);
assert_eq!(grid[(0, 0)], 1);
assert_eq!(grid[(0, 1)], 2);
assert_eq!(grid[(1, 0)], 3);
assert_eq!(grid[(1u32, 0u16)], 3);
assert_eq!(grid[(1, 1)], 4);
}

#[test]
fn idx_signed_tup() {
let grid: Grid<u8> = Grid::from_vec(vec![1, 2, 3, 4], 2);
assert_eq!(grid[(0i64, 0i64)], 1);
assert_eq!(grid[(0i64, 1i64)], 2);
assert_eq!(grid[(1i32, 0u16)], 3);
assert_eq!(grid[(1usize, 1i16)], 4);
}

#[test]
#[should_panic]
#[allow(clippy::should_panic_without_expect)]
Expand All @@ -3189,10 +3202,18 @@ mod test {
let _ = grid[(0, 20)];
}

#[test]
#[should_panic]
#[allow(clippy::should_panic_without_expect)]
fn idx_tup_panic_3() {
let grid = Grid::init(1, 2, 3);
let _ = grid[(1, -1)];
}

#[test]
fn idx_tup_set() {
let mut grid = Grid::init(1, 2, 3);
grid[(0, 0)] = 4;
grid[(0u32, 0i16)] = 4;
assert_eq!(grid[(0, 0)], 4);
}

Expand Down
Loading