From d0a7ee701a94ab2afd00abd9e4cda04c1a927804 Mon Sep 17 00:00:00 2001 From: Jack Maguire Date: Mon, 17 Apr 2023 20:50:53 +0100 Subject: [PATCH] add support for indexing (usize, usize) --- Cargo.toml | 2 +- src/lib.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 980595e..3b3a125 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grid" -version = "0.9.1-alpha.0" +version = "0.9.1-alpha.1" authors = ["Armin Becher "] edition = "2018" description = "Dynamic generic 2D data structure." diff --git a/src/lib.rs b/src/lib.rs index 7efc9f2..aee628b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -801,6 +801,22 @@ impl IndexMut for Grid { } } +impl Index<(usize, usize)> for Grid { + type Output = T; + + #[inline] + fn index(&self, (x, y): (usize, usize)) -> &T { + &self.data[y * self.cols + x] + } +} + +impl IndexMut<(usize, usize)> for Grid { + #[inline] + fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut T { + &mut self.data[y * self.cols + x] + } +} + impl fmt::Debug for Grid { #[allow(unused_must_use)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {