cl_aux/traits/
retain.rs

1#[cfg(feature = "alloc")]
2use alloc::vec::Vec;
3
4/// See [`Retain::retain`] for more information.
5pub trait Retain {
6  /// Input
7  type Input;
8
9  /// Retains only the elements specified by the `F` predicate.
10  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
25/// ```rust
26/// let mut opt = Some(1);
27/// cl_aux::Retain::retain(&mut opt, |n| n % 2 == 0);
28/// assert_eq!(opt, None);
29/// ```
30impl<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/// ```rust
44/// let mut structure = cl_aux::doc_tests::vec();
45/// cl_aux::Retain::retain(&mut structure, |n| n % 2 == 0);
46/// assert_eq!(&structure, &[2]);
47/// ```
48#[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/// ```rust
59/// let mut structure = cl_aux::doc_tests::array_vec();
60/// cl_aux::Retain::retain(&mut structure, |n| n % 2 == 0);
61/// assert_eq!(&structure[..], &[2]);
62/// ```
63#[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/// ```rust
74/// let mut structure = cl_aux::doc_tests::small_vec();
75/// cl_aux::Retain::retain(&mut structure, |n| n % 2 == 0);
76/// assert_eq!(&structure[..], &[2]);
77/// ```
78#[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/// ```rust
92/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
93/// cl_aux::Retain::retain(&mut structure, |n| n % 2 == 0);
94/// assert_eq!(&structure[..], &[2]);
95/// ```
96#[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/// ```rust
111/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
112/// cl_aux::Retain::retain(&mut structure, |n| n % 2 == 0);
113/// assert_eq!(&structure[..], &[2]);
114/// ```
115#[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}