@@ -135,22 +135,32 @@ where
135
135
/// assert_eq!(v.as_array(), &[0, 1, 2, 3]);
136
136
/// ```
137
137
pub const fn as_array ( & self ) -> & [ T ; LANES ] {
138
- & self . 0
138
+ // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
139
+ // is always valid and `Simd<T, LANES>` never has a lower alignment
140
+ // than `[T; LANES]`.
141
+ unsafe { & * ( self as * const Self as * const [ T ; LANES ] ) }
139
142
}
140
143
141
144
/// Returns a mutable array reference containing the entire SIMD vector.
142
145
pub fn as_mut_array ( & mut self ) -> & mut [ T ; LANES ] {
143
- & mut self . 0
146
+ // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
147
+ // is always valid and `Simd<T, LANES>` never has a lower alignment
148
+ // than `[T; LANES]`.
149
+ unsafe { & mut * ( self as * mut Self as * mut [ T ; LANES ] ) }
144
150
}
145
151
146
152
/// Converts an array to a SIMD vector.
147
153
pub const fn from_array ( array : [ T ; LANES ] ) -> Self {
148
- Self ( array)
154
+ // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
155
+ // is always valid.
156
+ unsafe { core:: mem:: transmute_copy ( & array) }
149
157
}
150
158
151
159
/// Converts a SIMD vector to an array.
152
160
pub const fn to_array ( self ) -> [ T ; LANES ] {
153
- self . 0
161
+ // SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
162
+ // is always valid.
163
+ unsafe { core:: mem:: transmute_copy ( & self ) }
154
164
}
155
165
156
166
/// Converts a slice to a SIMD vector containing `slice[..LANES]`.
@@ -735,7 +745,7 @@ where
735
745
{
736
746
#[ inline]
737
747
fn as_ref ( & self ) -> & [ T ; LANES ] {
738
- & self . 0
748
+ self . as_array ( )
739
749
}
740
750
}
741
751
@@ -746,7 +756,7 @@ where
746
756
{
747
757
#[ inline]
748
758
fn as_mut ( & mut self ) -> & mut [ T ; LANES ] {
749
- & mut self . 0
759
+ self . as_mut_array ( )
750
760
}
751
761
}
752
762
@@ -758,7 +768,7 @@ where
758
768
{
759
769
#[ inline]
760
770
fn as_ref ( & self ) -> & [ T ] {
761
- & self . 0
771
+ self . as_array ( )
762
772
}
763
773
}
764
774
@@ -769,7 +779,7 @@ where
769
779
{
770
780
#[ inline]
771
781
fn as_mut ( & mut self ) -> & mut [ T ] {
772
- & mut self . 0
782
+ self . as_mut_array ( )
773
783
}
774
784
}
775
785
0 commit comments