Skip to content

Commit c00e5a1

Browse files
committed
Have types! include a from_array constructor
1 parent c75d8da commit c00e5a1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

crates/core_arch/src/macros.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,28 @@ macro_rules! types {
103103
unsafe { simd_shuffle!(one, one, [0; $len]) }
104104
}
105105

106+
/// Constructs a vector from an array of the same elements and length
107+
///
108+
/// For now you don't *have* to use this to construct one of these
109+
/// (depending on the visibility you put on the field) but it's encouraged
110+
/// in case direct construction also gets banned.
111+
#[inline]
112+
$v const fn from_array(array: [$elem_type; $len]) -> Self {
113+
// Projecting into SIMD is banned, but this is technically an
114+
// `Rvalue::Aggregate`, which is not a projection.
115+
$name { do_not_field_project: array }
116+
}
117+
106118
/// Returns an array reference containing the entire SIMD vector.
119+
#[inline]
107120
$v const fn as_array(&self) -> &[$elem_type; $len] {
108121
// SAFETY: this type is just an overaligned `[T; N]` with
109122
// potential padding at the end, so pointer casting to a
110123
// `&[T; N]` is safe.
111124
//
112-
// NOTE: This deliberately doesn't just use `&self.0` because it may soon be banned
125+
// NOTE: This deliberately doesn't just use `&self.0` it's banned
113126
// see https://github.com/rust-lang/compiler-team/issues/838
114127
unsafe { &*(self as *const Self as *const [$elem_type; $len]) }
115-
116128
}
117129

118130
/// Returns a mutable array reference containing the entire SIMD vector.

0 commit comments

Comments
 (0)