use core::ops::{Deref, DerefMut};
#[cfg(feature = "alloc")]
use std_alloc::vec::Vec;
pub trait GetSize {
const CAPACITY: Option<usize>;
fn get_size(&self) -> usize;
fn size_is_zero(&self) -> bool {
self.get_size() == 0
}
fn is_full(&self) -> bool;
}
#[cfg(feature = "alloc")]
impl<T> GetSize for Vec<T> {
const CAPACITY: Option<usize> = None;
fn get_size(&self) -> usize {
self.len()
}
fn is_full(&self) -> bool {
false
}
}
impl<A: tinyvec::Array> GetSize for tinyvec::ArrayVec<A> {
const CAPACITY: Option<usize> = Some(A::CAPACITY);
fn get_size(&self) -> usize {
self.len()
}
fn is_full(&self) -> bool {
self.len() >= self.capacity()
}
}
pub trait Reserve: Default {
fn reserve(_: usize) -> Self {
Default::default()
}
}
pub trait Trunc
where Self: Sized
{
#[allow(missing_docs)]
fn trunc(&mut self, len: usize) -> ();
fn clear(&mut self) {
self.trunc(0);
}
}
#[cfg(feature = "alloc")]
impl<T> Trunc for Vec<T> {
fn trunc(&mut self, len: usize) -> () {
self.truncate(len)
}
}
impl<T, const N: usize> Trunc for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn trunc(&mut self, len: usize) -> () {
self.truncate(len)
}
}
pub trait Filled<T>: Sized {
#[allow(missing_docs)]
fn filled(t: T) -> Option<Self>
where T: Copy
{
Self::filled_using(|| t)
}
#[allow(missing_docs)]
fn filled_default() -> Option<Self>
where T: Default
{
Self::filled_using(|| Default::default())
}
#[allow(missing_docs)]
fn filled_using<F>(f: F) -> Option<Self>
where F: Fn() -> T;
}
#[cfg(feature = "alloc")]
impl<T> Reserve for Vec<T> {
fn reserve(n: usize) -> Self {
Self::with_capacity(n)
}
}
#[cfg(feature = "alloc")]
impl<T> Filled<T> for Vec<T> {
fn filled_using<F>(_: F) -> Option<Self>
where F: Fn() -> T
{
None
}
}
impl<A: tinyvec::Array> Reserve for tinyvec::ArrayVec<A> {}
impl<T, const N: usize> Filled<T> for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn filled_using<F>(f: F) -> Option<Self>
where F: Fn() -> T
{
Some(core::iter::repeat(()).take(N).map(|_| f()).collect())
}
fn filled(t: T) -> Option<Self>
where T: Copy
{
Some(Self::from([t; N]))
}
}
pub trait Array:
Default
+ GetSize
+ Reserve
+ Filled<<Self as Array>::Item>
+ Trunc
+ Deref<Target = [<Self as Array>::Item]>
+ DerefMut
+ Extend<<Self as Array>::Item>
+ FromIterator<<Self as Array>::Item>
+ IntoIterator<Item = <Self as Array>::Item>
{
type Item;
fn insert_at(&mut self, index: usize, value: <Self as Array>::Item);
fn remove(&mut self, index: usize) -> Option<<Self as Array>::Item>;
fn push(&mut self, value: <Self as Array>::Item);
}
pub trait AppendCopy<T: Copy> {
fn append_copy(&mut self, i: &[T]);
}
#[cfg(feature = "alloc")]
impl<T: Copy> AppendCopy<T> for Vec<T> {
fn append_copy(&mut self, i: &[T]) {
self.extend(i);
}
}
impl<T: Copy, A: tinyvec::Array<Item = T>> AppendCopy<T> for tinyvec::ArrayVec<A> {
fn append_copy(&mut self, i: &[T]) {
self.extend_from_slice(i);
}
}
#[cfg(feature = "alloc")]
impl<T> Array for Vec<T> {
type Item = T;
fn insert_at(&mut self, index: usize, value: T) {
self.insert(index, value);
}
fn remove(&mut self, index: usize) -> Option<T> {
if index < self.len() {
Some(Vec::remove(self, index))
} else {
None
}
}
fn push(&mut self, value: T) {
self.push(value)
}
}
impl<A: tinyvec::Array<Item = T>, T> Array for tinyvec::ArrayVec<A> where Self: Filled<T> + Trunc
{
type Item = T;
fn insert_at(&mut self, index: usize, value: A::Item) {
self.insert(index, value);
}
fn remove(&mut self, index: usize) -> Option<T> {
if index < self.len() {
Some(tinyvec::ArrayVec::remove(self, index))
} else {
None
}
}
fn push(&mut self, value: A::Item) {
self.push(value)
}
}