array_init_macro/
lib.rs

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
#![feature(maybe_uninit_uninit_array)]

#[macro_export]
macro_rules! arr {
    ($type:ty; $count:expr; $init:expr) => {
        unsafe {
            let mut array: [std::mem::MaybeUninit<$type>; $count] =
                std::mem::MaybeUninit::uninit_array();
            for element in &mut array[..] {
                *element = std::mem::MaybeUninit::new($init);
            }
            std::mem::transmute::<_, [$type; $count]>(array)
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple_numbers() {
        let array0 = arr![u8; 4; 1];
        assert_eq!(array0.len(), 4);
        assert_eq!(array0[0], 1);
        assert_eq!(array0[1], 1);
        assert_eq!(array0[2], 1);
        assert_eq!(array0[3], 1);

        const COUNT: usize = 3;
        let array1 = arr![u8; COUNT; 5];
        assert_eq!(array1.len(), COUNT);
    }
}