use std::ops::{Deref, DerefMut};
use arrayvec::ArrayVec as ArrayVecInner;
const fn check_capacity<const N: usize, const CAP: usize>() {
assert!(N <= CAP, "Array size exceeds capacity");
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArrayVec<T, const CAP: usize>(ArrayVecInner<T, CAP>);
impl<T, const CAP: usize> Default for ArrayVec<T, CAP> {
fn default() -> Self {
Self(ArrayVecInner::new())
}
}
impl<T, const CAP: usize> ArrayVec<T, CAP> {
pub fn new<const N: usize>(arr: [T; N]) -> Self {
check_capacity::<N, CAP>();
let mut inner = ArrayVecInner::new();
for item in arr {
inner.push(item);
}
Self(inner)
}
pub fn empty() -> Self {
Self(ArrayVecInner::new())
}
}
impl<T, const CAP: usize> Deref for ArrayVec<T, CAP> {
type Target = ArrayVecInner<T, CAP>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP> {
type Item = T;
type IntoIter = arrayvec::IntoIter<T, CAP>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<'a, T, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
}