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