| 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 i32 { |
| 94 |
use crate::{IVec2, IVec3, IVec4}; |
| 95 |
use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize}; |
| 96 |
|
| 97 |
impl_rkyv!(IVec2); |
| 98 |
impl_rkyv!(IVec3); |
| 99 |
impl_rkyv!(IVec4); |
| 100 |
} |
| 101 |
|
| 102 |
mod i64 { |
| 103 |
use crate::{I64Vec2, I64Vec3, I64Vec4}; |
| 104 |
use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize}; |
| 105 |
|
| 106 |
impl_rkyv!(I64Vec2); |
| 107 |
impl_rkyv!(I64Vec3); |
| 108 |
impl_rkyv!(I64Vec4); |
| 109 |
} |
| 110 |
|
| 111 |
mod u32 { |
| 112 |
use crate::{UVec2, UVec3, UVec4}; |
| 113 |
use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize}; |
| 114 |
|
| 115 |
impl_rkyv!(UVec2); |
| 116 |
impl_rkyv!(UVec3); |
| 117 |
impl_rkyv!(UVec4); |
| 118 |
} |
| 119 |
|
| 120 |
mod u64 { |
| 121 |
use crate::{U64Vec2, U64Vec3, U64Vec4}; |
| 122 |
use rkyv::{from_archived, to_archived, Archive, Deserialize, Fallible, Serialize}; |
| 123 |
|
| 124 |
impl_rkyv!(U64Vec2); |
| 125 |
impl_rkyv!(U64Vec3); |
| 126 |
impl_rkyv!(U64Vec4); |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(test)] |
| 130 |
mod test { |
| 131 |
pub type DefaultSerializer = rkyv::ser::serializers::CoreSerializer<256, 256>; |
| 132 |
pub type DefaultDeserializer = rkyv::Infallible; |
| 133 |
use rkyv::ser::Serializer; |
| 134 |
use rkyv::*; |
| 135 |
pub fn test_archive<T>(value: &T) |
| 136 |
where |
| 137 |
T: core::fmt::Debug + PartialEq + rkyv::Serialize<DefaultSerializer>, |
| 138 |
T::Archived: core::fmt::Debug + PartialEq<T> + rkyv::Deserialize<T, DefaultDeserializer>, |
| 139 |
{ |
| 140 |
let mut serializer = DefaultSerializer::default(); |
| 141 |
serializer |
| 142 |
.serialize_value(value) |
| 143 |
.expect("failed to archive value"); |
| 144 |
let len = serializer.pos(); |
| 145 |
let buffer = serializer.into_serializer().into_inner(); |
| 146 |
|
| 147 |
let archived_value = unsafe { rkyv::archived_root::<T>(&buffer[0..len]) }; |
| 148 |
assert_eq!(archived_value, value); |
| 149 |
let mut deserializer = DefaultDeserializer::default(); |
| 150 |
assert_eq!( |
| 151 |
&archived_value.deserialize(&mut deserializer).unwrap(), |
| 152 |
value |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_rkyv() { |
| 158 |
use crate::{Affine2, Affine3A, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4}; |
| 159 |
test_archive(&Affine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0])); |
| 160 |
test_archive(&Affine3A::from_cols_array(&[ |
| 161 |
1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0, |
| 162 |
])); |
| 163 |
test_archive(&Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0])); |
| 164 |
test_archive(&Mat3::from_cols_array(&[ |
| 165 |
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, |
| 166 |
])); |
| 167 |
test_archive(&Mat3A::from_cols_array(&[ |
| 168 |
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, |
| 169 |
])); |
| 170 |
test_archive(&Mat4::from_cols_array(&[ |
| 171 |
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, |
| 172 |
])); |
| 173 |
test_archive(&Quat::from_xyzw(1.0, 2.0, 3.0, 4.0)); |
| 174 |
test_archive(&Vec2::new(1.0, 2.0)); |
| 175 |
test_archive(&Vec3::new(1.0, 2.0, 3.0)); |
| 176 |
test_archive(&Vec3A::new(1.0, 2.0, 3.0)); |
| 177 |
test_archive(&Vec4::new(1.0, 2.0, 3.0, 4.0)); |
| 178 |
|
| 179 |
use crate::{DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4}; |
| 180 |
test_archive(&DAffine2::from_cols_array(&[1.0, 0.0, 2.0, 0.0, 3.0, 4.0])); |
| 181 |
test_archive(&DAffine3::from_cols_array(&[ |
| 182 |
1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0, 4.0, 5.0, 6.0, |
| 183 |
])); |
| 184 |
test_archive(&DMat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0])); |
| 185 |
test_archive(&DMat3::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(&DMat4::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(&DQuat::from_xyzw(1.0, 2.0, 3.0, 4.0)); |
| 192 |
test_archive(&DVec2::new(1.0, 2.0)); |
| 193 |
test_archive(&DVec3::new(1.0, 2.0, 3.0)); |
| 194 |
test_archive(&DVec4::new(1.0, 2.0, 3.0, 4.0)); |
| 195 |
|
| 196 |
use crate::{IVec2, IVec3, IVec4}; |
| 197 |
test_archive(&IVec2::new(-1, 2)); |
| 198 |
test_archive(&IVec3::new(-1, 2, 3)); |
| 199 |
test_archive(&IVec4::new(-1, 2, 3, 4)); |
| 200 |
|
| 201 |
use crate::{I64Vec2, I64Vec3, I64Vec4}; |
| 202 |
test_archive(&I64Vec2::new(-1, 2)); |
| 203 |
test_archive(&I64Vec3::new(-1, 2, 3)); |
| 204 |
test_archive(&I64Vec4::new(-1, 2, 3, 4)); |
| 205 |
|
| 206 |
use crate::{UVec2, UVec3, UVec4}; |
| 207 |
test_archive(&UVec2::new(1, 2)); |
| 208 |
test_archive(&UVec3::new(1, 2, 3)); |
| 209 |
test_archive(&UVec4::new(1, 2, 3, 4)); |
| 210 |
|
| 211 |
use crate::{U64Vec2, U64Vec3, U64Vec4}; |
| 212 |
test_archive(&U64Vec2::new(1, 2)); |
| 213 |
test_archive(&U64Vec3::new(1, 2, 3)); |
| 214 |
test_archive(&U64Vec4::new(1, 2, 3, 4)); |
| 215 |
} |
| 216 |
} |