#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};
pub trait WithCapacity {
type Input;
fn with_capacity(input: Self::Input) -> Self;
}
impl<T, const N: usize> WithCapacity for [T; N]
where
T: Default,
{
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
[(); N].map(|_| T::default())
}
}
impl<T> WithCapacity for Option<T> {
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
None
}
}
#[cfg(feature = "alloc")]
impl WithCapacity for String {
type Input = usize;
#[inline]
fn with_capacity(input: Self::Input) -> Self {
String::with_capacity(input)
}
}
#[cfg(feature = "alloc")]
impl<T> WithCapacity for Vec<T> {
type Input = usize;
#[inline]
fn with_capacity(input: Self::Input) -> Self {
Vec::with_capacity(input)
}
}
#[cfg(feature = "arrayvec")]
impl<const N: usize> WithCapacity for arrayvec::ArrayString<N> {
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
arrayvec::ArrayString::new()
}
}
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> WithCapacity for arrayvec::ArrayVec<T, N> {
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
arrayvec::ArrayVec::new()
}
}
#[cfg(feature = "smallvec")]
impl<A> WithCapacity for smallvec::SmallVec<A>
where
A: smallvec::Array,
{
type Input = usize;
#[inline]
fn with_capacity(input: Self::Input) -> Self {
smallvec::SmallVec::with_capacity(input)
}
}
#[cfg(feature = "staticvec")]
impl<T, const N: usize> WithCapacity for staticvec::StaticVec<T, N> {
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
staticvec::StaticVec::new()
}
}
#[cfg(feature = "tinyvec")]
impl<A> WithCapacity for tinyvec::ArrayVec<A>
where
A: Default + tinyvec::Array,
A::Item: Default,
{
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
tinyvec::ArrayVec::new()
}
}
#[cfg(feature = "tinyvec")]
impl<A> WithCapacity for tinyvec::TinyVec<A>
where
A: Default + tinyvec::Array,
A::Item: Default,
{
type Input = usize;
#[inline]
fn with_capacity(_: Self::Input) -> Self {
tinyvec::TinyVec::new()
}
}