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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Const equivalents of array functions.
pub use ArrayBuilder;
pub use ;
use drop_warning;
/// Const equivalent of
/// [`array::map`](https://doc.rust-lang.org/std/primitive.array.html#method.map)
///
/// Consider using [`konst::array::map_nd`](crate::array::map_nd)
/// if you're mapping from and into an
/// array of non-drop types in const and you need early returns.
///
///
///
/// # Example
///
/// ```rust
/// assert_eq!(PAIRS, [(3, "hello"), (5, "world"), (8, "foo")]);
///
/// const PAIRS: [(u8, &str); 3] =
/// swap_pairs([("hello", 3), ("world", 5), ("foo", 8)]);
///
/// const fn swap_pairs<T, U, const N: usize>(pairs: [(T, U); N]) -> [(U, T); N] {
/// konst::array::map!(pairs, |pair: (T, U)| {
/// // need to use `destructure` to destructure types that may contain Drop fields
/// konst::destructure!{(a, b) = pair}
/// (b, a)
/// })
/// }
/// ```
///
pub use crate__array_map_by_val as map;
/// Const equivalent of [`array::from_fn`](core::array::from_fn).
///
/// Consider using [`konst::array::from_fn_nd`](crate::array::from_fn_nd)
/// if you're creating an array of non-drop types in const and you need early returns.
///
///
///
/// # Example
///
/// ```rust
/// use konst::array;
///
/// {
/// const POWERS: [u64; 5] = array::from_fn!(|i| 2u64.pow(i as u32));
///
/// assert_eq!(POWERS, [1, 2, 4, 8, 16]);
/// }
///
/// // Annotating the array type
/// assert_eq!(
/// array::from_fn!([&str; 6] => |i| konst::string::str_up_to("hello", i)),
/// ["", "h", "he", "hel", "hell", "hello"],
/// );
/// ```
///
pub use crate__array_from_fn2 as from_fn;
/// Const equivalent of
/// [`array::map`](https://doc.rust-lang.org/std/primitive.array.html#method.map),
/// which allows `return`ing from the closure in const.
///
/// Consider using [`konst::array::map`](crate::array::map)
/// if the closure does not contain any early returns.
///
/// # Note
///
/// This macro requires the input and output elements to not need dropping.
/// In exchange, there can be early returns inside the closure passed to this macro,
/// which will return from the function that this macro is invoked inside of.
///
///
/// # Example
///
/// ```rust
/// use konst::{array, result};
///
/// use std::num::ParseIntError;
///
/// const OK: [u8; 4] = result::unwrap!(parse_u8s(["3", "5", "8", "13"]));
/// assert_eq!(OK, [3, 5, 8, 13]);
///
/// const ERR: ParseIntError = result::unwrap_err!(parse_u8s(["3", "AAA", "5"]));
///
/// const fn parse_u8s<const N: usize>(strs: [&str; N]) -> Result<[u8; N], ParseIntError> {
/// // `try_` returns from `parse_u8s` on `Err`, not from closure scope!
/// Ok(array::map_nd!(strs, |s| konst::try_!(u8::from_str_radix(s, 10))))
/// }
/// ```
///
pub use crate__array_map_by_val_nd as map_nd;
/// Const equivalent of [`array::from_fn`](core::array::from_fn),
/// which allows `return`ing from the closure in const.
///
/// Consider using [`konst::array::from_fn`](crate::array::from_fn)
/// if the closure does not contain any early returns.
///
/// # Note
///
/// This macro requires the output elements to not need dropping.
/// In exchange, there can be early returns inside the closure passed to this macro,
/// which will return from the function that this macro is invoked inside of.
///
///
/// # Example
///
/// ### No array type annotation
///
/// ```rust
/// use konst::{array, string};
///
/// const NON3: Option<[&str; 4]> = split_comma("3, 5, 8");
/// assert_eq!(NON3, None);
///
/// const SOM: [&str; 4] = split_comma("3, 5, 8, 13").unwrap();
/// assert_eq!(SOM, ["3", "5", "8", "13"]);
///
/// // drops the excess comma-separated items
/// const SOM5: [&str; 4] = split_comma("3, 5, 8, 13, 21").unwrap();
/// assert_eq!(SOM5, ["3", "5", "8", "13"]);
///
/// const fn split_comma<const N: usize>(string: &str) -> Option<[&str; N]> {
/// let mut iter = string::split(string, ",");
///
/// // `try_opt` returns from `split_comma` on `None`, not from closure scope!
/// Some(array::from_fn_nd!(|_| konst::try_opt!(iter.next()).trim_ascii()))
/// }
/// ```
///
/// ### Array type annotation
///
/// ```rust
/// use konst::{array, string};
///
/// // Annotating the array type
/// let arr = array::from_fn_nd!([u8; 4] => |x| x.pow(2) as _);
/// assert_eq!(arr, [0, 1, 4, 9])
/// ```
///
pub use crate__array_from_fn_nd as from_fn_nd;