Debian Codegraph @

debian/rust-vendor/itertools/tests/laziness.rs

raw
1 #![allow(unstable_name_collisions)]
2
3 use itertools::Itertools;
4
5 #[derive(Debug, Clone)]
6 #[must_use = "iterators are lazy and do nothing unless consumed"]
7 struct Panicking;
8
9 impl Iterator for Panicking {
10 type Item = u8;
11
12 fn next(&mut self) -> Option<u8> {
13 panic!("iterator adaptor is not lazy")
14 }
15
16 fn size_hint(&self) -> (usize, Option<usize>) {
17 (0, Some(0))
18 }
19 }
20
21 impl ExactSizeIterator for Panicking {}
22
23 /// ## Usage example
24 /// ```compile_fail
25 /// must_use_tests! {
26 /// name {
27 /// Panicking.name(); // Add `let _ =` only if required (encountered error).
28 /// }
29 /// // ...
30 /// }
31 /// ```
32 ///
33 /// **TODO:** test missing `must_use` attributes better, maybe with a new lint.
34 macro_rules! must_use_tests {
35 ($($(#[$attr:meta])* $name:ident $body:block)*) => {
36 $(
37 /// `#[deny(unused_must_use)]` should force us to ignore the resulting iterators
38 /// by adding `let _ = ...;` on every iterator.
39 /// If it does not, then a `must_use` attribute is missing on the associated struct.
40 ///
41 /// However, it's only helpful if we don't add `let _ =` before seeing if there is an error or not.
42 /// And it does not protect us against removed `must_use` attributes.
43 /// There is no simple way to test this yet.
44 #[deny(unused_must_use)]
45 #[test]
46 $(#[$attr])*
47 fn $name() $body
48 )*
49 };
50 }
51
52 must_use_tests! {
53 // Itertools trait:
54 interleave {
55 let _ = Panicking.interleave(Panicking);
56 }
57 interleave_shortest {
58 let _ = Panicking.interleave_shortest(Panicking);
59 }
60 intersperse {
61 let _ = Panicking.intersperse(0);
62 }
63 intersperse_with {
64 let _ = Panicking.intersperse_with(|| 0);
65 }
66 get {
67 let _ = Panicking.get(1..4);
68 let _ = Panicking.get(1..=4);
69 let _ = Panicking.get(1..);
70 let _ = Panicking.get(..4);
71 let _ = Panicking.get(..=4);
72 let _ = Panicking.get(..);
73 }
74 zip_longest {
75 let _ = Panicking.zip_longest(Panicking);
76 }
77 zip_eq {
78 let _ = Panicking.zip_eq(Panicking);
79 }
80 batching {
81 let _ = Panicking.batching(Iterator::next);
82 }
83 chunk_by {
84 // ChunkBy
85 let _ = Panicking.chunk_by(|x| *x);
86 // Groups
87 let _ = Panicking.chunk_by(|x| *x).into_iter();
88 }
89 chunks {
90 // IntoChunks
91 let _ = Panicking.chunks(1);
92 let _ = Panicking.chunks(2);
93 // Chunks
94 let _ = Panicking.chunks(1).into_iter();
95 let _ = Panicking.chunks(2).into_iter();
96 }
97 tuple_windows {
98 let _ = Panicking.tuple_windows::<(_,)>();
99 let _ = Panicking.tuple_windows::<(_, _)>();
100 let _ = Panicking.tuple_windows::<(_, _, _)>();
101 }
102 circular_tuple_windows {
103 let _ = Panicking.circular_tuple_windows::<(_,)>();
104 let _ = Panicking.circular_tuple_windows::<(_, _)>();
105 let _ = Panicking.circular_tuple_windows::<(_, _, _)>();
106 }
107 array_windows {
108 let _ = Panicking.array_windows::<0>();
109 let _ = Panicking.array_windows::<1>();
110 let _ = Panicking.array_windows::<2>();
111 let _ = Panicking.array_windows::<3>();
112 }
113 circular_array_windows {
114 let _ = Panicking.circular_array_windows::<0>();
115 let _ = Panicking.circular_array_windows::<1>();
116 let _ = Panicking.circular_array_windows::<2>();
117 let _ = Panicking.circular_array_windows::<3>();
118 }
119 tuples {
120 let _ = Panicking.tuples::<(_,)>();
121 let _ = Panicking.tuples::<(_, _)>();
122 let _ = Panicking.tuples::<(_, _, _)>();
123 }
124 tee {
125 let _ = Panicking.tee();
126 }
127 map_into {
128 let _ = Panicking.map_into::<u16>();
129 }
130 map_ok {
131 let _ = Panicking.map(Ok::<u8, ()>).map_ok(|x| x + 1);
132 }
133 filter_ok {
134 let _ = Panicking.map(Ok::<u8, ()>).filter_ok(|x| x % 2 == 0);
135 }
136 filter_map_ok {
137 let _ = Panicking.map(Ok::<u8, ()>).filter_map_ok(|x| {
138 if x % 2 == 0 {
139 Some(x + 1)
140 } else {
141 None
142 }
143 });
144 }
145 flatten_ok {
146 let _ = Panicking.map(|x| Ok::<_, ()>([x])).flatten_ok();
147 }
148 merge {
149 let _ = Panicking.merge(Panicking);
150 }
151 merge_by {
152 let _ = Panicking.merge_by(Panicking, |_, _| true);
153 }
154 merge_join_by {
155 let _ = Panicking.merge_join_by(Panicking, |_, _| true);
156 let _ = Panicking.merge_join_by(Panicking, Ord::cmp);
157 }
158 #[should_panic]
159 kmerge {
160 let _ = Panicking.map(|_| Panicking).kmerge();
161 }
162 #[should_panic]
163 kmerge_by {
164 let _ = Panicking.map(|_| Panicking).kmerge_by(|_, _| true);
165 }
166 cartesian_product {
167 let _ = Panicking.cartesian_product(Panicking);
168 }
169 multi_cartesian_product {
170 let _ = vec![Panicking, Panicking, Panicking].into_iter().multi_cartesian_product();
171 }
172 coalesce {
173 let _ = Panicking.coalesce(|x, y| if x == y { Ok(x) } else { Err((x, y)) });
174 }
175 dedup {
176 let _ = Panicking.dedup();
177 }
178 dedup_by {
179 let _ = Panicking.dedup_by(|_, _| true);
180 }
181 dedup_with_count {
182 let _ = Panicking.dedup_with_count();
183 }
184 dedup_by_with_count {
185 let _ = Panicking.dedup_by_with_count(|_, _| true);
186 }
187 duplicates {
188 let _ = Panicking.duplicates();
189 }
190 duplicates_by {
191 let _ = Panicking.duplicates_by(|x| *x);
192 }
193 unique {
194 let _ = Panicking.unique();
195 }
196 unique_by {
197 let _ = Panicking.unique_by(|x| *x);
198 }
199 peeking_take_while {
200 let _ = Panicking.peekable().peeking_take_while(|x| x % 2 == 0);
201 }
202 take_while_ref {
203 let _ = Panicking.take_while_ref(|x| x % 2 == 0);
204 }
205 take_while_inclusive {
206 let _ = Panicking.take_while_inclusive(|x| x % 2 == 0);
207 }
208 while_some {
209 let _ = Panicking.map(Some).while_some();
210 }
211 #[allow(deprecated)]
212 tuple_combinations1 {
213 let _ = Panicking.tuple_combinations::<(_,)>();
214 }
215 #[allow(deprecated)]
216 #[should_panic]
217 tuple_combinations2 {
218 let _ = Panicking.tuple_combinations::<(_, _)>();
219 }
220 #[allow(deprecated)]
221 #[should_panic]
222 tuple_combinations3 {
223 let _ = Panicking.tuple_combinations::<(_, _, _)>();
224 }
225 array_combinations1 {
226 let _ = Panicking.array_combinations::<1>();
227 }
228 array_combinations2 {
229 let _ = Panicking.array_combinations::<2>();
230 }
231 array_combinations3 {
232 let _ = Panicking.array_combinations::<3>();
233 }
234 combinations {
235 let _ = Panicking.combinations(0);
236 let _ = Panicking.combinations(1);
237 let _ = Panicking.combinations(2);
238 }
239 combinations_with_replacement {
240 let _ = Panicking.combinations_with_replacement(0);
241 let _ = Panicking.combinations_with_replacement(1);
242 let _ = Panicking.combinations_with_replacement(2);
243 }
244 array_combinations_with_replacement {
245 let _ = Panicking.array_combinations_with_replacement::<0>();
246 let _ = Panicking.array_combinations_with_replacement::<1>();
247 let _ = Panicking.array_combinations_with_replacement::<2>();
248 }
249 permutations {
250 let _ = Panicking.permutations(0);
251 let _ = Panicking.permutations(1);
252 let _ = Panicking.permutations(2);
253 }
254 powerset {
255 let _ = Panicking.powerset();
256 }
257 pad_using {
258 let _ = Panicking.pad_using(25, |_| 10);
259 }
260 with_position {
261 let _ = Panicking.with_position();
262 }
263 positions {
264 let _ = Panicking.positions(|v| v % 2 == 0);
265 }
266 update {
267 let _ = Panicking.update(|n| *n += 1);
268 }
269 multipeek {
270 let _ = Panicking.multipeek();
271 }
272 // Not iterator themselves but still lazy.
273 into_grouping_map {
274 let _ = Panicking.map(|x| (x, x + 1)).into_grouping_map();
275 }
276 into_grouping_map_by {
277 let _ = Panicking.into_grouping_map_by(|x| *x);
278 }
279 // Macros:
280 iproduct {
281 let _ = itertools::iproduct!(Panicking);
282 let _ = itertools::iproduct!(Panicking, Panicking);
283 let _ = itertools::iproduct!(Panicking, Panicking, Panicking);
284 }
285 izip {
286 let _ = itertools::izip!(Panicking);
287 let _ = itertools::izip!(Panicking, Panicking);
288 let _ = itertools::izip!(Panicking, Panicking, Panicking);
289 }
290 chain {
291 let _ = itertools::chain!(Panicking);
292 let _ = itertools::chain!(Panicking, Panicking);
293 let _ = itertools::chain!(Panicking, Panicking, Panicking);
294 }
295 // Free functions:
296 multizip {
297 let _ = itertools::multizip((Panicking, Panicking));
298 }
299 put_back {
300 let _ = itertools::put_back(Panicking);
301 let _ = itertools::put_back(Panicking).with_value(15);
302 }
303 peek_nth {
304 let _ = itertools::peek_nth(Panicking);
305 }
306 put_back_n {
307 let _ = itertools::put_back_n(Panicking);
308 }
309 rciter {
310 let _ = itertools::rciter(Panicking);
311 }
312 }