1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5/// Given `Self` (`$aligned`), `Self::ULE` (`$unaligned`), and a conversion function (`$single` or
6/// `Self::from_aligned`), implement `from_array` for arrays of `$aligned` to `$unaligned`.
7///
8/// The `$default` argument is due to current compiler limitations.
9/// Pass any (cheap to construct) value.
10#[macro_export]
11macro_rules! impl_ule_from_array {
12 ($aligned:ty, $unaligned:ty, $default:expr, $single:path) => {
13 #[doc = concat!("Convert an array of `", stringify!($aligned), "` to an array of `", stringify!($unaligned), "`.")]
14 pub const fn from_array<const N: usize>(arr: [$aligned; N]) -> [Self; N] {
15 let mut result = [$default; N];
16 let mut i = 0;
17 // Won't panic because i < N and arr has length N
18 #[allow(clippy::indexing_slicing)]
19 while i < N {
20 result[i] = $single(arr[i]);
21 i += 1;
22 }
23 result
24 }
25 };
26 ($aligned:ty, $unaligned:ty, $default:expr) => {
27 impl_ule_from_array!($aligned, $unaligned, $default, Self::from_aligned);
28 };
29}
30