From ee69c3efaf6031f281c2b26458ccaf444029b491 Mon Sep 17 00:00:00 2001 From: A248 Date: Wed, 26 Jul 2023 15:23:47 +0600 Subject: [PATCH] Fixup Range#get: add check for column out of range --- src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 714e42c6..6baca9a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -499,9 +499,18 @@ impl Range { } /// 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