parry3d/utils/
as_bytes.rs

1use core::mem;
2use core::slice;
3
4use crate::math::{Real, Vector2, Vector3};
5
6/// Trait that transforms thing to a slice of u8.
7pub trait AsBytes {
8    /// Converts `self` to a slice of bytes.
9    fn as_bytes(&self) -> &[u8];
10}
11
12macro_rules! as_bytes_impl(
13    ($T: ty, $dimension: expr) => (
14        impl AsBytes for $T {
15            #[inline(always)]
16            fn as_bytes(&self) -> &[u8] {
17                unsafe {
18                    slice::from_raw_parts(self as *const $T as *const u8, mem::size_of::<Real>() * $dimension)
19                }
20            }
21        }
22    )
23);
24
25as_bytes_impl!(Vector2, 2);
26as_bytes_impl!(Vector3, 3);
27
28// TODO: implement for all `T: Copy` instead?