cl_aux/traits/
size_hint.rs1use 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
11pub trait SizeHint {
13 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
37impl SizeHint for () {
41 #[inline]
42 fn size_hint(&self) -> (usize, Option<usize>) {
43 (0, Some(0))
44 }
45}
46
47impl<T> SizeHint for Option<T> {
54 #[inline]
55 fn size_hint(&self) -> (usize, Option<usize>) {
56 (self.length(), Some(self.length()))
57 }
58}
59
60impl<T> SizeHint for SingleItemStorage<T> {
65 #[inline]
66 fn size_hint(&self) -> (usize, Option<usize>) {
67 (1, Some(1))
68 }
69}
70
71impl<T> SizeHint for [T] {
76 #[inline]
77 fn size_hint(&self) -> (usize, Option<usize>) {
78 (self.len(), Some(self.len()))
79 }
80}
81
82impl<T> SizeHint for &'_ [T] {
87 #[inline]
88 fn size_hint(&self) -> (usize, Option<usize>) {
89 (self.len(), Some(self.len()))
90 }
91}
92
93impl<T> SizeHint for &'_ mut [T] {
98 #[inline]
99 fn size_hint(&self) -> (usize, Option<usize>) {
100 (self.len(), Some(self.len()))
101 }
102}
103
104impl<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#[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#[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#[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#[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#[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#[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#[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#[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#[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#[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#[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}