]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/to_bytes.rs
Rollup merge of #99544 - dylni:expose-utf8lossy, r=Mark-Simulacrum
[rust.git] / library / portable-simd / crates / core_simd / src / to_bytes.rs
1 macro_rules! impl_to_bytes {
2     { $ty:ty, $size:literal } => {
3         impl<const LANES: usize> crate::simd::Simd<$ty, LANES>
4         where
5             crate::simd::LaneCount<LANES>: crate::simd::SupportedLaneCount,
6             crate::simd::LaneCount<{{ $size * LANES }}>: crate::simd::SupportedLaneCount,
7         {
8             /// Return the memory representation of this integer as a byte array in native byte
9             /// order.
10             pub fn to_ne_bytes(self) -> crate::simd::Simd<u8, {{ $size * LANES }}> {
11                 // Safety: transmuting between vectors is safe
12                 unsafe { core::mem::transmute_copy(&self) }
13             }
14
15             /// Create a native endian integer value from its memory representation as a byte array
16             /// in native endianness.
17             pub fn from_ne_bytes(bytes: crate::simd::Simd<u8, {{ $size * LANES }}>) -> Self {
18                 // Safety: transmuting between vectors is safe
19                 unsafe { core::mem::transmute_copy(&bytes) }
20             }
21         }
22     }
23 }
24
25 impl_to_bytes! { u8, 1 }
26 impl_to_bytes! { u16, 2 }
27 impl_to_bytes! { u32, 4 }
28 impl_to_bytes! { u64, 8 }
29 #[cfg(target_pointer_width = "32")]
30 impl_to_bytes! { usize, 4 }
31 #[cfg(target_pointer_width = "64")]
32 impl_to_bytes! { usize, 8 }
33
34 impl_to_bytes! { i8, 1 }
35 impl_to_bytes! { i16, 2 }
36 impl_to_bytes! { i32, 4 }
37 impl_to_bytes! { i64, 8 }
38 #[cfg(target_pointer_width = "32")]
39 impl_to_bytes! { isize, 4 }
40 #[cfg(target_pointer_width = "64")]
41 impl_to_bytes! { isize, 8 }