1
0
Fork 0

update ArrayVec

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-17 22:07:04 +01:00
parent 00261d95c1
commit 7d7884326c
2 changed files with 19 additions and 59 deletions

View file

@ -2,12 +2,12 @@ use core::iter::ExactSizeIterator;
use core::iter::FusedIterator;
use core::mem::MaybeUninit;
#[derive(Clone)]
pub(crate) struct ArrayVec<T: Copy, const N: usize> {
pub(crate) struct ArrayVec<T, const N: usize> {
len: usize,
array: [MaybeUninit<T>; N],
}
impl<T: Copy, const N: usize> ArrayVec<T, N> {
impl<T, const N: usize> ArrayVec<T, N> {
#[inline]
pub(crate) fn new() -> Self {
Self {
@ -36,57 +36,16 @@ impl<T: Copy, const N: usize> ArrayVec<T, N> {
}
}
#[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<T, N> {
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<T, N>,
index: usize,
}
impl<'l, T: Copy, const N: usize> Iterator for ArrayVecIter<'l, T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
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<usize>) {
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<T: Copy, const N: usize> IntoIterator for ArrayVec<T, N> {
impl<T, const N: usize> IntoIterator for ArrayVec<T, N> {
type Item = T;
type IntoIter = ArrayVecIntoIter<T, N>;
#[inline]
@ -97,11 +56,12 @@ impl<T: Copy, const N: usize> IntoIterator for ArrayVec<T, N> {
}
}
}
pub(crate) struct ArrayVecIntoIter<T: Copy, const N: usize> {
pub(crate) struct ArrayVecIntoIter<T, const N: usize> {
array: ArrayVec<T, N>,
index: usize,
}
impl<T: Copy, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
impl<T, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
@ -125,8 +85,8 @@ impl<T: Copy, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
(len, Some(len))
}
}
impl<T: Copy, const N: usize> FusedIterator for ArrayVecIntoIter<T, N> {}
impl<T: Copy, const N: usize> ExactSizeIterator for ArrayVecIntoIter<T, N> {
impl<T, const N: usize> FusedIterator for ArrayVecIntoIter<T, N> {}
impl<T, const N: usize> ExactSizeIterator for ArrayVecIntoIter<T, N> {
#[inline]
fn len(&self) -> usize {
unsafe { self.array.len().unchecked_sub(self.index) }