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
//! Implements `From<[T; N]>` and `Into<[T; N]>` for vector types.

macro_rules! impl_from_array {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt
     | ($non_default_array:expr, $non_default_vec:expr)) => {
        impl From<[$elem_ty; $elem_count]> for $id {
            #[inline]
            fn from(array: [$elem_ty; $elem_count]) -> Self {
                union U {
                    array: [$elem_ty; $elem_count],
                    vec: $id,
                }
                unsafe { U { array }.vec }
            }
        }

        impl From<$id> for [$elem_ty; $elem_count] {
            #[inline]
            fn from(vec: $id) -> Self {
                union U {
                    array: [$elem_ty; $elem_count],
                    vec: $id,
                }
                unsafe { U { vec }.array }
            }
        }

        // FIXME: `Into::into` is not inline, but due to
        // the blanket impl in `std`, which is not
        // marked `default`, we cannot override it here with
        // specialization.
        /*
        impl Into<[$elem_ty; $elem_count]> for $id {
            #[inline]
            fn into(self) -> [$elem_ty; $elem_count] {
                union U {
                    array: [$elem_ty; $elem_count],
                    vec: $id,
                }
                unsafe { U { vec: self }.array }
            }
        }

        impl Into<$id> for [$elem_ty; $elem_count] {
            #[inline]
            fn into(self) -> $id {
                union U {
                    array: [$elem_ty; $elem_count],
                    vec: $id,
                }
                unsafe { U { array: self }.vec }
            }
        }
        */

        test_if! {
            $test_tt:
            paste::item! {
                // Comparisons use integer casts within mantissa^1 range.
                #[allow(clippy::float_cmp)]
                mod [<$id _from>] {
                    use super::*;
                    #[test]
                    fn array() {
                        let vec: $id = Default::default();

                        // FIXME: Workaround for arrays with more than 32
                        // elements.
                        //
                        // Safe because we never take a reference to any
                        // uninitialized element.
                        union W {
                            array: [$elem_ty; $elem_count],
                            other: ()
                        }
                        let mut array = W { other: () };
                        for i in 0..$elem_count {
                            let default: $elem_ty = Default::default();
                            // note: array.other is the active member and
                            // initialized so we can take a reference to it:
                            let p = unsafe {
                                &mut array.other as *mut () as *mut $elem_ty
                            };
                            // note: default is a valid bit-pattern for
                            // $elem_ty:
                            unsafe {
                                crate::ptr::write(p.wrapping_add(i), default)
                            };
                        }
                        // note: the array variant of the union is properly
                        // initialized:
                        let mut array = unsafe {
                            array.array
                        };

                        array[0] = $non_default_array;
                        let vec = vec.replace(0, $non_default_vec);

                        let vec_from_array = $id::from(array);
                        assert_eq!(vec_from_array, vec);
                        let array_from_vec
                            = <[$elem_ty; $elem_count]>::from(vec);
                        // FIXME: Workaround for arrays with more than 32
                        // elements.
                        for i in 0..$elem_count {
                            assert_eq!(array_from_vec[i], array[i]);
                        }

                        let vec_from_into_array: $id = array.into();
                        assert_eq!(vec_from_into_array, vec);
                        let array_from_into_vec: [$elem_ty; $elem_count]
                            = vec.into();
                        // FIXME: Workaround for arrays with more than 32
                        // elements.
                        for i in 0..$elem_count {
                            assert_eq!(array_from_into_vec[i], array[i]);
                        }
                    }
                }
            }
        }
    };
}