#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};
pub trait Extend<IN> {
type Error;
type Output;
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = IN>,
) -> Result<Self::Output, Self::Error>;
}
impl<T> Extend<T> for () {
type Error = crate::Error;
type Output = ();
#[inline]
fn extend(&mut self, _: impl IntoIterator<Item = T>) -> Result<Self::Output, Self::Error> {
Ok(())
}
}
impl<T> Extend<T> for Option<T> {
type Error = crate::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = T>,
) -> Result<Self::Output, Self::Error> {
_check_capacity!(self);
let err = || crate::Error::InsufficientCapacity(stringify!(self), 1);
let mut iter = into_iter.into_iter();
let next = iter.next().ok_or_else(err)?;
*self = Some(next);
if iter.next().is_some() {
Err(err())
} else {
Ok(())
}
}
}
#[cfg(feature = "alloc")]
impl Extend<char> for String {
type Error = crate::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = char>,
) -> Result<Self::Output, Self::Error> {
core::iter::Extend::extend(self, into_iter);
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<T> Extend<T> for Vec<T> {
type Error = crate::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = T>,
) -> Result<Self::Output, Self::Error> {
core::iter::Extend::extend(self, into_iter);
Ok(())
}
}
#[cfg(feature = "arrayvec")]
impl<const N: usize> Extend<char> for arrayvec::ArrayString<N>
where
Self: crate::Push<char, Output = ()>,
{
type Error = <Self as crate::Push<char>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = char>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> Extend<T> for arrayvec::ArrayVec<T, N>
where
Self: crate::Push<T, Output = ()>,
{
type Error = <Self as crate::Push<T>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = T>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}
#[cfg(feature = "smallvec")]
impl<A> Extend<A::Item> for smallvec::SmallVec<A>
where
A: smallvec::Array,
Self: crate::Push<A::Item, Output = ()>,
{
type Error = <Self as crate::Push<A::Item>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = A::Item>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}
#[cfg(feature = "staticvec")]
impl<T, const N: usize> Extend<T> for staticvec::StaticVec<T, N>
where
Self: crate::Push<T, Output = ()>,
{
type Error = <Self as crate::Push<T>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = T>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}
#[cfg(feature = "tinyvec")]
impl<A> Extend<A::Item> for tinyvec::ArrayVec<A>
where
A: tinyvec::Array,
Self: crate::Push<A::Item, Output = ()>,
{
type Error = <Self as crate::Push<A::Item>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = A::Item>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}
#[cfg(feature = "tinyvec")]
impl<A> Extend<A::Item> for tinyvec::TinyVec<A>
where
A: tinyvec::Array,
A::Item: Default,
Self: crate::Push<A::Item, Output = ()>,
{
type Error = <Self as crate::Push<A::Item>>::Error;
type Output = ();
#[inline]
fn extend(
&mut self,
into_iter: impl IntoIterator<Item = A::Item>,
) -> Result<Self::Output, Self::Error> {
for elem in into_iter.into_iter() {
crate::Push::push(self, elem)?;
}
Ok(())
}
}