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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// For loop over a range
///
/// # Example
///
/// ```rust
/// use konst::for_range;
///
/// const LEN: usize = 10;
/// const ARR: [u32; LEN] = {
/// let mut ret = [1; LEN];
/// for_range!{i in 2..LEN =>
/// ret[i] = ret[i - 1] + ret[i - 2];
/// }
/// ret
/// };
///
/// assert_eq!(ARR, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]);
/// ```
///
/// Emulates by-value destructuring of a [`Some`] variant that contains a Drop type in const.
///
/// # Motivation
///
/// This macro works around the fact that this code
///
/// ```rust,compile_fail
/// const fn foo<T>(opt: Option<T>) -> Result<T, ()> {
/// match opt {
/// Some(x) => Ok(x),
/// None => Err(())
/// }
/// }
/// ```
/// causes this error as of Rust 1.89:
/// ```text
/// error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
/// --> konst/src/macros/control_flow.rs:42:17
/// |
/// 3 | const fn foo<T>(opt: Option<T>) -> Result<T, ()> {
/// | ^^^ the destructor for this type cannot be evaluated in constant functions
/// ...
/// 8 | }
/// | - value is dropped here
/// ```
///
/// # Example
///
/// ```rust
/// assert_eq!(ok_or_none_error(Some(10)), Ok(10));
/// assert_eq!(ok_or_none_error(None::<String>), Err(ItWasNoneError));
///
///
/// const fn ok_or_none_error<T>(opt: Option<T>) -> Result<T, ItWasNoneError> {
/// konst::if_let_Some!{x = opt => {
/// Ok(x)
/// } else {
/// Err(ItWasNoneError)
/// }}
/// }
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct ItWasNoneError;
/// ```
///
/// Emulates a by-value `while let Some` loop over
/// `Drop`-type-containing `Option`s in const.
///
/// # Motivation
///
/// This macro works around the fact that this code
///
/// ```rust,compile_fail
/// use konst::array::ArrayBuilder;
///
/// const fn foo<T: SomeTrait>() -> [T; 3] {
/// let mut builder = ArrayBuilder::of_drop();
/// while let Some(x) = produce_option(&builder) {
/// builder.push(x);
/// }
/// builder.build()
/// }
///
/// # trait SomeTrait {}
/// # const fn produce_option<T: SomeTrait, U>(_: &U) -> Option<T> {
/// # None
/// # }
/// ```
/// causes this error as of Rust 1.89:
/// ```text
/// error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
/// --> konst/src/macros/control_flow.rs:108:25
/// |
/// 9 | while let Some(x) = produce_option(&builder) {
/// | ^^^^^^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions
/// 10 | builder.push(x);
/// 11 | }
/// | - value is dropped here
/// ```
///
/// # Example
///
/// This example requires the `"iter"` feature (enabled by default),
/// because it uses [`ArrayBuilder`](crate::array::ArrayBuilder).
///
/// use konst::array::ArrayBuilder;
/// use konst::drop_flavor::MayDrop;
///
/// assert_eq!(make_strings::<1>(), [String::new()]);
/// assert_eq!(make_strings::<2>(), [String::new(), String::new()]);
/// assert_eq!(make_strings::<3>(), [String::new(), String::new(), String::new()]);
///
/// const fn make_strings<const N: usize>() -> [String; N] {
/// let mut builder = ArrayBuilder::of_drop();
/// konst::while_let_Some!{x = produce_option(&builder) =>
/// builder.push(x);
/// }
/// builder.build()
/// }
///
/// const fn produce_option<const N: usize>(
/// ab: &ArrayBuilder<String, N, MayDrop>
/// ) -> Option<String> {
/// if ab.is_full() {
/// None
/// } else {
/// Some(String::new())
/// }
/// }
/// ```
///