konst_macro_rules/
collect_const.rs

1use crate::type_eq::TypeEq;
2
3pub enum CollectorCmd<T, Ret, const CAP: usize> {
4    ComputeLength(TypeEq<ComputedLength, Ret>),
5    BuildArray(TypeEq<[T; CAP], Ret>),
6}
7
8impl<T> CollectorCmd<T, ComputedLength, 0> {
9    pub const COMPUTE_LENGTH: Self = Self::ComputeLength(TypeEq::NEW);
10}
11
12impl<T, const CAP: usize> CollectorCmd<T, [T; CAP], CAP> {
13    pub const BUILD_ARRAY: Self = Self::BuildArray(TypeEq::NEW);
14}
15
16pub struct ComputedLength {
17    pub length: usize,
18}
19
20#[macro_export]
21macro_rules! iter_collect_const {
22    ($Item:ty => $($rem:tt)*) => {{
23        const fn __func_zxe7hgbnjs<Ret_KO9Y329U2U, const CAP_KO9Y329U2U: usize>(
24            cmd: $crate::__::CollectorCmd<$Item, Ret_KO9Y329U2U, CAP_KO9Y329U2U>,
25        ) -> Ret_KO9Y329U2U {
26            let mut array = $crate::utils_1_56::uninit_array::<_, CAP_KO9Y329U2U>();
27            let mut length = 0usize;
28
29            $crate::__process_iter_args!{
30                ($crate::__iter_collect_const)
31                ($Item, (cmd array length),)
32                (
33                    item,
34                    'zxe7hgbnjs,
35                    adapter,
36                )
37                $($rem)*
38            }
39
40            match cmd {
41                $crate::__::CollectorCmd::ComputeLength(teq) => {
42                    teq.to_right($crate::__::ComputedLength { length })
43                }
44                $crate::__::CollectorCmd::BuildArray(teq) => {
45                    if length == CAP_KO9Y329U2U {
46                        // SAFETY: The above condition ensures that
47                        // all of the array is initialized
48                        let array = unsafe{ $crate::utils_1_56::array_assume_init(array) };
49                        teq.to_right(array)
50                    } else {
51                        let _: () = [/*initialization was skipped somehow*/][length];
52                        loop{}
53                    }
54                }
55            }
56        }
57
58        const __COUNT81608BFNA5: $crate::__::usize =
59            __func_zxe7hgbnjs($crate::__::CollectorCmd::COMPUTE_LENGTH).length;
60
61        const __ARR81608BFNA5: [$Item; __COUNT81608BFNA5] =
62            __func_zxe7hgbnjs($crate::__::CollectorCmd::BUILD_ARRAY);
63
64        __ARR81608BFNA5
65    }};
66}
67
68#[doc(hidden)]
69#[macro_export]
70macro_rules! __iter_collect_const {
71    (
72        @each
73        $Item:ty,
74        ($cmd:ident $array:ident $length:ident),
75        ($item:ident adapter),
76        $(,)*
77    ) => {
78        if let $crate::__::CollectorCmd::BuildArray(teq) = $cmd {
79            teq.reachability_hint();
80
81            $array[$length] = $crate::__::MaybeUninit::new($item);
82        }
83
84        $length += 1;
85    };
86    (@end $($tt:tt)*) => {};
87}