@@ -135,22 +135,26 @@ 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: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
139
+ unsafe { & * core:: ptr:: from_ref ( self ) . cast ( ) }
139
140
}
140
141
141
142
/// Returns a mutable array reference containing the entire SIMD vector.
142
143
pub fn as_mut_array ( & mut self ) -> & mut [ T ; LANES ] {
143
- & mut self . 0
144
+ // SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
145
+ unsafe { & mut * core:: ptr:: from_mut ( self ) . cast ( ) }
144
146
}
145
147
146
148
/// Converts an array to a SIMD vector.
147
149
pub const fn from_array ( array : [ T ; LANES ] ) -> Self {
148
- Self ( array)
150
+ // SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
151
+ unsafe { core:: mem:: transmute_copy ( & array) }
149
152
}
150
153
151
154
/// Converts a SIMD vector to an array.
152
155
pub const fn to_array ( self ) -> [ T ; LANES ] {
153
- self . 0
156
+ // SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
157
+ unsafe { core:: mem:: transmute_copy ( & self ) }
154
158
}
155
159
156
160
/// Converts a slice to a SIMD vector containing `slice[..LANES]`.
@@ -735,7 +739,7 @@ where
735
739
{
736
740
#[ inline]
737
741
fn as_ref ( & self ) -> & [ T ; LANES ] {
738
- & self . 0
742
+ self . as_array ( )
739
743
}
740
744
}
741
745
@@ -746,7 +750,7 @@ where
746
750
{
747
751
#[ inline]
748
752
fn as_mut ( & mut self ) -> & mut [ T ; LANES ] {
749
- & mut self . 0
753
+ self . as_mut_array ( )
750
754
}
751
755
}
752
756
@@ -758,7 +762,7 @@ where
758
762
{
759
763
#[ inline]
760
764
fn as_ref ( & self ) -> & [ T ] {
761
- & self . 0
765
+ self . as_array ( )
762
766
}
763
767
}
764
768
@@ -769,7 +773,7 @@ where
769
773
{
770
774
#[ inline]
771
775
fn as_mut ( & mut self ) -> & mut [ T ] {
772
- & mut self . 0
776
+ self . as_mut_array ( )
773
777
}
774
778
}
775
779
0 commit comments