Debian Codegraph @

debian/missing-sources/glam-0.29.3/src/features/impl_rkyv.rs

raw
1 #[cfg(feature = "bytecheck")]
2 macro_rules! impl_rkyv {
3 (@bytecheck $type:ty) => {
4 impl<C: ?Sized> bytecheck::CheckBytes<C> for $type {
5 type Error = core::convert::Infallible;
6
7 #[inline]
8 unsafe fn check_bytes<'a>(
9 value: *const Self,
10 _: &mut C,
11 ) -> Result<&'a Self, Self::Error> {
12 Ok(&*value)
13 }
14 }
15 };
16
17 ($type:ty) => {
18 impl_rkyv_derive!(@serialize $type);
19 impl_rkyv_derive!(@archive_deserialize $type);
20 impl_rkyv!(@bytecheck $type);
21 };
22 }
23
24 #[cfg(not(feature = "bytecheck"))]
25 macro_rules! impl_rkyv {
26 ($type:ty) => {
27 impl_rkyv_derive!(@serialize $type);
28 impl_rkyv_derive!(@archive_deserialize $type);
29 };
30 }
31
32 macro_rules! impl_rkyv_derive {
33 (@serialize $type:ty) => {
34 impl<S: Fallible + ?Sized> Serialize<S> for $type {
35 #[inline]
36 fn serialize(&self, _: &mut S) -> Result<Self::Resolver, S::Error> {
37 Ok(())
38 }
39 }
40 };
41
42 (@archive_deserialize $type:ty) => {
43 impl Archive for $type {
44 type Archived = $type;
45 type Resolver = ();
46
47 #[inline]
48 unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
49 #[allow(unexpected_cfgs)]
50 out.write(to_archived!(*self as Self));
51 }
52 }
53
54 impl<D: Fallible + ?Sized> Deserialize<$type, D> for $type {
55 #[inline]
56 fn deserialize(&self, _: &mut D) -> Result<$type, D::Error> {
57 #[allow(unexpected_cfgs)]
58 Ok(from_archived!(*self))
59 }
60 }
61 };
62 }
63
64 mod f32 {
65 use crate::{Affine2, Affine3A, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
66 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
67 impl_rkyv!(Affine2);
68 impl_rkyv!(Affine3A);
69 impl_rkyv!(Mat2);
70 impl_rkyv!(Mat3);
71 impl_rkyv!(Mat3A);
72 impl_rkyv!(Mat4);
73 impl_rkyv!(Quat);
74 impl_rkyv!(Vec2);
75 impl_rkyv!(Vec3);
76 impl_rkyv!(Vec3A);
77 impl_rkyv!(Vec4);
78 }
79
80 mod f64 {
81 use crate::{DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
82 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
83
84 impl_rkyv!(DAffine2);
85 impl_rkyv!(DAffine3);
86 impl_rkyv!(DMat2);
87 impl_rkyv!(DMat3);
88 impl_rkyv!(DMat4);
89 impl_rkyv!(DQuat);
90 impl_rkyv!(DVec2);
91 impl_rkyv!(DVec3);
92 impl_rkyv!(DVec4);
93 }
94
95 mod i8 {
96 use crate::{I8Vec2, I8Vec3, I8Vec4};
97 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
98
99 impl_rkyv!(I8Vec2);
100 impl_rkyv!(I8Vec3);
101 impl_rkyv!(I8Vec4);
102 }
103
104 mod i16 {
105 use crate::{I16Vec2, I16Vec3, I16Vec4};
106 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
107
108 impl_rkyv!(I16Vec2);
109 impl_rkyv!(I16Vec3);
110 impl_rkyv!(I16Vec4);
111 }
112
113 mod i32 {
114 use crate::{IVec2, IVec3, IVec4};
115 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
116
117 impl_rkyv!(IVec2);
118 impl_rkyv!(IVec3);
119 impl_rkyv!(IVec4);
120 }
121
122 mod i64 {
123 use crate::{I64Vec2, I64Vec3, I64Vec4};
124 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
125
126 impl_rkyv!(I64Vec2);
127 impl_rkyv!(I64Vec3);
128 impl_rkyv!(I64Vec4);
129 }
130
131 mod u8 {
132 use crate::{U8Vec2, U8Vec3, U8Vec4};
133 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
134
135 impl_rkyv!(U8Vec2);
136 impl_rkyv!(U8Vec3);
137 impl_rkyv!(U8Vec4);
138 }
139
140 mod u16 {
141 use crate::{U16Vec2, U16Vec3, U16Vec4};
142 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
143
144 impl_rkyv!(U16Vec2);
145 impl_rkyv!(U16Vec3);
146 impl_rkyv!(U16Vec4);
147 }
148
149 mod u32 {
150 use crate::{UVec2, UVec3, UVec4};
151 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
152
153 impl_rkyv!(UVec2);
154 impl_rkyv!(UVec3);
155 impl_rkyv!(UVec4);
156 }
157
158 mod u64 {
159 use crate::{U64Vec2, U64Vec3, U64Vec4};
160 use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
161
162 impl_rkyv!(U64Vec2);
163 impl_rkyv!(U64Vec3);
164 impl_rkyv!(U64Vec4);
165 }
166
167 #[cfg(test)]
168 mod test {
169 pub type DefaultSerializer = rkyv::ser::serializers::CoreSerializer<256, 256>;
170 pub type DefaultDeserializer = rkyv::Infallible;
171 use rkyv::ser::Serializer;
172 use rkyv::*;
173 pub fn test_archive<T>(value: &T)
174 where
175 T: core::fmt::Debug + PartialEq + rkyv::Serialize<DefaultSerializer>,
176 T::Archived: core::fmt::Debug + PartialEq<T> + rkyv::Deserialize<T, DefaultDeserializer>,
177 {
178 let mut serializer = DefaultSerializer::default();
179 serializer
180 .serialize_value(value)
181 .expect("failed to archive value");
182 let len = serializer.pos();
183 let buffer = serializer.into_serializer().into_inner();
184
185 let archived_value = unsafe { rkyv::archived_root::<T>(&buffer[0..len]) };
186 assert_eq!(archived_value, value);
187 let mut deserializer = DefaultDeserializer::default();
188 assert_eq!(
189 &archived_value.deserialize(&mut deserializer).unwrap(),
190 value
191 );
192 }
193
194 #[test]
195 fn test_rkyv() {
196 use crate::{Affine2, Affine3A, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
197 test_archive(&Affine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0]));
198 test_archive(&Affine3A::from_cols_array(&[
199 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0,
200 ]));
201 test_archive(&Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]));
202 test_archive(&Mat3::from_cols_array(&[
203 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
204 ]));
205 test_archive(&Mat3A::from_cols_array(&[
206 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
207 ]));
208 test_archive(&Mat4::from_cols_array(&[
209 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
210 ]));
211 test_archive(&Quat::from_xyzw(1.0, 2.0, 3.0, 4.0));
212 test_archive(&Vec2::new(1.0, 2.0));
213 test_archive(&Vec3::new(1.0, 2.0, 3.0));
214 test_archive(&Vec3A::new(1.0, 2.0, 3.0));
215 test_archive(&Vec4::new(1.0, 2.0, 3.0, 4.0));
216
217 use crate::{DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
218 test_archive(&DAffine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0]));
219 test_archive(&DAffine3::from_cols_array(&[
220 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0,
221 ]));
222 test_archive(&DMat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]));
223 test_archive(&DMat3::from_cols_array(&[
224 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
225 ]));
226 test_archive(&DMat4::from_cols_array(&[
227 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
228 ]));
229 test_archive(&DQuat::from_xyzw(1.0, 2.0, 3.0, 4.0));
230 test_archive(&DVec2::new(1.0, 2.0));
231 test_archive(&DVec3::new(1.0, 2.0, 3.0));
232 test_archive(&DVec4::new(1.0, 2.0, 3.0, 4.0));
233
234 use crate::{I8Vec2, I8Vec3, I8Vec4};
235 test_archive(&I8Vec2::new(-1, 2));
236 test_archive(&I8Vec3::new(-1, 2, 3));
237 test_archive(&I8Vec4::new(-1, 2, 3, 4));
238
239 use crate::{I16Vec2, I16Vec3, I16Vec4};
240 test_archive(&I16Vec2::new(-1, 2));
241 test_archive(&I16Vec3::new(-1, 2, 3));
242 test_archive(&I16Vec4::new(-1, 2, 3, 4));
243
244 use crate::{IVec2, IVec3, IVec4};
245 test_archive(&IVec2::new(-1, 2));
246 test_archive(&IVec3::new(-1, 2, 3));
247 test_archive(&IVec4::new(-1, 2, 3, 4));
248
249 use crate::{I64Vec2, I64Vec3, I64Vec4};
250 test_archive(&I64Vec2::new(-1, 2));
251 test_archive(&I64Vec3::new(-1, 2, 3));
252 test_archive(&I64Vec4::new(-1, 2, 3, 4));
253
254 use crate::{U8Vec2, U8Vec3, U8Vec4};
255 test_archive(&U8Vec2::new(1, 2));
256 test_archive(&U8Vec3::new(1, 2, 3));
257 test_archive(&U8Vec4::new(1, 2, 3, 4));
258
259 use crate::{U16Vec2, U16Vec3, U16Vec4};
260 test_archive(&U16Vec2::new(1, 2));
261 test_archive(&U16Vec3::new(1, 2, 3));
262 test_archive(&U16Vec4::new(1, 2, 3, 4));
263
264 use crate::{UVec2, UVec3, UVec4};
265 test_archive(&UVec2::new(1, 2));
266 test_archive(&UVec3::new(1, 2, 3));
267 test_archive(&UVec4::new(1, 2, 3, 4));
268
269 use crate::{U64Vec2, U64Vec3, U64Vec4};
270 test_archive(&U64Vec2::new(1, 2));
271 test_archive(&U64Vec3::new(1, 2, 3));
272 test_archive(&U64Vec4::new(1, 2, 3, 4));
273 }
274 }