1#[cfg(feature = "alloc")]
2use alloc::vec::Vec;
3
4pub trait Retain {
6 type Input;
8
9 fn retain(&mut self, input: Self::Input);
11}
12
13impl<T> Retain for &mut T
14where
15 T: Retain,
16{
17 type Input = T::Input;
18
19 #[inline]
20 fn retain(&mut self, input: Self::Input) {
21 (*self).retain(input);
22 }
23}
24
25impl<T> Retain for Option<T> {
31 type Input = fn(&T) -> bool;
32
33 #[inline]
34 fn retain(&mut self, input: Self::Input) {
35 if let Some(elem) = self.as_mut() {
36 if !input(elem) {
37 *self = None;
38 }
39 }
40 }
41}
42
43#[cfg(feature = "alloc")]
49impl<T> Retain for Vec<T> {
50 type Input = fn(&T) -> bool;
51
52 #[inline]
53 fn retain(&mut self, input: Self::Input) {
54 self.retain(input);
55 }
56}
57
58#[cfg(feature = "arrayvec")]
64impl<T, const N: usize> Retain for arrayvec::ArrayVec<T, N> {
65 type Input = fn(&T) -> bool;
66
67 #[inline]
68 fn retain(&mut self, input: Self::Input) {
69 self.retain(|i| input(i));
70 }
71}
72
73#[cfg(feature = "smallvec")]
79impl<A> Retain for smallvec::SmallVec<A>
80where
81 A: smallvec::Array,
82{
83 type Input = fn(&A::Item) -> bool;
84
85 #[inline]
86 fn retain(&mut self, input: Self::Input) {
87 self.retain(|i| input(i));
88 }
89}
90
91#[cfg(feature = "tinyvec")]
97impl<A> Retain for tinyvec::ArrayVec<A>
98where
99 A: tinyvec::Array,
100 A::Item: Default,
101{
102 type Input = fn(&A::Item) -> bool;
103
104 #[inline]
105 fn retain(&mut self, input: Self::Input) {
106 self.retain(input);
107 }
108}
109
110#[cfg(all(feature = "alloc", feature = "tinyvec"))]
116impl<A> Retain for tinyvec::TinyVec<A>
117where
118 A: tinyvec::Array,
119 A::Item: Default,
120{
121 type Input = fn(&A::Item) -> bool;
122
123 #[inline]
124 fn retain(&mut self, input: Self::Input) {
125 self.retain(input);
126 }
127}