Add bounds check for child_at

This commit is contained in:
hodasemi 2023-01-18 07:28:32 +01:00
parent 70e54664e9
commit 74355aac21

View file

@ -3,11 +3,9 @@ use anyhow::Result;
use crate::builder::validator::gridinfo::GridInfo;
use std::{
sync::{
use std::sync::{
atomic::{AtomicBool, AtomicU32, Ordering::SeqCst},
Arc, RwLock, RwLockReadGuard,
},
};
use std::{ops::Deref, sync::Weak};
@ -68,7 +66,9 @@ impl ChildState {
fn get(&self) -> Option<(Arc<dyn GuiElementTraits>, u32, u32)> {
match self {
ChildState::Some { child, x, y } => Some((child.clone(), *x, *y)),
ChildState::Extend { weak_child, x, y } => weak_child.upgrade().map(|child| (child, *x, *y)),
ChildState::Extend { weak_child, x, y } => {
weak_child.upgrade().map(|child| (child, *x, *y))
}
ChildState::None => None,
}
}
@ -177,6 +177,22 @@ impl Grid {
}
pub fn child_at(&self, x: usize, y: usize) -> Result<Option<Arc<dyn GuiElementTraits>>> {
if x >= self.dim_x {
return Err(anyhow::anyhow!(
"Tried to access Grid at {} while only being {} wide",
x,
self.dim_x
));
}
if y >= self.dim_y {
return Err(anyhow::anyhow!(
"Tried to access Grid at {} while only being {} tall",
y,
self.dim_y
));
}
Ok(self.children.read().unwrap()[x][y].map(|c| c.clone()))
}