PosMap: improve

This commit is contained in:
Simon Gardling 2025-03-25 11:03:14 -04:00
parent 774dc39f0d
commit fe57ccda3f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -9,8 +9,9 @@ use super::{Board, CoordPair};
pub struct PosMap<T>([T; Board::AREA.0 as usize]);
impl<T: Copy> PosMap<T> {
pub const unsafe fn uninit() -> Self {
Self([MaybeUninit::zeroed().assume_init(); Board::AREA.0 as usize])
// SAFETY! only used when creating PosMap (needs to be filled)
const unsafe fn uninit() -> Self {
Self(MaybeUninit::zeroed().assume_init())
}
pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
@ -30,10 +31,8 @@ impl<T: Copy> PosMap<T> {
const_for!(i in 0..Board::SIZE => {
const_for!(j in 0..Board::SIZE => {
// take because we are consuming `self`
n[i as usize][j as usize] =
unsafe { std::ptr::read(self.get_mut(CoordPair::from_axes(i, j))) };
});
std::mem::swap(&mut n[i as usize][j as usize], self.get_mut(CoordPair::from_axes(i, j)));
});
});
n
}
@ -67,7 +66,7 @@ mod test {
.try_into()
.unwrap();
let posmap = PosMap::from(data.clone());
let posmap = PosMap::from(data);
assert_eq!(data, posmap.into_2d());
}