use crate::core::{Pos, Size};
pub unsafe trait TrustedSizeGrid {
fn width(&self) -> usize;
fn height(&self) -> usize;
fn size(&self) -> Size {
Size {
width: self.width(),
height: self.height(),
}
}
fn contains(&self, pos: Pos) -> bool {
pos.x < self.width() && pos.y < self.height()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestGrid {
width: usize,
height: usize,
}
unsafe impl TrustedSizeGrid 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(Pos::new(5, 3)));
}
#[test]
fn contains_false_x() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert!(!grid.contains(Pos::new(10, 3)));
}
#[test]
fn contains_false_y() {
let grid = TestGrid {
width: 10,
height: 5,
};
assert!(!grid.contains(Pos::new(5, 5)));
}
}