diff --git a/rust-toolchain.toml b/rust-toolchain.toml deleted file mode 100644 index 271800cb2f3791b3adc24328e71c9e2550b439db..0000000000000000000000000000000000000000 --- a/rust-toolchain.toml +++ /dev/null @@ -1,2 +0,0 @@ -[toolchain] -channel = "nightly" \ No newline at end of file diff --git a/src/system/utils.rs b/src/system/utils.rs index 0e58218ee804c2ac01f441e3c9cc9d02c87d0234..277e38daa656838d9f09c431f683ebe27de4c330 100644 --- a/src/system/utils.rs +++ b/src/system/utils.rs @@ -54,6 +54,18 @@ impl Indexer { ((y.as_() * self.width) + x.as_()).as_() } + pub fn index_checked( + &self, + x: impl AsPrimitive<i64>, + y: impl AsPrimitive<i64>, + ) -> Option<usize> { + if self.is_valid(x, y) { + Some(self.index(x, y)) + } else { + None + } + } + pub fn reverse(&self, index: impl AsPrimitive<i64>) -> (usize, usize) { ( (index.as_() % self.width).max(0) as usize, @@ -67,4 +79,11 @@ impl Indexer { pub fn height(&self) -> i64 { self.height } + + pub fn is_valid(&self, x: impl AsPrimitive<i64>, y: impl AsPrimitive<i64>) -> bool { + let x = x.as_(); + let y = y.as_(); + + x >= 0 && x < self.width && y >= 0 && y < self.height + } }