1#![allow(unused_unsafe)]
6
7use crate::PodCastError;
8use core::{marker::*, mem::*};
9
10#[cfg(not(target_arch = "spirv"))]
24#[cold]
25#[inline(never)]
26#[cfg_attr(feature = "track_caller", track_caller)]
27pub(crate) fn something_went_wrong<D: core::fmt::Display>(
28 _src: &str, _err: D,
29) -> ! {
30 panic!("{src}>{err}", src = _src, err = _err);
34}
35
36#[cfg(target_arch = "spirv")]
38#[cold]
39#[inline(never)]
40pub(crate) fn something_went_wrong<D>(_src: &str, _err: D) -> ! {
41 panic!("Called a panicing helper from bytemuck which paniced");
47}
48
49#[inline(always)]
54pub(crate) unsafe fn bytes_of<T: Copy>(t: &T) -> &[u8] {
55 match try_cast_slice::<T, u8>(core::slice::from_ref(t)) {
56 Ok(s) => s,
57 Err(_) => unreachable!(),
58 }
59}
60
61#[inline]
66pub(crate) unsafe fn bytes_of_mut<T: Copy>(t: &mut T) -> &mut [u8] {
67 match try_cast_slice_mut::<T, u8>(core::slice::from_mut(t)) {
68 Ok(s) => s,
69 Err(_) => unreachable!(),
70 }
71}
72
73#[inline]
79#[cfg_attr(feature = "track_caller", track_caller)]
80pub(crate) unsafe fn from_bytes<T: Copy>(s: &[u8]) -> &T {
81 match try_from_bytes(s) {
82 Ok(t) => t,
83 Err(e) => something_went_wrong("from_bytes", e),
84 }
85}
86
87#[inline]
93#[cfg_attr(feature = "track_caller", track_caller)]
94pub(crate) unsafe fn from_bytes_mut<T: Copy>(s: &mut [u8]) -> &mut T {
95 match try_from_bytes_mut(s) {
96 Ok(t) => t,
97 Err(e) => something_went_wrong("from_bytes_mut", e),
98 }
99}
100
101#[inline]
106pub(crate) unsafe fn try_pod_read_unaligned<T: Copy>(
107 bytes: &[u8],
108) -> Result<T, PodCastError> {
109 if bytes.len() != size_of::<T>() {
110 Err(PodCastError::SizeMismatch)
111 } else {
112 Ok(unsafe { (bytes.as_ptr() as *const T).read_unaligned() })
113 }
114}
115
116#[inline]
121#[cfg_attr(feature = "track_caller", track_caller)]
122pub(crate) unsafe fn pod_read_unaligned<T: Copy>(bytes: &[u8]) -> T {
123 match try_pod_read_unaligned(bytes) {
124 Ok(t) => t,
125 Err(e) => something_went_wrong("pod_read_unaligned", e),
126 }
127}
128
129#[inline]
134#[cfg_attr(feature = "track_caller", track_caller)]
135pub(crate) fn is_aligned_to(ptr: *const (), align: usize) -> bool {
136 #[cfg(feature = "align_offset")]
137 {
138 ptr.align_offset(align) == 0
143 }
144 #[cfg(not(feature = "align_offset"))]
145 {
146 ((ptr as usize) % align) == 0
147 }
148}
149
150#[inline]
157pub(crate) unsafe fn try_from_bytes<T: Copy>(
158 s: &[u8],
159) -> Result<&T, PodCastError> {
160 if s.len() != size_of::<T>() {
161 Err(PodCastError::SizeMismatch)
162 } else if !is_aligned_to(s.as_ptr() as *const (), align_of::<T>()) {
163 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
164 } else {
165 Ok(unsafe { &*(s.as_ptr() as *const T) })
166 }
167}
168
169#[inline]
176pub(crate) unsafe fn try_from_bytes_mut<T: Copy>(
177 s: &mut [u8],
178) -> Result<&mut T, PodCastError> {
179 if s.len() != size_of::<T>() {
180 Err(PodCastError::SizeMismatch)
181 } else if !is_aligned_to(s.as_ptr() as *const (), align_of::<T>()) {
182 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
183 } else {
184 Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) })
185 }
186}
187
188#[inline]
194#[cfg_attr(feature = "track_caller", track_caller)]
195pub(crate) unsafe fn cast<A: Copy, B: Copy>(a: A) -> B {
196 if size_of::<A>() == size_of::<B>() {
197 unsafe { transmute!(a) }
198 } else {
199 something_went_wrong("cast", PodCastError::SizeMismatch)
200 }
201}
202
203#[inline]
209#[cfg_attr(feature = "track_caller", track_caller)]
210pub(crate) unsafe fn cast_mut<A: Copy, B: Copy>(a: &mut A) -> &mut B {
211 if size_of::<A>() == size_of::<B>() && align_of::<A>() >= align_of::<B>() {
212 match try_cast_mut(a) {
214 Ok(b) => b,
215 Err(_) => unreachable!(),
216 }
217 } else {
218 match try_cast_mut(a) {
219 Ok(b) => b,
220 Err(e) => something_went_wrong("cast_mut", e),
221 }
222 }
223}
224
225#[inline]
231#[cfg_attr(feature = "track_caller", track_caller)]
232pub(crate) unsafe fn cast_ref<A: Copy, B: Copy>(a: &A) -> &B {
233 if size_of::<A>() == size_of::<B>() && align_of::<A>() >= align_of::<B>() {
234 match try_cast_ref(a) {
236 Ok(b) => b,
237 Err(_) => unreachable!(),
238 }
239 } else {
240 match try_cast_ref(a) {
241 Ok(b) => b,
242 Err(e) => something_went_wrong("cast_ref", e),
243 }
244 }
245}
246
247#[inline]
253#[cfg_attr(feature = "track_caller", track_caller)]
254pub(crate) unsafe fn cast_slice<A: Copy, B: Copy>(a: &[A]) -> &[B] {
255 match try_cast_slice(a) {
256 Ok(b) => b,
257 Err(e) => something_went_wrong("cast_slice", e),
258 }
259}
260
261#[inline]
267#[cfg_attr(feature = "track_caller", track_caller)]
268pub(crate) unsafe fn cast_slice_mut<A: Copy, B: Copy>(a: &mut [A]) -> &mut [B] {
269 match try_cast_slice_mut(a) {
270 Ok(b) => b,
271 Err(e) => something_went_wrong("cast_slice_mut", e),
272 }
273}
274
275#[inline]
286pub(crate) unsafe fn try_cast<A: Copy, B: Copy>(
287 a: A,
288) -> Result<B, PodCastError> {
289 if size_of::<A>() == size_of::<B>() {
290 Ok(unsafe { transmute!(a) })
291 } else {
292 Err(PodCastError::SizeMismatch)
293 }
294}
295
296#[inline]
303pub(crate) unsafe fn try_cast_ref<A: Copy, B: Copy>(
304 a: &A,
305) -> Result<&B, PodCastError> {
306 if align_of::<B>() > align_of::<A>()
309 && !is_aligned_to(a as *const A as *const (), align_of::<B>())
310 {
311 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
312 } else if size_of::<B>() == size_of::<A>() {
313 Ok(unsafe { &*(a as *const A as *const B) })
314 } else {
315 Err(PodCastError::SizeMismatch)
316 }
317}
318
319#[inline]
323pub(crate) unsafe fn try_cast_mut<A: Copy, B: Copy>(
324 a: &mut A,
325) -> Result<&mut B, PodCastError> {
326 if align_of::<B>() > align_of::<A>()
329 && !is_aligned_to(a as *const A as *const (), align_of::<B>())
330 {
331 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
332 } else if size_of::<B>() == size_of::<A>() {
333 Ok(unsafe { &mut *(a as *mut A as *mut B) })
334 } else {
335 Err(PodCastError::SizeMismatch)
336 }
337}
338
339#[inline]
353pub(crate) unsafe fn try_cast_slice<A: Copy, B: Copy>(
354 a: &[A],
355) -> Result<&[B], PodCastError> {
356 let input_bytes = core::mem::size_of_val::<[A]>(a);
357 if align_of::<B>() > align_of::<A>()
360 && !is_aligned_to(a.as_ptr() as *const (), align_of::<B>())
361 {
362 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
363 } else if size_of::<B>() == size_of::<A>() {
364 Ok(unsafe { core::slice::from_raw_parts(a.as_ptr() as *const B, a.len()) })
365 } else if (size_of::<B>() != 0 && input_bytes % size_of::<B>() == 0)
366 || (size_of::<B>() == 0 && input_bytes == 0)
367 {
368 let new_len =
369 if size_of::<B>() != 0 { input_bytes / size_of::<B>() } else { 0 };
370 Ok(unsafe { core::slice::from_raw_parts(a.as_ptr() as *const B, new_len) })
371 } else {
372 Err(PodCastError::OutputSliceWouldHaveSlop)
373 }
374}
375
376#[inline]
381pub(crate) unsafe fn try_cast_slice_mut<A: Copy, B: Copy>(
382 a: &mut [A],
383) -> Result<&mut [B], PodCastError> {
384 let input_bytes = core::mem::size_of_val::<[A]>(a);
385 if align_of::<B>() > align_of::<A>()
388 && !is_aligned_to(a.as_ptr() as *const (), align_of::<B>())
389 {
390 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
391 } else if size_of::<B>() == size_of::<A>() {
392 Ok(unsafe {
393 core::slice::from_raw_parts_mut(a.as_mut_ptr() as *mut B, a.len())
394 })
395 } else if (size_of::<B>() != 0 && input_bytes % size_of::<B>() == 0)
396 || (size_of::<B>() == 0 && input_bytes == 0)
397 {
398 let new_len =
399 if size_of::<B>() != 0 { input_bytes / size_of::<B>() } else { 0 };
400 Ok(unsafe {
401 core::slice::from_raw_parts_mut(a.as_mut_ptr() as *mut B, new_len)
402 })
403 } else {
404 Err(PodCastError::OutputSliceWouldHaveSlop)
405 }
406}