From 7d7884326cb6419f395c18c26e74f1fad62d2d6f Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Mon, 17 Nov 2025 22:07:04 +0100 Subject: [PATCH] update ArrayVec --- src/array_vec.rs | 70 +++++++++++------------------------------------- src/moves.rs | 8 +++--- 2 files changed, 19 insertions(+), 59 deletions(-) diff --git a/src/array_vec.rs b/src/array_vec.rs index dec6f0f..d6ad5ae 100644 --- a/src/array_vec.rs +++ b/src/array_vec.rs @@ -2,12 +2,12 @@ use core::iter::ExactSizeIterator; use core::iter::FusedIterator; use core::mem::MaybeUninit; -#[derive(Clone)] -pub(crate) struct ArrayVec { +pub(crate) struct ArrayVec { len: usize, array: [MaybeUninit; N], } -impl ArrayVec { + +impl ArrayVec { #[inline] pub(crate) fn new() -> Self { Self { @@ -36,57 +36,16 @@ impl ArrayVec { } } #[inline] - pub(crate) fn as_slice_mut(&mut self) -> &mut [T] { + pub(crate) fn as_slice(&self) -> &[T] { + unsafe { core::mem::transmute::<_, &[T]>(self.array.get_unchecked(0..self.len)) } + } + #[inline] + pub(crate) fn as_mut_slice(&mut self) -> &mut [T] { unsafe { core::mem::transmute::<_, &mut [T]>(self.array.get_unchecked_mut(0..self.len)) } } } -impl<'l, T: Copy, const N: usize> IntoIterator for &'l ArrayVec { - type Item = T; - type IntoIter = ArrayVecIter<'l, T, N>; - #[inline] - fn into_iter(self) -> Self::IntoIter { - ArrayVecIter { - array: self, - index: 0, - } - } -} -pub(crate) struct ArrayVecIter<'l, T: Copy, const N: usize> { - array: &'l ArrayVec, - index: usize, -} -impl<'l, T: Copy, const N: usize> Iterator for ArrayVecIter<'l, T, N> { - type Item = T; - #[inline] - fn next(&mut self) -> Option { - if self.index < self.array.len { - unsafe { - let item = self - .array - .array - .get_unchecked(self.index) - .assume_init_read(); - self.index = self.index.unchecked_add(1); - Some(item) - } - } else { - None - } - } - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } -} -impl<'l, T: Copy, const N: usize> FusedIterator for ArrayVecIter<'l, T, N> {} -impl<'l, T: Copy, const N: usize> ExactSizeIterator for ArrayVecIter<'l, T, N> { - #[inline] - fn len(&self) -> usize { - unsafe { self.array.len().unchecked_sub(self.index) } - } -} -impl IntoIterator for ArrayVec { + +impl IntoIterator for ArrayVec { type Item = T; type IntoIter = ArrayVecIntoIter; #[inline] @@ -97,11 +56,12 @@ impl IntoIterator for ArrayVec { } } } -pub(crate) struct ArrayVecIntoIter { + +pub(crate) struct ArrayVecIntoIter { array: ArrayVec, index: usize, } -impl Iterator for ArrayVecIntoIter { +impl Iterator for ArrayVecIntoIter { type Item = T; #[inline] fn next(&mut self) -> Option { @@ -125,8 +85,8 @@ impl Iterator for ArrayVecIntoIter { (len, Some(len)) } } -impl FusedIterator for ArrayVecIntoIter {} -impl ExactSizeIterator for ArrayVecIntoIter { +impl FusedIterator for ArrayVecIntoIter {} +impl ExactSizeIterator for ArrayVecIntoIter { #[inline] fn len(&self) -> usize { unsafe { self.array.len().unchecked_sub(self.index) } diff --git a/src/moves.rs b/src/moves.rs index a383efe..3dc3d76 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -256,7 +256,7 @@ impl<'l> Moves<'l> { pub fn iter(&'l self) -> MovesIter<'l> { MovesIter { position: self.position, - iter: (&self.array).into_iter(), + iter: self.array.as_slice().into_iter(), } } @@ -301,7 +301,7 @@ impl<'l> Moves<'l> { where F: FnMut(Move, Move) -> core::cmp::Ordering, { - self.array.as_slice_mut().sort_unstable_by(|a, b| { + self.array.as_mut_slice().sort_unstable_by(|a, b| { compare( Move { position: self.position, @@ -319,7 +319,7 @@ impl<'l> Moves<'l> { /// An iterator over legal moves. pub struct MovesIter<'l> { position: &'l Position, - iter: ArrayVecIter<'l, RawMove, MAX_LEGAL_MOVES>, + iter: core::slice::Iter<'l, RawMove>, } impl<'l> Iterator for MovesIter<'l> { type Item = Move<'l>; @@ -327,7 +327,7 @@ impl<'l> Iterator for MovesIter<'l> { fn next(&mut self) -> Option { self.iter.next().map(|raw| Move { position: self.position, - raw, + raw: *raw, }) } }