diff --git a/src/elements/grid.rs b/src/elements/grid.rs index 7a9f50a..f46cef5 100644 --- a/src/elements/grid.rs +++ b/src/elements/grid.rs @@ -3,11 +3,9 @@ use anyhow::Result; use crate::builder::validator::gridinfo::GridInfo; -use std::{ - sync::{ - atomic::{AtomicBool, AtomicU32, Ordering::SeqCst}, - Arc, RwLock, RwLockReadGuard, - }, +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, 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>> { + 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())) }