From 9c0c4e02a0a50f35bee15f5f129647e01b7eb30e Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 25 Mar 2025 09:42:33 -0400 Subject: [PATCH] PosMap: optimize into_2d --- src/repr/pos_map.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/repr/pos_map.rs b/src/repr/pos_map.rs index f4622ec..36f1557 100644 --- a/src/repr/pos_map.rs +++ b/src/repr/pos_map.rs @@ -24,18 +24,24 @@ impl PosMap { n } - pub const fn into_2d(&self) -> [[T; Board::SIZE as usize]; Board::SIZE as usize] { + pub const fn into_2d(mut self) -> [[T; Board::SIZE as usize]; Board::SIZE as usize] { let mut n: [[T; Board::SIZE as usize]; Board::SIZE as usize] = unsafe { MaybeUninit::zeroed().assume_init() }; const_for!(i in 0..Board::SIZE => { const_for!(j in 0..Board::SIZE => { - n[i as usize][j as usize] = *self.get(CoordPair::from_axes(i, j)); - }); + // 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))) }; + }); }); n } + pub const fn get_mut(&mut self, coords: CoordPair) -> &mut T { + &mut self.0[coords.0 as usize] + } + pub const fn get(&self, coords: CoordPair) -> &T { &self.0[coords.0 as usize] }