use crate::{
core::{Pos, Size},
grid::GridBase,
};
pub unsafe trait BoundedGrid: GridBase {
fn width(&self) -> usize;
fn height(&self) -> usize;
fn size(&self) -> Size {
Size {
width: self.width(),
height: self.height(),
}
}
fn contains(&self, x: usize, y: usize) -> bool {
x < self.width() && y < self.height()
}
fn contains_pos(&self, pos: Pos) -> bool {
self.contains(pos.x, pos.y)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestGrid {
width: usize,
height: usize,
}
impl GridBase for TestGrid {
type Element = ();
}
unsafe impl BoundedGrid for TestGrid {
fn width(&self) -> usize {
self.width
}
fn height(&self) -> usize {
self.height
}
}
#[test]
fn size() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert_eq!(
grid.size(),
Size {
width: 10,
height: 5
}
);
}
#[test]
fn contains_true() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert!(grid.contains(5, 3));
assert!(grid.contains_pos(Pos::new(5, 3)));
}
#[test]
fn contains_false_x() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert!(!grid.contains(10, 3));
assert!(!grid.contains_pos(Pos::new(10, 3)));
}
#[test]
fn contains_false_y() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert!(!grid.contains(5, 5));
assert!(!grid.contains_pos(Pos::new(5, 5)));
}
}