Merge pull request #1193 from dimforge/rkyv-same-type

Make sure Archive types are Self
This commit is contained in:
Sébastien Crozet 2023-01-13 11:11:43 +01:00 committed by GitHub
commit 0cf79aef0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 234 additions and 110 deletions

View File

@ -50,7 +50,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# Needed because the --all-features build which enables cuda support. # Needed because the --all-features build which enables cuda support.
- uses: Jimver/cuda-toolkit@v0.2.4 - uses: Jimver/cuda-toolkit@v0.2.8
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- run: cargo build --all-features; - run: cargo build --all-features;
- run: cargo build -p nalgebra-glm --all-features; - run: cargo build -p nalgebra-glm --all-features;
@ -123,9 +123,9 @@ jobs:
build-cuda: build-cuda:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: Jimver/cuda-toolkit@v0.2.4 - uses: Jimver/cuda-toolkit@v0.2.8
with: with:
cuda: '11.2.2' cuda: '11.5.0'
- name: Install nightly-2021-12-04 - name: Install nightly-2021-12-04
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:

View File

@ -54,8 +54,8 @@ convert-glam022 = [ "glam022" ]
## `serde-serialize`. ## `serde-serialize`.
serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize-no-std = [ "serde", "num-complex/serde" ]
serde-serialize = [ "serde-serialize-no-std", "serde/std" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ]
rkyv-serialize-no-std = [ "rkyv" ] rkyv-serialize-no-std = [ "rkyv/size_32" ]
rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "bytecheck" ] rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "rkyv/validation", "bytecheck" ]
# Randomness # Randomness
## To use rand in a #[no-std] environment, enable the ## To use rand in a #[no-std] environment, enable the
@ -82,7 +82,7 @@ alga = { version = "0.9", default-features = false, optional = true }
rand_distr = { version = "0.4", default-features = false, optional = true } rand_distr = { version = "0.4", default-features = false, optional = true }
matrixmultiply = { version = "0.3", optional = true } matrixmultiply = { version = "0.3", optional = true }
serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true }
rkyv = { version = "~0.7.1", optional = true } rkyv = { version = "0.7", default-features = false, optional = true }
bytecheck = { version = "~0.6.1", optional = true } bytecheck = { version = "~0.6.1", optional = true }
mint = { version = "0.5", optional = true } mint = { version = "0.5", optional = true }
quickcheck = { version = "1", optional = true } quickcheck = { version = "1", optional = true }
@ -137,4 +137,3 @@ lto = true
[package.metadata.docs.rs] [package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs # Enable all the features when building the docs on docs.rs
all-features = true all-features = true

View File

@ -29,12 +29,16 @@ use std::mem;
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "ArrayStorage<T::Archived, R, C>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
[[T; R]; C]: rkyv::Archive<Archived = [[T::Archived; R]; C]>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]); pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);

View File

@ -208,12 +208,10 @@ dim_ops!(
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(as = "Self")
#[cfg_attr(
feature = "rkyv-serialize",
archive_attr(derive(bytecheck::CheckBytes))
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Const<const R: usize>; pub struct Const<const R: usize>;

View File

@ -11,6 +11,11 @@ use std::mem;
#[cfg(feature = "serde-serialize-no-std")] #[cfg(feature = "serde-serialize-no-std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "rkyv-serialize-no-std")]
use super::rkyv_wrappers::CustomPhantom;
#[cfg(feature = "rkyv-serialize-no-std")]
use rkyv::{with::With, Archive, Archived};
use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf}; use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf};
use simba::simd::SimdPartialOrd; use simba::simd::SimdPartialOrd;
@ -152,12 +157,17 @@ pub type MatrixCross<T, R1, C1, R2, C2> =
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Matrix<T::Archived, R, C, S::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: Archive,
S: Archive,
With<PhantomData<(T, R, C)>, CustomPhantom<(Archived<T>, R, C)>>: Archive<Archived = PhantomData<(Archived<T>, R, C)>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Matrix<T, R, C, S> { pub struct Matrix<T, R, C, S> {
/// The data storage that contains all the matrix components. Disappointed? /// The data storage that contains all the matrix components. Disappointed?
@ -195,6 +205,7 @@ pub struct Matrix<T, R, C, S> {
// of the `RawStorage` trait. However, because we don't have // of the `RawStorage` trait. However, because we don't have
// specialization, this is not possible because these `T, R, C` // specialization, this is not possible because these `T, R, C`
// allows us to desambiguate a lot of configurations. // allows us to desambiguate a lot of configurations.
#[cfg_attr(feature = "rkyv-serialize-no-std", with(CustomPhantom<(T::Archived, R, C)>))]
_phantoms: PhantomData<(T, R, C)>, _phantoms: PhantomData<(T, R, C)>,
} }

View File

@ -42,6 +42,9 @@ mod min_max;
/// Mechanisms for working with values that may not be initialized. /// Mechanisms for working with values that may not be initialized.
pub mod uninit; pub mod uninit;
#[cfg(feature = "rkyv-serialize-no-std")]
mod rkyv_wrappers;
pub use self::matrix::*; pub use self::matrix::*;
pub use self::norm::*; pub use self::norm::*;
pub use self::scalar::*; pub use self::scalar::*;

46
src/base/rkyv_wrappers.rs Normal file
View File

@ -0,0 +1,46 @@
//! Wrapper that allows changing the generic type of a PhantomData<T>
//!
//! Copied from https://github.com/rkyv/rkyv_contrib (MIT-Apache2 licences) which isnt published yet.
use rkyv::{
with::{ArchiveWith, DeserializeWith, SerializeWith},
Fallible,
};
use std::marker::PhantomData;
/// A wrapper that allows for changing the generic type of a PhantomData<T>.
pub struct CustomPhantom<NT: ?Sized> {
_data: PhantomData<*const NT>,
}
impl<OT: ?Sized, NT: ?Sized> ArchiveWith<PhantomData<OT>> for CustomPhantom<NT> {
type Archived = PhantomData<NT>;
type Resolver = ();
#[inline]
unsafe fn resolve_with(
_: &PhantomData<OT>,
_: usize,
_: Self::Resolver,
_: *mut Self::Archived,
) {
}
}
impl<OT: ?Sized, NT: ?Sized, S: Fallible + ?Sized> SerializeWith<PhantomData<OT>, S>
for CustomPhantom<NT>
{
#[inline]
fn serialize_with(_: &PhantomData<OT>, _: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(())
}
}
impl<OT: ?Sized, NT: ?Sized, D: Fallible + ?Sized>
DeserializeWith<PhantomData<NT>, PhantomData<OT>, D> for CustomPhantom<NT>
{
#[inline]
fn deserialize_with(_: &PhantomData<NT>, _: &mut D) -> Result<PhantomData<OT>, D::Error> {
Ok(PhantomData)
}
}

View File

@ -23,12 +23,15 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF
#[derive(Clone, Hash, Copy)] #[derive(Clone, Hash, Copy)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Unit<T::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
// #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] // #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Unit<T> { pub struct Unit<T> {
pub(crate) value: T, pub(crate) value: T,

View File

@ -42,12 +42,16 @@ use simba::scalar::{ClosedNeg, RealField};
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "DualQuaternion<T::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
Quaternion<T>: rkyv::Archive<Archived = Quaternion<T::Archived>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct DualQuaternion<T> { pub struct DualQuaternion<T> {
/// The real component of the quaternion /// The real component of the quaternion

View File

@ -67,13 +67,18 @@ use crate::geometry::{AbstractRotation, Point, Translation};
Owned<T, Const<D>>: Deserialize<'de>, Owned<T, Const<D>>: Deserialize<'de>,
T: Scalar")) T: Scalar"))
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Isometry<T::Archived, R::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
R: rkyv::Archive,
Translation<T, D>: rkyv::Archive<Archived = Translation<T::Archived, D>>
")
)
)] )]
pub struct Isometry<T, R, const D: usize> { pub struct Isometry<T, R, const D: usize> {
/// The pure rotational part of this isometry. /// The pure rotational part of this isometry.

View File

@ -21,12 +21,16 @@ use crate::geometry::{Point3, Projective3};
#[repr(C)] #[repr(C)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Orthographic3<T::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
Matrix4<T>: rkyv::Archive<Archived = Matrix4<T::Archived>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Orthographic3<T> { pub struct Orthographic3<T> {

View File

@ -22,12 +22,16 @@ use crate::geometry::{Point3, Projective3};
#[repr(C)] #[repr(C)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Perspective3<T::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
Matrix4<T>: rkyv::Archive<Archived = Matrix4<T::Archived>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Perspective3<T> { pub struct Perspective3<T> {

View File

@ -36,13 +36,19 @@ use std::mem::MaybeUninit;
/// of said transformations for details. /// of said transformations for details.
#[repr(C)] #[repr(C)]
#[derive(Clone)] #[derive(Clone)]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "OPoint<T::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
T::Archived: Scalar,
OVector<T, D>: rkyv::Archive<Archived = OVector<T::Archived, D>>,
DefaultAllocator: Allocator<T::Archived, D>,
")
)
)] )]
pub struct OPoint<T: Scalar, D: DimName> pub struct OPoint<T: Scalar, D: DimName>
where where

View File

@ -25,12 +25,16 @@ use crate::geometry::{Point3, Rotation};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Quaternion<T::Archived>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
Vector4<T>: rkyv::Archive<Archived = Vector4<T::Archived>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Quaternion<T> { pub struct Quaternion<T> {
/// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order.

View File

@ -51,12 +51,16 @@ use crate::geometry::Point;
#[repr(C)] #[repr(C)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Rotation<T::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
SMatrix<T, D, D>: rkyv::Archive<Archived = SMatrix<T::Archived, D, D>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Rotation<T, const D: usize> { pub struct Rotation<T, const D: usize> {

View File

@ -19,12 +19,16 @@ use crate::geometry::Point;
#[repr(C)] #[repr(C)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Scale<T::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
SVector<T, D>: rkyv::Archive<Archived = SVector<T::Archived, D>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Scale<T, const D: usize> { pub struct Scale<T, const D: usize> {

View File

@ -34,13 +34,18 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation};
DefaultAllocator: Allocator<T, Const<D>>, DefaultAllocator: Allocator<T, Const<D>>,
Owned<T, Const<D>>: Deserialize<'de>")) Owned<T, Const<D>>: Deserialize<'de>"))
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Similarity<T::Archived, R::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
R: rkyv::Archive,
Isometry<T, R, D>: rkyv::Archive<Archived = Isometry<T::Archived, R::Archived, D>>
")
)
)] )]
pub struct Similarity<T, R, const D: usize> { pub struct Similarity<T, R, const D: usize> {
/// The part of this similarity that does not include the scaling factor. /// The part of this similarity that does not include the scaling factor.

View File

@ -19,12 +19,16 @@ use crate::geometry::Point;
#[repr(C)] #[repr(C)]
#[cfg_attr( #[cfg_attr(
feature = "rkyv-serialize-no-std", feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
)] archive(
#[cfg_attr( as = "Translation<T::Archived, D>",
feature = "rkyv-serialize", bound(archive = "
archive_attr(derive(bytecheck::CheckBytes)) T: rkyv::Archive,
SVector<T, D>: rkyv::Archive<Archived = SVector<T::Archived, D>>
")
)
)] )]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Translation<T, const D: usize> { pub struct Translation<T, const D: usize> {

View File

@ -83,14 +83,12 @@ an optimized set of tools for computer graphics and physics. Those features incl
unused_variables, unused_variables,
unused_mut, unused_mut,
unused_parens, unused_parens,
unused_qualifications,
rust_2018_idioms, rust_2018_idioms,
rust_2018_compatibility, rust_2018_compatibility,
future_incompatible, future_incompatible,
missing_copy_implementations missing_copy_implementations
)] )]
#![cfg_attr(feature = "rkyv-serialize-no-std", warn(unused_results))] // TODO: deny this once bytecheck stops generating warnings. #![cfg_attr(not(feature = "rkyv-serialize-no-std"), deny(unused_results))] // TODO: deny this globally once bytecheck stops generating unused results.
#![cfg_attr(not(feature = "rkyv-serialize-no-std"), deny(unused_results))]
#![doc( #![doc(
html_favicon_url = "https://nalgebra.org/img/favicon.ico", html_favicon_url = "https://nalgebra.org/img/favicon.ico",
html_root_url = "https://docs.rs/nalgebra/0.25.0" html_root_url = "https://docs.rs/nalgebra/0.25.0"

View File

@ -1,44 +1,62 @@
#![cfg(feature = "rkyv-serialize")]
use na::{ use na::{
Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, Quaternion, Rotation2, Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, Quaternion, Rotation2,
Rotation3, Similarity3, SimilarityMatrix2, SimilarityMatrix3, Translation2, Translation3, Rotation3, Similarity3, SimilarityMatrix2, SimilarityMatrix3, Translation2, Translation3,
}; };
use rkyv::ser::{serializers::AllocSerializer, Serializer}; use rand;
use rkyv::{Deserialize, Infallible};
macro_rules! test_rkyv( macro_rules! test_rkyv_same_type(
($($test: ident, $ty: ident);* $(;)*) => {$( ($($test: ident, $ty: ident);* $(;)*) => {$(
#[test] #[test]
fn $test() { fn $test() {
let v: $ty<f32> = rand::random(); let value: $ty<f32> = rand::random();
// serialize let bytes = rkyv::to_bytes::<_, 256>(&value).unwrap();
let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&v).unwrap();
let serialized = serializer.into_serializer().into_inner();
let deserialized: $ty<f32> = unsafe { rkyv::from_bytes_unchecked(&serialized).unwrap() }; let archived = rkyv::check_archived_root::<$ty<f32>>(&bytes[..]).unwrap();
assert_eq!(v, deserialized); // Compare archived and non-archived
assert_eq!(archived, &value);
#[cfg(feature = "rkyv-safe-deser")] // Make sure Debug implementations are the same for Archived and non-Archived versions.
{ assert_eq!(format!("{:?}", value), format!("{:?}", archived));
let deserialized: $ty<f32> = rkyv::from_bytes(&serialized).unwrap();
assert_eq!(v, deserialized);
} }
)*}
);
macro_rules! test_rkyv_diff_type(
($($test: ident, $ty: ident);* $(;)*) => {$(
#[test]
fn $test() {
let value: $ty<String> = Default::default();
let bytes = rkyv::to_bytes::<_, 256>(&value).unwrap();
let archived = rkyv::check_archived_root::<$ty<String>>(&bytes[..]).unwrap();
let deserialized: $ty<String> = archived.deserialize(&mut Infallible).unwrap();
assert_eq!(deserialized, value);
} }
)*} )*}
); );
test_rkyv!( // Tests to make sure
rkyv_matrix3x4, Matrix3x4; test_rkyv_same_type!(
rkyv_point3, Point3; rkyv_same_type_matrix3x4, Matrix3x4;
rkyv_translation3, Translation3; rkyv_same_type_point3, Point3;
rkyv_rotation3, Rotation3; rkyv_same_type_translation3, Translation3;
rkyv_isometry3, Isometry3; rkyv_same_type_rotation3, Rotation3;
rkyv_isometry_matrix3, IsometryMatrix3; rkyv_same_type_isometry3, Isometry3;
rkyv_similarity3, Similarity3; rkyv_same_type_isometry_matrix3, IsometryMatrix3;
rkyv_similarity_matrix3, SimilarityMatrix3; rkyv_same_type_similarity3, Similarity3;
rkyv_quaternion, Quaternion; rkyv_same_type_similarity_matrix3, SimilarityMatrix3;
rkyv_point2, Point2; rkyv_same_type_quaternion, Quaternion;
rkyv_translation2, Translation2; rkyv_same_type_point2, Point2;
rkyv_rotation2, Rotation2; rkyv_same_type_translation2, Translation2;
rkyv_isometry_matrix2, IsometryMatrix2; rkyv_same_type_rotation2, Rotation2;
rkyv_similarity_matrix2, SimilarityMatrix2; // rkyv_same_type_isometry2, Isometry2;
rkyv_same_type_isometry_matrix2, IsometryMatrix2;
// rkyv_same_type_similarity2, Similarity2;
rkyv_same_type_similarity_matrix2, SimilarityMatrix2;
); );
test_rkyv_diff_type! {
rkyv_diff_type_matrix3x4, Matrix3x4;
}