use derive_more::{From, Into};
use std::vec::Vec;
pub type OneOrMore<T> = SmallVec<T, 1>;
pub type TwoOrMore<T> = SmallVec<T, 2>;
pub type FourOrMore<T> = SmallVec<T, 4>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From, Into)]
pub struct SmallVec<T, const N: usize>(smallvec::SmallVec<[T; N]>);
impl<T, const N: usize> Default for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn default() -> Self {
Self::new()
}
}
impl<T, const N: usize> core::ops::Deref for SmallVec<T, N> {
type Target = smallvec::SmallVec<[T; N]>;
#[cfg_attr(not(tarpaulin), inline(always))]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T, const N: usize> core::ops::DerefMut for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T, const N: usize> AsRef<[T]> for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn as_ref(&self) -> &[T] {
&self.0
}
}
impl<T, const N: usize> AsMut<[T]> for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn as_mut(&mut self) -> &mut [T] {
&mut self.0
}
}
impl<T, const N: usize> core::iter::FromIterator<T> for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self(iter.into_iter().collect())
}
}
impl<T, const N: usize> core::iter::Extend<T> for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.0.extend(iter);
}
}
impl<T, const N: usize> IntoIterator for SmallVec<T, N> {
type Item = T;
type IntoIter = smallvec::IntoIter<[T; N]>;
#[cfg_attr(not(tarpaulin), inline(always))]
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T, const N: usize> core::iter::IntoIterator for &'a SmallVec<T, N> {
type Item = &'a T;
type IntoIter = core::slice::Iter<'a, T>;
#[cfg_attr(not(tarpaulin), inline(always))]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<T, const N: usize> From<Vec<T>> for SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn from(value: Vec<T>) -> Self {
Self(smallvec::SmallVec::from_vec(value))
}
}
impl<T, const N: usize> From<SmallVec<T, N>> for Vec<T> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn from(value: SmallVec<T, N>) -> Self {
value.0.into_vec()
}
}
impl<T, const N: usize> SmallVec<T, N> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new() -> Self {
Self(smallvec::SmallVec::new_const())
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_capacity(capacity: usize) -> Self {
Self(smallvec::SmallVec::with_capacity(capacity))
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn from_const(vec: [T; N]) -> Self {
Self(smallvec::SmallVec::from_const(vec))
}
}