glam/features/
impl_bytemuck.rs

1use crate::{
2    Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4,
3    I16Vec2, I16Vec3, I16Vec4, I64Vec2, I64Vec3, I64Vec4, I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3,
4    IVec4, Mat2, Mat3, Mat3A, Mat4, Quat, U16Vec2, U16Vec3, U16Vec4, U64Vec2, U64Vec3, U64Vec4,
5    U8Vec2, U8Vec3, U8Vec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
6};
7use bytemuck::{AnyBitPattern, Pod, Zeroable};
8
9// Affine2 contains internal padding due to Mat2 using SIMD
10unsafe impl AnyBitPattern for Affine2 {}
11unsafe impl Zeroable for Affine2 {}
12unsafe impl AnyBitPattern for Affine3A {}
13unsafe impl Zeroable for Affine3A {}
14
15unsafe impl Pod for Mat2 {}
16unsafe impl Zeroable for Mat2 {}
17unsafe impl Pod for Mat3 {}
18unsafe impl Zeroable for Mat3 {}
19unsafe impl AnyBitPattern for Mat3A {}
20unsafe impl Zeroable for Mat3A {}
21unsafe impl Pod for Mat4 {}
22unsafe impl Zeroable for Mat4 {}
23
24unsafe impl Pod for Quat {}
25unsafe impl Zeroable for Quat {}
26
27unsafe impl Pod for Vec2 {}
28unsafe impl Zeroable for Vec2 {}
29unsafe impl Pod for Vec3 {}
30unsafe impl Zeroable for Vec3 {}
31unsafe impl AnyBitPattern for Vec3A {}
32unsafe impl Zeroable for Vec3A {}
33unsafe impl Pod for Vec4 {}
34unsafe impl Zeroable for Vec4 {}
35
36unsafe impl Pod for DAffine2 {}
37unsafe impl Zeroable for DAffine2 {}
38unsafe impl Pod for DAffine3 {}
39unsafe impl Zeroable for DAffine3 {}
40
41unsafe impl Pod for DMat2 {}
42unsafe impl Zeroable for DMat2 {}
43unsafe impl Pod for DMat3 {}
44unsafe impl Zeroable for DMat3 {}
45unsafe impl Pod for DMat4 {}
46unsafe impl Zeroable for DMat4 {}
47
48unsafe impl Pod for DQuat {}
49unsafe impl Zeroable for DQuat {}
50
51unsafe impl Pod for DVec2 {}
52unsafe impl Zeroable for DVec2 {}
53unsafe impl Pod for DVec3 {}
54unsafe impl Zeroable for DVec3 {}
55unsafe impl Pod for DVec4 {}
56unsafe impl Zeroable for DVec4 {}
57
58unsafe impl Pod for I8Vec2 {}
59unsafe impl Zeroable for I8Vec2 {}
60unsafe impl Pod for I8Vec3 {}
61unsafe impl Zeroable for I8Vec3 {}
62unsafe impl Pod for I8Vec4 {}
63unsafe impl Zeroable for I8Vec4 {}
64
65unsafe impl Pod for U8Vec2 {}
66unsafe impl Zeroable for U8Vec2 {}
67unsafe impl Pod for U8Vec3 {}
68unsafe impl Zeroable for U8Vec3 {}
69unsafe impl Pod for U8Vec4 {}
70unsafe impl Zeroable for U8Vec4 {}
71
72unsafe impl Pod for I16Vec2 {}
73unsafe impl Zeroable for I16Vec2 {}
74unsafe impl Pod for I16Vec3 {}
75unsafe impl Zeroable for I16Vec3 {}
76unsafe impl Pod for I16Vec4 {}
77unsafe impl Zeroable for I16Vec4 {}
78
79unsafe impl Pod for U16Vec2 {}
80unsafe impl Zeroable for U16Vec2 {}
81unsafe impl Pod for U16Vec3 {}
82unsafe impl Zeroable for U16Vec3 {}
83unsafe impl Pod for U16Vec4 {}
84unsafe impl Zeroable for U16Vec4 {}
85
86unsafe impl Pod for IVec2 {}
87unsafe impl Zeroable for IVec2 {}
88unsafe impl Pod for IVec3 {}
89unsafe impl Zeroable for IVec3 {}
90unsafe impl Pod for IVec4 {}
91unsafe impl Zeroable for IVec4 {}
92
93unsafe impl Pod for UVec2 {}
94unsafe impl Zeroable for UVec2 {}
95unsafe impl Pod for UVec3 {}
96unsafe impl Zeroable for UVec3 {}
97unsafe impl Pod for UVec4 {}
98unsafe impl Zeroable for UVec4 {}
99
100unsafe impl Pod for I64Vec2 {}
101unsafe impl Zeroable for I64Vec2 {}
102unsafe impl Pod for I64Vec3 {}
103unsafe impl Zeroable for I64Vec3 {}
104unsafe impl Pod for I64Vec4 {}
105unsafe impl Zeroable for I64Vec4 {}
106
107unsafe impl Pod for U64Vec2 {}
108unsafe impl Zeroable for U64Vec2 {}
109unsafe impl Pod for U64Vec3 {}
110unsafe impl Zeroable for U64Vec3 {}
111unsafe impl Pod for U64Vec4 {}
112unsafe impl Zeroable for U64Vec4 {}
113
114#[cfg(test)]
115mod test {
116    use crate::{
117        Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4,
118        I16Vec2, I16Vec3, I16Vec4, I64Vec2, I64Vec3, I64Vec4, I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3,
119        IVec4, Mat2, Mat3, Mat3A, Mat4, Quat, U16Vec2, U16Vec3, U16Vec4, U64Vec2, U64Vec3, U64Vec4,
120        U8Vec2, U8Vec3, U8Vec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
121    };
122    use core::mem;
123
124    macro_rules! test_pod_t {
125        ($name:ident, $t:ty) => {
126            #[test]
127            fn $name() {
128                let t = <$t>::default();
129                let b = bytemuck::bytes_of(&t);
130                // the below loop will fail in miri if we're doing something bad here.
131                for bi in b {
132                    assert_eq!(bi, bi);
133                }
134                // should be the same address
135                assert_eq!(&t as *const $t as usize, b.as_ptr() as usize);
136                // should be the same size
137                assert_eq!(b.len(), mem::size_of_val(&t));
138            }
139        };
140    }
141
142    macro_rules! test_any_bit_pattern_t {
143        ($name:ident, $t:ident) => {
144            #[test]
145            fn $name() {
146                let b = [0_u8; mem::size_of::<$t>()];
147                let t: $t = bytemuck::cast(b);
148                // should be the same size
149                assert_eq!(b.len(), mem::size_of_val(&t));
150                // should be zero
151                assert_eq!(t, $t::ZERO);
152            }
153        };
154    }
155
156    test_any_bit_pattern_t!(affine2, Affine2);
157    test_any_bit_pattern_t!(affine3a, Affine3A);
158    test_pod_t!(mat2, Mat2);
159    test_pod_t!(mat3, Mat3);
160    test_any_bit_pattern_t!(mat3a, Mat3A);
161    test_pod_t!(mat4, Mat4);
162    test_pod_t!(quat, Quat);
163    test_pod_t!(vec2, Vec2);
164    test_pod_t!(vec3, Vec3);
165    test_any_bit_pattern_t!(vec3a, Vec3A);
166    test_pod_t!(vec4, Vec4);
167
168    test_pod_t!(daffine2, DAffine2);
169    test_pod_t!(daffine3, DAffine3);
170    test_pod_t!(dmat2, DMat2);
171    test_pod_t!(dmat3, DMat3);
172    test_pod_t!(dmat4, DMat4);
173    test_pod_t!(dquat, DQuat);
174    test_pod_t!(dvec2, DVec2);
175    test_pod_t!(dvec3, DVec3);
176    test_pod_t!(dvec4, DVec4);
177
178    test_pod_t!(i8vec2, I8Vec2);
179    test_pod_t!(i8vec3, I8Vec3);
180    test_pod_t!(i8vec4, I8Vec4);
181
182    test_pod_t!(u8vec2, U8Vec2);
183    test_pod_t!(u8vec3, U8Vec3);
184    test_pod_t!(u8vec4, U8Vec4);
185
186    test_pod_t!(i16vec2, I16Vec2);
187    test_pod_t!(i16vec3, I16Vec3);
188    test_pod_t!(i16vec4, I16Vec4);
189
190    test_pod_t!(u16vec2, U16Vec2);
191    test_pod_t!(u16vec3, U16Vec3);
192    test_pod_t!(u16vec4, U16Vec4);
193
194    test_pod_t!(ivec2, IVec2);
195    test_pod_t!(ivec3, IVec3);
196    test_pod_t!(ivec4, IVec4);
197
198    test_pod_t!(uvec2, UVec2);
199    test_pod_t!(uvec3, UVec3);
200    test_pod_t!(uvec4, UVec4);
201
202    test_pod_t!(i64vec2, I64Vec2);
203    test_pod_t!(i64vec3, I64Vec3);
204    test_pod_t!(i64vec4, I64Vec4);
205
206    test_pod_t!(u64vec2, U64Vec2);
207    test_pod_t!(u64vec3, U64Vec3);
208    test_pod_t!(u64vec4, U64Vec4);
209}