#[cfg(feature = "alloc")]
use alloc::vec::Vec;
pub trait Retain {
type Input;
type Output;
fn retain(&mut self, input: Self::Input) -> Self::Output;
}
impl<T> Retain for Option<T> {
type Input = fn(&T) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
if let Some(elem) = self.as_mut() {
if !input(elem) {
*self = None;
}
}
}
}
#[cfg(feature = "alloc")]
impl<T> Retain for Vec<T> {
type Input = fn(&T) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(input);
}
}
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> Retain for arrayvec::ArrayVec<T, N> {
type Input = fn(&T) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(|i| input(i));
}
}
#[cfg(feature = "smallvec")]
impl<A> Retain for smallvec::SmallVec<A>
where
A: smallvec::Array,
{
type Input = fn(&A::Item) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(|i| input(i));
}
}
#[cfg(feature = "staticvec")]
impl<T, const N: usize> Retain for staticvec::StaticVec<T, N> {
type Input = fn(&T) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(input);
}
}
#[cfg(feature = "tinyvec")]
impl<A> Retain for tinyvec::ArrayVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Input = fn(&A::Item) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(input);
}
}
#[cfg(feature = "tinyvec")]
impl<A> Retain for tinyvec::TinyVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Input = fn(&A::Item) -> bool;
type Output = ();
#[inline]
fn retain(&mut self, input: Self::Input) {
self.retain(input);
}
}