1 extern crate itertools; 2 3 use itertools::Itertools; 4 5 struct PanickingCounter { 6 curr: usize, 7 max: usize, 8 } 9 10 impl Iterator for PanickingCounter { 11 type Item = (); 12 next(&mut self) -> Option<Self::Item>13 fn next(&mut self) -> Option<Self::Item> { 14 self.curr += 1; 15 16 if self.curr == self.max { 17 panic!( 18 "Input iterator reached maximum of {} suggesting collection by adaptor", 19 self.max 20 ); 21 } 22 23 Some(()) 24 } 25 } 26 no_collect_test<A, T>(to_adaptor: T) where A: Iterator, T: Fn(PanickingCounter) -> A27fn no_collect_test<A, T>(to_adaptor: T) 28 where A: Iterator, T: Fn(PanickingCounter) -> A 29 { 30 let counter = PanickingCounter { curr: 0, max: 10_000 }; 31 let adaptor = to_adaptor(counter); 32 33 for _ in adaptor.take(5) {} 34 } 35 36 #[test] permutations_no_collect()37fn permutations_no_collect() { 38 no_collect_test(|iter| iter.permutations(5)) 39 } 40 41 #[test] combinations_no_collect()42fn combinations_no_collect() { 43 no_collect_test(|iter| iter.combinations(5)) 44 } 45 46 #[test] combinations_with_replacement_no_collect()47fn combinations_with_replacement_no_collect() { 48 no_collect_test(|iter| iter.combinations_with_replacement(5)) 49 }