1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
});
}
/// Iterates over all elements of an [iterator](crate::iter::ConstIntoIter),
/// const equivalent of [`Iterator::for_each`]
///
/// This macro supports emulating iterator methods by expanding to equivalent code.
/// They are documented in the [`iterator_dsl`] module,
/// because they are also supported by other `konst::iter` macros.
///
/// # Drop behavior
///
/// The behavior regarding dropping iterators is
/// [documented here](crate::iter::ConstIntoIter#dropping).
///
/// # Examples
///
/// ### Custom iterator
///
/// ```rust
/// use konst::iter::{ConstIntoIter, IsIteratorKind};
///
/// struct Upto10(u8);
///
/// impl ConstIntoIter for Upto10 {
/// type Kind = IsIteratorKind;
/// type IntoIter = Self;
/// type Item = u8;
/// const ITEMS_NEED_DROP: bool = false;
/// }
///
/// impl Upto10 {
/// const fn next(&mut self) -> Option<u8> {
/// if self.0 < 10 {
/// let ret = self.0;
/// self.0 += 1;
/// Some(ret)
/// } else {
/// None
/// }
/// }
/// }
///
/// const N: u32 = {
/// let mut n = 0u32;
/// konst::iter::for_each!{elem in Upto10(7) =>
/// n = n * 10 + elem as u32;
/// }
/// n
/// };
///
/// assert_eq!(N, 789);
///
/// ```
///
/// ### Summing pairs
///
// TODO: remove the ignore once array has by-value iteration
/// ```rust, ignore
/// use konst::iter::for_each;
///
/// const fn add_pairs<const N: usize>(l: [u32; N], r: [u32; N]) -> [u32; N] {
/// let mut out = [0u32; N];
///
/// for_each!{(i, val) in l,zip(r),map(|(l, r)| l + r),enumerate() =>
/// out[i] = val;
/// }
///
/// out
/// }
///
/// assert_eq!(add_pairs([], []), []);
/// assert_eq!(add_pairs([3], [5]), [8]);
/// assert_eq!(add_pairs([3, 5], [8, 13]), [11, 18]);
///
/// ```
///
/// [`iterator_dsl`]: crate::iter::iterator_dsl
pub use __for_each_hidden as for_each;