aboutsummaryrefslogtreecommitdiff
path: root/src/features/impl_bytemuck.rs
blob: bed7ecde989f73983ddc310a9416711054cd9d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::{
    DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, Quat,
    UVec2, UVec3, UVec4, Vec2, Vec3, Vec4,
};
use bytemuck::{Pod, Zeroable};

unsafe impl Pod for Mat2 {}
unsafe impl Zeroable for Mat2 {}
unsafe impl Pod for Mat3 {}
unsafe impl Zeroable for Mat3 {}
unsafe impl Pod for Mat4 {}
unsafe impl Zeroable for Mat4 {}

unsafe impl Pod for Quat {}
unsafe impl Zeroable for Quat {}

unsafe impl Pod for Vec2 {}
unsafe impl Zeroable for Vec2 {}
unsafe impl Pod for Vec3 {}
unsafe impl Zeroable for Vec3 {}
unsafe impl Pod for Vec4 {}
unsafe impl Zeroable for Vec4 {}

unsafe impl Pod for DMat2 {}
unsafe impl Zeroable for DMat2 {}
unsafe impl Pod for DMat3 {}
unsafe impl Zeroable for DMat3 {}
unsafe impl Pod for DMat4 {}
unsafe impl Zeroable for DMat4 {}

unsafe impl Pod for DQuat {}
unsafe impl Zeroable for DQuat {}

unsafe impl Pod for DVec2 {}
unsafe impl Zeroable for DVec2 {}
unsafe impl Pod for DVec3 {}
unsafe impl Zeroable for DVec3 {}
unsafe impl Pod for DVec4 {}
unsafe impl Zeroable for DVec4 {}

unsafe impl Pod for IVec2 {}
unsafe impl Zeroable for IVec2 {}
unsafe impl Pod for IVec3 {}
unsafe impl Zeroable for IVec3 {}
unsafe impl Pod for IVec4 {}
unsafe impl Zeroable for IVec4 {}

unsafe impl Pod for UVec2 {}
unsafe impl Zeroable for UVec2 {}
unsafe impl Pod for UVec3 {}
unsafe impl Zeroable for UVec3 {}
unsafe impl Pod for UVec4 {}
unsafe impl Zeroable for UVec4 {}

#[cfg(test)]
mod test {
    use crate::{
        DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4,
        Quat, UVec2, UVec3, UVec4, Vec2, Vec3, Vec4,
    };
    use core::mem;

    macro_rules! test_t {
        ($name:ident, $t:ty) => {
            #[test]
            fn $name() {
                let t = <$t>::default();
                let b = bytemuck::bytes_of(&t);
                assert_eq!(t.as_ref().as_ptr() as usize, b.as_ptr() as usize);
                assert_eq!(b.len(), mem::size_of_val(&t));
            }
        };
    }

    test_t!(mat2, Mat2);
    test_t!(mat3, Mat3);
    test_t!(mat4, Mat4);
    test_t!(quat, Quat);
    test_t!(vec2, Vec2);
    test_t!(vec3, Vec3);
    test_t!(vec4, Vec4);

    test_t!(dmat2, DMat2);
    test_t!(dmat3, DMat3);
    test_t!(dmat4, DMat4);
    test_t!(dquat, DQuat);
    test_t!(dvec2, DVec2);
    test_t!(dvec3, DVec3);
    test_t!(dvec4, DVec4);

    test_t!(ivec2, IVec2);
    test_t!(ivec3, IVec3);
    test_t!(ivec4, IVec4);

    test_t!(uvec2, UVec2);
    test_t!(uvec3, UVec3);
    test_t!(uvec4, UVec4);
}