konst/iter/iterator_dsl.rs
1/*!
2Documentation on the iterator DSL that some `konst::iter` macros support.
3
4The macros from this crate don't directly invoke any of the method listed below,
5they expand to equivalent code, this allows the macros to work on stable.
6
7The `konst::iter::collect_const` macro is used in examples here purely for simplicity.
8
9# Methods
10
11Every iterator method below behaves the same as in Iterator,
12unless specified otherwise.
13
14The methods listed alphabetically:
15- [`copied`](#copied)
16- [`enumerate`](#enumerate)
17- [`filter_map`](#filter_map)
18- [`filter`](#filter)
19- [`flat_map`](#flat_map)
20- [`flatten`](#flatten)
21- [`map`](#map)
22- [`rev`](#rev)
23- [`skip_while`](#skip_while)
24- [`skip`](#skip)
25- [`take_while`](#take_while)
26- [`take`](#take)
27- [`zip`](#zip)
28
29
30### `zip`
31
32Limitation: the iterator DSL can't passed as an argument to this method.
33
34```rust
35use konst::iter;
36
37const ARR: [(&u8, usize); 4] =
38 iter::collect_const!((&u8, usize) => &[3u8, 5, 8, 13],zip(100..));
39
40assert_eq!(ARR, [(&3, 100), (&5, 101), (&8, 102), (&13, 103)]);
41```
42
43### `enumerate`
44
45`enumerate` always counts from `0`, regardless of whether the iterator is reversed.
46
47```rust
48use konst::iter;
49
50const ARR: [(usize, &u8); 4] =
51 iter::collect_const!((usize, &u8) => &[3u8, 5, 8, 13],enumerate());
52
53assert_eq!(ARR, [(0, &3), (1, &5), (2, &8), (3, &13)]);
54```
55
56### `filter`
57
58```rust
59use konst::iter;
60
61const ARR: [&u8; 4] = iter::collect_const!(&u8 =>
62 &[3u8, 5, 8, 13, 21],
63 filter(|e| !e.is_power_of_two()),
64);
65
66assert_eq!(ARR, [&3, &5, &13, &21]);
67```
68
69### `map`
70
71```rust
72use konst::iter;
73
74const ARR: [usize; 4] = iter::collect_const!(usize => (1..=4),map(|e| e * 3));
75
76assert_eq!(ARR, [3, 6, 9, 12]);
77```
78
79### `filter_map`
80
81```rust
82use konst::iter;
83
84use std::num::NonZeroU8;
85
86const ARR: [NonZeroU8; 4] = iter::collect_const!(NonZeroU8 =>
87 &[3, 0, 1, 5, 6],
88 filter_map(|x| NonZeroU8::new(*x)),
89);
90
91assert_eq!(ARR, [3, 1, 5, 6].map(|n| NonZeroU8::new(n).unwrap()));
92```
93
94### `flat_map`
95
96Limitation: the iterator DSL can't passed as an argument to this method.
97
98```rust
99use konst::iter;
100
101const ARR: [usize; 9] = iter::collect_const!(usize =>
102 &[3, 5, 8],
103 flat_map(|x| {
104 let x10 = *x * 10;
105 x10..x10 + 3
106 }),
107);
108
109assert_eq!(ARR, [30, 31, 32, 50, 51, 52, 80, 81, 82]);
110```
111
112### `flatten`
113
114```rust
115use konst::iter;
116
117const ARR: [&u8; 4] =
118 iter::collect_const!(&u8 => &[&[3, 5], &[8, 13]], flatten());
119
120assert_eq!(ARR, [&3, &5, &8, &13]);
121```
122
123### `copied`
124
125```rust
126use konst::iter;
127
128const ARR: [u8; 3] = iter::collect_const!(u8 =>
129 &[2, 3, 4, 5, 6],
130 copied(),
131 filter(|n| *n % 2 == 0)
132);
133
134assert_eq!(ARR, [2, 4, 6]);
135```
136
137### `rev`
138
139Limitation: iterator-reversing methods can't be called more than once in
140the same macro invocation.
141
142```rust
143use konst::iter;
144
145const ARR: [&u8; 3] = iter::collect_const!(&u8 => &[2, 3, 5],rev());
146
147assert_eq!(ARR, [&5, &3, &2]);
148```
149
150### `take`
151
152```rust
153use konst::iter;
154
155const ARR: [usize; 3] = iter::collect_const!(usize => 10..,take(3));
156
157assert_eq!(ARR, [10, 11, 12]);
158```
159
160### `take_while`
161
162```rust
163use konst::iter;
164
165const ARR: [&u8; 4] = iter::collect_const!(&u8 =>
166 &[3, 5, 8, 13, 21, 34, 55],take_while(|elem| **elem < 20 )
167);
168
169assert_eq!(ARR, [&3, &5, &8, &13]);
170```
171
172### `skip`
173
174```rust
175use konst::iter;
176
177const ARR: [usize; 3] = iter::collect_const!(usize => 10..=15,skip(3));
178
179assert_eq!(ARR, [13, 14, 15]);
180```
181
182### `skip_while`
183
184```rust
185use konst::iter;
186
187const ARR: [&u8; 3] = iter::collect_const!(&u8 =>
188 &[3, 5, 8, 13, 21, 34, 55],skip_while(|elem| **elem < 20 )
189);
190
191assert_eq!(ARR, [&21, &34, &55]);
192```
193
194
195
196
197
198*/