tinc/private/
const_macros.rs

1#[inline]
2pub const fn len_sum(to_concat: &'static [&'static [&'static str]]) -> usize {
3    let mut len = 0;
4    let mut i = 0;
5    while i < to_concat.len() {
6        len += to_concat[i].len();
7        i += 1;
8    }
9    len
10}
11
12#[inline]
13pub const fn concat_array<const LEN: usize>(to_concat: &'static [&'static [&'static str]]) -> [&'static str; LEN] {
14    let mut res: [&'static str; LEN] = [""; LEN];
15    let mut shift = 0;
16    let mut i = 0;
17    while i < to_concat.len() {
18        let to_concat_one = to_concat[i];
19        let mut j = 0;
20        while j < to_concat_one.len() {
21            res[j + shift] = to_concat_one[j];
22            j += 1;
23        }
24        shift += j;
25        i += 1;
26    }
27    res
28}
29
30#[macro_export]
31#[doc(hidden)]
32macro_rules! __private_const_concat_str_array {
33    ($($rest:expr),*) => {{
34        const TO_CONCAT: &[&[&str]] = &[$($rest),*];
35        const LEN: usize = $crate::__private::const_macros::len_sum(TO_CONCAT);
36        &$crate::__private::const_macros::concat_array::<LEN>(TO_CONCAT)
37    }};
38    ($($rest:expr),*,) => {
39        $crate::__private_const_concat_str_array!($($rest),*)
40    };
41}
42
43#[cfg(test)]
44#[cfg_attr(coverage_nightly, coverage(off))]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_len_sum() {
50        const TO_CONCAT: &[&[&str]] = &[&["a", "b"], &["c"]];
51        assert_eq!(len_sum(TO_CONCAT), 3);
52    }
53
54    #[test]
55    fn test_concat_array() {
56        const TO_CONCAT: &[&[&str]] = &[&["a", "b"], &["c"]];
57        const LEN: usize = len_sum(TO_CONCAT);
58        let result = concat_array::<LEN>(TO_CONCAT);
59        assert_eq!(result, ["a", "b", "c"]);
60    }
61}