cl_aux/traits/
length.rs

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