use crate::OptArrayVec;
impl<T, const CAP: usize> OptArrayVec<T, CAP> {
const ARRAY_REPEAT_VALUE: Option<T> = None;
pub fn cap(&self) -> usize {
CAP
}
pub fn len(&self) -> usize {
self.inner.iter().filter(|x| x.is_some()).count()
}
pub fn is_empty(&self) -> bool {
if CAP == 0 {
true
} else {
self.inner[0].is_none()
}
}
#[must_use]
pub const fn new() -> Self {
Self {
inner: [Self::ARRAY_REPEAT_VALUE; CAP],
}
}
pub fn push(&mut self, element: T) {
for i in 0..CAP {
let at = &mut self.inner[i];
if at.is_none() {
*at = Some(element);
return;
}
}
panic!("OptArrayVec is full!")
}
pub fn clear(&mut self) {
*self = Self::new();
}
pub fn pop(&mut self) -> Option<T> {
for i in (0..CAP).rev() {
let at = &mut self.inner[i];
if let Some(val) = at.take() {
return Some(val);
}
}
None
}
pub fn into_inner(self) -> [Option<T>; CAP] {
self.inner
}
pub fn remaining_capacity(&self) -> usize {
CAP - self.len()
}
}