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                 out.write(to_archived!(*self as Self));
50             }
51         }
52 
53         impl<D: Fallible + ?Sized> Deserialize<$type, D> for $type {
54             #[inline]
55             fn deserialize(&self, _: &mut D) -> Result<$type, D::Error> {
56                 Ok(from_archived!(*self))
57             }
58         }
59     };
60 }
61 
62 mod f32 {
63     use crate::{Affine2, Affine3A, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
64     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
65     impl_rkyv!(Affine2);
66     impl_rkyv!(Affine3A);
67     impl_rkyv!(Mat2);
68     impl_rkyv!(Mat3);
69     impl_rkyv!(Mat3A);
70     impl_rkyv!(Mat4);
71     impl_rkyv!(Quat);
72     impl_rkyv!(Vec2);
73     impl_rkyv!(Vec3);
74     impl_rkyv!(Vec3A);
75     impl_rkyv!(Vec4);
76 }
77 
78 mod f64 {
79     use crate::{DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
80     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
81 
82     impl_rkyv!(DAffine2);
83     impl_rkyv!(DAffine3);
84     impl_rkyv!(DMat2);
85     impl_rkyv!(DMat3);
86     impl_rkyv!(DMat4);
87     impl_rkyv!(DQuat);
88     impl_rkyv!(DVec2);
89     impl_rkyv!(DVec3);
90     impl_rkyv!(DVec4);
91 }
92 
93 mod i16 {
94     use crate::{I16Vec2, I16Vec3, I16Vec4};
95     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
96 
97     impl_rkyv!(I16Vec2);
98     impl_rkyv!(I16Vec3);
99     impl_rkyv!(I16Vec4);
100 }
101 
102 mod i32 {
103     use crate::{IVec2, IVec3, IVec4};
104     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
105 
106     impl_rkyv!(IVec2);
107     impl_rkyv!(IVec3);
108     impl_rkyv!(IVec4);
109 }
110 
111 mod i64 {
112     use crate::{I64Vec2, I64Vec3, I64Vec4};
113     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
114 
115     impl_rkyv!(I64Vec2);
116     impl_rkyv!(I64Vec3);
117     impl_rkyv!(I64Vec4);
118 }
119 
120 mod u16 {
121     use crate::{U16Vec2, U16Vec3, U16Vec4};
122     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
123 
124     impl_rkyv!(U16Vec2);
125     impl_rkyv!(U16Vec3);
126     impl_rkyv!(U16Vec4);
127 }
128 
129 mod u32 {
130     use crate::{UVec2, UVec3, UVec4};
131     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
132 
133     impl_rkyv!(UVec2);
134     impl_rkyv!(UVec3);
135     impl_rkyv!(UVec4);
136 }
137 
138 mod u64 {
139     use crate::{U64Vec2, U64Vec3, U64Vec4};
140     use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize};
141 
142     impl_rkyv!(U64Vec2);
143     impl_rkyv!(U64Vec3);
144     impl_rkyv!(U64Vec4);
145 }
146 
147 #[cfg(test)]
148 mod test {
149     pub type DefaultSerializer = rkyv::ser::serializers::CoreSerializer<256, 256>;
150     pub type DefaultDeserializer = rkyv::Infallible;
151     use rkyv::ser::Serializer;
152     use rkyv::*;
test_archive<T>(value: &T) where T: core::fmt::Debug + PartialEq + rkyv::Serialize<DefaultSerializer>, T::Archived: core::fmt::Debug + PartialEq<T> + rkyv::Deserialize<T, DefaultDeserializer>,153     pub fn test_archive<T>(value: &T)
154     where
155         T: core::fmt::Debug + PartialEq + rkyv::Serialize<DefaultSerializer>,
156         T::Archived: core::fmt::Debug + PartialEq<T> + rkyv::Deserialize<T, DefaultDeserializer>,
157     {
158         let mut serializer = DefaultSerializer::default();
159         serializer
160             .serialize_value(value)
161             .expect("failed to archive value");
162         let len = serializer.pos();
163         let buffer = serializer.into_serializer().into_inner();
164 
165         let archived_value = unsafe { rkyv::archived_root::<T>(&buffer[0..len]) };
166         assert_eq!(archived_value, value);
167         let mut deserializer = DefaultDeserializer::default();
168         assert_eq!(
169             &archived_value.deserialize(&mut deserializer).unwrap(),
170             value
171         );
172     }
173 
174     #[test]
test_rkyv()175     fn test_rkyv() {
176         use crate::{Affine2, Affine3A, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
177         test_archive(&Affine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0]));
178         test_archive(&Affine3A::from_cols_array(&[
179             1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0,
180         ]));
181         test_archive(&Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]));
182         test_archive(&Mat3::from_cols_array(&[
183             1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
184         ]));
185         test_archive(&Mat3A::from_cols_array(&[
186             1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
187         ]));
188         test_archive(&Mat4::from_cols_array(&[
189             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,
190         ]));
191         test_archive(&Quat::from_xyzw(1.0, 2.0, 3.0, 4.0));
192         test_archive(&Vec2::new(1.0, 2.0));
193         test_archive(&Vec3::new(1.0, 2.0, 3.0));
194         test_archive(&Vec3A::new(1.0, 2.0, 3.0));
195         test_archive(&Vec4::new(1.0, 2.0, 3.0, 4.0));
196 
197         use crate::{DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
198         test_archive(&DAffine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0]));
199         test_archive(&DAffine3::from_cols_array(&[
200             1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0,
201         ]));
202         test_archive(&DMat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]));
203         test_archive(&DMat3::from_cols_array(&[
204             1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
205         ]));
206         test_archive(&DMat4::from_cols_array(&[
207             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,
208         ]));
209         test_archive(&DQuat::from_xyzw(1.0, 2.0, 3.0, 4.0));
210         test_archive(&DVec2::new(1.0, 2.0));
211         test_archive(&DVec3::new(1.0, 2.0, 3.0));
212         test_archive(&DVec4::new(1.0, 2.0, 3.0, 4.0));
213 
214         use crate::{I16Vec2, I16Vec3, I16Vec4};
215         test_archive(&I16Vec2::new(-1, 2));
216         test_archive(&I16Vec3::new(-1, 2, 3));
217         test_archive(&I16Vec4::new(-1, 2, 3, 4));
218 
219         use crate::{IVec2, IVec3, IVec4};
220         test_archive(&IVec2::new(-1, 2));
221         test_archive(&IVec3::new(-1, 2, 3));
222         test_archive(&IVec4::new(-1, 2, 3, 4));
223 
224         use crate::{I64Vec2, I64Vec3, I64Vec4};
225         test_archive(&I64Vec2::new(-1, 2));
226         test_archive(&I64Vec3::new(-1, 2, 3));
227         test_archive(&I64Vec4::new(-1, 2, 3, 4));
228 
229         use crate::{U16Vec2, U16Vec3, U16Vec4};
230         test_archive(&U16Vec2::new(1, 2));
231         test_archive(&U16Vec3::new(1, 2, 3));
232         test_archive(&U16Vec4::new(1, 2, 3, 4));
233 
234         use crate::{UVec2, UVec3, UVec4};
235         test_archive(&UVec2::new(1, 2));
236         test_archive(&UVec3::new(1, 2, 3));
237         test_archive(&UVec4::new(1, 2, 3, 4));
238 
239         use crate::{U64Vec2, U64Vec3, U64Vec4};
240         test_archive(&U64Vec2::new(1, 2));
241         test_archive(&U64Vec3::new(1, 2, 3));
242         test_archive(&U64Vec4::new(1, 2, 3, 4));
243     }
244 }
245