]>
git.proxmox.com Git - rustc.git/blob - vendor/packed_simd_2/src/api/from/from_array.rs
1 //! Implements `From<[T; N]>` and `Into<[T; N]>` for vector types.
3 macro_rules
! impl_from_array
{
4 ([$elem_ty
:ident
; $elem_count
:expr
]: $id
:ident
| $test_tt
:tt
5 | ($non_default_array
:expr
, $non_default_vec
:expr
)) => {
6 impl From
<[$elem_ty
; $elem_count
]> for $id
{
8 fn from(array
: [$elem_ty
; $elem_count
]) -> Self {
10 array
: [$elem_ty
; $elem_count
],
13 unsafe { U { array }
.vec
}
17 impl From
<$id
> for [$elem_ty
; $elem_count
] {
19 fn from(vec
: $id
) -> Self {
21 array
: [$elem_ty
; $elem_count
],
24 unsafe { U { vec }
.array
}
28 // FIXME: `Into::into` is not inline, but due to
29 // the blanket impl in `std`, which is not
30 // marked `default`, we cannot override it here with
33 impl Into<[$elem_ty; $elem_count]> for $id {
35 fn into(self) -> [$elem_ty; $elem_count] {
37 array: [$elem_ty; $elem_count],
40 unsafe { U { vec: self }.array }
44 impl Into<$id> for [$elem_ty; $elem_count] {
46 fn into(self) -> $id {
48 array: [$elem_ty; $elem_count],
51 unsafe { U { array: self }.vec }
59 // Comparisons use integer casts within mantissa^1 range.
60 #[allow(clippy::float_cmp)]
65 let vec
: $id
= Default
::default();
67 // FIXME: Workaround for arrays with more than 32
70 // Safe because we never take a reference to any
71 // uninitialized element.
73 array
: [$elem_ty
; $elem_count
],
76 let mut array
= W { other: () }
;
77 for i
in 0..$elem_count
{
78 let default: $elem_ty
= Default
::default();
79 // note: array.other is the active member and
80 // initialized so we can take a reference to it:
82 &mut array
.other
as *mut () as *mut $elem_ty
84 // note: default is a valid bit-pattern for
87 crate::ptr
::write(p
.wrapping_add(i
), default)
90 // note: the array variant of the union is properly
92 let mut array
= unsafe {
96 array
[0] = $non_default_array
;
97 let vec
= vec
.replace(0, $non_default_vec
);
99 let vec_from_array
= $id
::from(array
);
100 assert_eq
!(vec_from_array
, vec
);
102 = <[$elem_ty
; $elem_count
]>::from(vec
);
103 // FIXME: Workaround for arrays with more than 32
105 for i
in 0..$elem_count
{
106 assert_eq
!(array_from_vec
[i
], array
[i
]);
109 let vec_from_into_array
: $id
= array
.into();
110 assert_eq
!(vec_from_into_array
, vec
);
111 let array_from_into_vec
: [$elem_ty
; $elem_count
]
113 // FIXME: Workaround for arrays with more than 32
115 for i
in 0..$elem_count
{
116 assert_eq
!(array_from_into_vec
[i
], array
[i
]);