cl_aux/traits/
size_hint.rs

1use crate::{IterWrapper, Length as _, SingleItemStorage};
2#[cfg(feature = "alloc")]
3use alloc::{
4  collections::{BTreeMap, BTreeSet},
5  string::String,
6  vec::Vec,
7};
8#[cfg(feature = "std")]
9use std::collections::{HashMap, HashSet};
10
11/// See [`SizeHint::size_hint`] for more information.
12pub trait SizeHint {
13  /// Has the same semantics of [`Iterator::size_hint`].
14  fn size_hint(&self) -> (usize, Option<usize>);
15}
16
17impl<T> SizeHint for &T
18where
19  T: SizeHint,
20{
21  #[inline]
22  fn size_hint(&self) -> (usize, Option<usize>) {
23    (*self).size_hint()
24  }
25}
26
27impl<T> SizeHint for IterWrapper<T>
28where
29  T: Iterator,
30{
31  #[inline]
32  fn size_hint(&self) -> (usize, Option<usize>) {
33    self.0.size_hint()
34  }
35}
36
37/// ```rust
38/// assert_eq!(cl_aux::SizeHint::size_hint(&()), (0, Some(0)));
39/// ```
40impl SizeHint for () {
41  #[inline]
42  fn size_hint(&self) -> (usize, Option<usize>) {
43    (0, Some(0))
44  }
45}
46
47/// ```rust
48/// let mut opt = Some(0);
49/// assert_eq!(cl_aux::SizeHint::size_hint(&opt), (1, Some(1)));
50/// opt.take();
51/// assert_eq!(cl_aux::SizeHint::size_hint(&opt), (0, Some(0)));
52/// ```
53impl<T> SizeHint for Option<T> {
54  #[inline]
55  fn size_hint(&self) -> (usize, Option<usize>) {
56    (self.length(), Some(self.length()))
57  }
58}
59
60/// ```rust
61/// let structure = cl_aux::doc_tests::single_item_storage();
62/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (1, Some(1)));
63/// ```
64impl<T> SizeHint for SingleItemStorage<T> {
65  #[inline]
66  fn size_hint(&self) -> (usize, Option<usize>) {
67    (1, Some(1))
68  }
69}
70
71/// ```rust
72/// let structure = cl_aux::doc_tests::slice();
73/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
74/// ```
75impl<T> SizeHint for [T] {
76  #[inline]
77  fn size_hint(&self) -> (usize, Option<usize>) {
78    (self.len(), Some(self.len()))
79  }
80}
81
82/// ```rust
83/// let structure = cl_aux::doc_tests::slice();
84/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
85/// ```
86impl<T> SizeHint for &'_ [T] {
87  #[inline]
88  fn size_hint(&self) -> (usize, Option<usize>) {
89    (self.len(), Some(self.len()))
90  }
91}
92
93/// ```rust
94/// let mut structure = cl_aux::doc_tests::slice_mut!();
95/// assert_eq!(cl_aux::SizeHint::size_hint(&mut structure), (3, Some(3)));
96/// ```
97impl<T> SizeHint for &'_ mut [T] {
98  #[inline]
99  fn size_hint(&self) -> (usize, Option<usize>) {
100    (self.len(), Some(self.len()))
101  }
102}
103
104/// ```rust
105/// let structure = cl_aux::doc_tests::array();
106/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
107/// ```
108impl<T, const N: usize> SizeHint for [T; N] {
109  #[inline]
110  fn size_hint(&self) -> (usize, Option<usize>) {
111    (self.len(), Some(self.len()))
112  }
113}
114
115/// ```rust
116/// let mut structure = cl_aux::doc_tests::b_tree_map();
117/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
118/// ```
119#[cfg(feature = "alloc")]
120impl<K, V> SizeHint for BTreeMap<K, V> {
121  #[inline]
122  fn size_hint(&self) -> (usize, Option<usize>) {
123    (self.len(), Some(self.len()))
124  }
125}
126
127/// ```rust
128/// let mut structure = cl_aux::doc_tests::b_tree_set();
129/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
130/// ```
131#[cfg(feature = "alloc")]
132impl<V> SizeHint for BTreeSet<V> {
133  #[inline]
134  fn size_hint(&self) -> (usize, Option<usize>) {
135    (self.len(), Some(self.len()))
136  }
137}
138
139/// ```rust
140/// let mut structure = cl_aux::doc_tests::hash_map();
141/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
142/// ```
143#[cfg(feature = "std")]
144impl<K, V, S> SizeHint for HashMap<K, V, S>
145where
146  S: core::hash::BuildHasher,
147{
148  #[inline]
149  fn size_hint(&self) -> (usize, Option<usize>) {
150    (self.len(), Some(self.len()))
151  }
152}
153
154/// ```rust
155/// let mut structure = cl_aux::doc_tests::hash_set();
156/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
157/// ```
158#[cfg(feature = "std")]
159impl<V, S> SizeHint for HashSet<V, S>
160where
161  S: core::hash::BuildHasher,
162{
163  #[inline]
164  fn size_hint(&self) -> (usize, Option<usize>) {
165    (self.len(), Some(self.len()))
166  }
167}
168
169/// ```rust
170/// let structure = cl_aux::doc_tests::string();
171/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (5, Some(5)));
172/// ```
173#[cfg(feature = "alloc")]
174impl SizeHint for String {
175  #[inline]
176  fn size_hint(&self) -> (usize, Option<usize>) {
177    (self.len(), Some(self.len()))
178  }
179}
180
181/// ```rust
182/// let structure = cl_aux::doc_tests::vec();
183/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
184/// ```
185#[cfg(feature = "alloc")]
186impl<T> SizeHint for Vec<T> {
187  #[inline]
188  fn size_hint(&self) -> (usize, Option<usize>) {
189    (self.len(), Some(self.len()))
190  }
191}
192
193/// ```rust
194/// let structure = cl_aux::doc_tests::array_string();
195/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (5, Some(5)));
196/// ```
197#[cfg(feature = "arrayvec")]
198impl<const N: usize> SizeHint for arrayvec::ArrayString<N> {
199  #[inline]
200  fn size_hint(&self) -> (usize, Option<usize>) {
201    (self.len(), Some(self.len()))
202  }
203}
204
205/// ```rust
206/// let structure = cl_aux::doc_tests::array_vec();
207/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
208/// ```
209#[cfg(feature = "arrayvec")]
210impl<T, const N: usize> SizeHint for arrayvec::ArrayVec<T, N> {
211  #[inline]
212  fn size_hint(&self) -> (usize, Option<usize>) {
213    (self.len(), Some(self.len()))
214  }
215}
216
217/// ```rust
218/// let structure = cl_aux::doc_tests::small_vec();
219/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
220/// ```
221#[cfg(feature = "smallvec")]
222impl<A> SizeHint for smallvec::SmallVec<A>
223where
224  A: smallvec::Array,
225{
226  #[inline]
227  fn size_hint(&self) -> (usize, Option<usize>) {
228    (self.len(), Some(self.len()))
229  }
230}
231
232/// ```rust
233/// let structure = cl_aux::doc_tests::tiny_vec_array_vec();
234/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
235/// ```
236#[cfg(feature = "tinyvec")]
237impl<A> SizeHint for tinyvec::ArrayVec<A>
238where
239  A: tinyvec::Array,
240  A::Item: Default,
241{
242  #[inline]
243  fn size_hint(&self) -> (usize, Option<usize>) {
244    (self.len(), Some(self.len()))
245  }
246}
247
248/// ```rust
249/// let structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
250/// assert_eq!(cl_aux::SizeHint::size_hint(&structure), (3, Some(3)));
251/// ```
252#[cfg(all(feature = "alloc", feature = "tinyvec"))]
253impl<A> SizeHint for tinyvec::TinyVec<A>
254where
255  A: tinyvec::Array,
256  A::Item: Default,
257{
258  #[inline]
259  fn size_hint(&self) -> (usize, Option<usize>) {
260    (self.len(), Some(self.len()))
261  }
262}