Skip to content

Commit

Permalink
Fixup Range#get: add check for column out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Jul 26, 2023
1 parent 3a5966f commit ee69c3e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,18 @@ impl<T: CellType> Range<T> {
}

/// Get cell value from **relative position**.
///
/// Unlike using the Index trait, this will not panic but rather yield `None` if out of range.
/// Otherwise, returns the cell value. The coordinate format is (row, column).
///
pub fn get(&self, relative_position: (usize, usize)) -> Option<&T> {
let (row, col) = relative_position;
self.inner.get(row * self.width() + col)
let (height, width) = self.get_size();
if col >= height { // row is checked implicitly
None
} else {
self.inner.get(row * width + col)
}
}

/// Get an iterator over inner rows
Expand Down

0 comments on commit ee69c3e

Please sign in to comment.