Skip to content

Commit

Permalink
Merge pull request #342 from A248/range-check-on-get
Browse files Browse the repository at this point in the history
Fixup Range#get: add check for column out of range
  • Loading branch information
tafia authored Aug 16, 2023
2 parents 3a5966f + ee69c3e commit 8a6ed46
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 8a6ed46

Please sign in to comment.