Skip to content
Snippets Groups Projects
Verified Commit 54a96c57 authored by Louis's avatar Louis :fire:
Browse files

Add safe indexer access methods

parent f47b00e8
No related branches found
No related tags found
No related merge requests found
[toolchain]
channel = "nightly"
\ No newline at end of file
......@@ -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
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment