glam/features/
impl_bytemuck.rs

1#[cfg(test)]
2mod test {
3    use crate::{
4        Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4,
5        I16Vec2, I16Vec3, I16Vec4, I64Vec2, I64Vec3, I64Vec4, I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3,
6        IVec4, Mat2, Mat3, Mat3A, Mat4, Quat, U16Vec2, U16Vec3, U16Vec4, U64Vec2, U64Vec3, U64Vec4,
7        U8Vec2, U8Vec3, U8Vec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
8    };
9    use core::mem;
10
11    macro_rules! test_pod_t {
12        ($name:ident, $t:ty) => {
13            #[test]
14            fn $name() {
15                let t = <$t>::default();
16                let b = bytemuck::bytes_of(&t);
17                // the below loop will fail in miri if we're doing something bad here.
18                for bi in b {
19                    assert_eq!(bi, bi);
20                }
21                // should be the same address
22                assert_eq!(&t as *const $t as usize, b.as_ptr() as usize);
23                // should be the same size
24                assert_eq!(b.len(), mem::size_of_val(&t));
25            }
26        };
27    }
28
29    macro_rules! test_any_bit_pattern_t {
30        ($name:ident, $t:ident) => {
31            #[test]
32            fn $name() {
33                let b = [0_u8; mem::size_of::<$t>()];
34                let t: $t = bytemuck::cast(b);
35                // should be the same size
36                assert_eq!(b.len(), mem::size_of_val(&t));
37                // should be zero
38                assert_eq!(t, $t::ZERO);
39            }
40        };
41    }
42
43    test_any_bit_pattern_t!(affine2, Affine2);
44    test_any_bit_pattern_t!(affine3a, Affine3A);
45    test_pod_t!(mat2, Mat2);
46    test_pod_t!(mat3, Mat3);
47    test_any_bit_pattern_t!(mat3a, Mat3A);
48    test_pod_t!(mat4, Mat4);
49    test_pod_t!(quat, Quat);
50    test_pod_t!(vec2, Vec2);
51    test_pod_t!(vec3, Vec3);
52    test_any_bit_pattern_t!(vec3a, Vec3A);
53    test_pod_t!(vec4, Vec4);
54
55    test_pod_t!(daffine2, DAffine2);
56    test_pod_t!(daffine3, DAffine3);
57    test_pod_t!(dmat2, DMat2);
58    test_pod_t!(dmat3, DMat3);
59    test_pod_t!(dmat4, DMat4);
60    test_pod_t!(dquat, DQuat);
61    test_pod_t!(dvec2, DVec2);
62    test_pod_t!(dvec3, DVec3);
63    test_pod_t!(dvec4, DVec4);
64
65    test_pod_t!(i8vec2, I8Vec2);
66    test_pod_t!(i8vec3, I8Vec3);
67    test_pod_t!(i8vec4, I8Vec4);
68
69    test_pod_t!(u8vec2, U8Vec2);
70    test_pod_t!(u8vec3, U8Vec3);
71    test_pod_t!(u8vec4, U8Vec4);
72
73    test_pod_t!(i16vec2, I16Vec2);
74    test_pod_t!(i16vec3, I16Vec3);
75    test_pod_t!(i16vec4, I16Vec4);
76
77    test_pod_t!(u16vec2, U16Vec2);
78    test_pod_t!(u16vec3, U16Vec3);
79    test_pod_t!(u16vec4, U16Vec4);
80
81    test_pod_t!(ivec2, IVec2);
82    test_pod_t!(ivec3, IVec3);
83    test_pod_t!(ivec4, IVec4);
84
85    test_pod_t!(uvec2, UVec2);
86    test_pod_t!(uvec3, UVec3);
87    test_pod_t!(uvec4, UVec4);
88
89    test_pod_t!(i64vec2, I64Vec2);
90    test_pod_t!(i64vec3, I64Vec3);
91    test_pod_t!(i64vec4, I64Vec4);
92
93    test_pod_t!(u64vec2, U64Vec2);
94    test_pod_t!(u64vec3, U64Vec3);
95    test_pod_t!(u64vec4, U64Vec4);
96}