From e959f2eb9cec6bd771eebf74cee425c0edc934cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 13 Jan 2023 09:58:41 +0100 Subject: [PATCH] Fix CheckBytes derives + drop Complex rkyv support for now --- Cargo.toml | 8 +--- src/base/array_storage.rs | 5 +-- src/base/dimension.rs | 8 ++-- src/base/matrix.rs | 9 ++--- src/base/mod.rs | 3 ++ src/base/rkyv_wrappers.rs | 72 +++++++++++++++++++++++++++++++++ src/base/unit.rs | 5 +-- src/geometry/dual_quaternion.rs | 5 +-- src/geometry/orthographic.rs | 5 +-- src/geometry/perspective.rs | 5 +-- src/geometry/quaternion.rs | 11 ++--- src/geometry/rotation.rs | 5 +-- src/geometry/scale.rs | 5 +-- src/geometry/translation.rs | 5 +-- tests/core/rkyv.rs | 11 +++-- 15 files changed, 100 insertions(+), 62 deletions(-) create mode 100644 src/base/rkyv_wrappers.rs diff --git a/Cargo.toml b/Cargo.toml index b657e6a2..9d0b1a7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,8 +54,8 @@ convert-glam022 = [ "glam022" ] ## `serde-serialize`. serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ] -rkyv-serialize-no-std = [ "rkyv", "rkyv_wrappers", "num-complex/rkyv" ] -rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "rkyv/validation", "bytecheck", "num-complex/bytecheck" ] +rkyv-serialize-no-std = [ "rkyv/size_32" ] +rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "rkyv/validation", "bytecheck" ] # Randomness ## To use rand in a #[no-std] environment, enable the @@ -83,7 +83,6 @@ rand_distr = { version = "0.4", default-features = false, optional = true } matrixmultiply = { version = "0.3", optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } rkyv = { version = "0.7", default-features = false, optional = true } -rkyv_wrappers = { git = "https://github.com/rkyv/rkyv_contrib", optional = true } bytecheck = { version = "~0.6.1", optional = true } mint = { version = "0.5", optional = true } quickcheck = { version = "1", optional = true } @@ -138,6 +137,3 @@ lto = true [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs all-features = true - -[patch.crates-io] -num-complex = { git = "https://github.com/zyansheep/num-complex" } \ No newline at end of file diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 94ed8bb0..5c165399 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -38,10 +38,7 @@ use std::mem; ") ) )] -#[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))] pub struct ArrayStorage(pub [[T; R]; C]); diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 06697618..3d1de1ff 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -208,12 +208,10 @@ dim_ops!( #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr( feature = "rkyv-serialize-no-std", - derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) -)] -#[cfg_attr( - feature = "rkyv-serialize", - archive_attr(derive(bytecheck::CheckBytes)) + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), + archive(as = "Self") )] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Const; diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 8f664dd4..700e2f02 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -12,9 +12,9 @@ use std::mem; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{with::With, Archive, Archived}; +use super::rkyv_wrappers::CustomPhantom; #[cfg(feature = "rkyv-serialize-no-std")] -use rkyv_wrappers::custom_phantom::CustomPhantom; +use rkyv::{with::With, Archive, Archived}; use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf}; use simba::simd::SimdPartialOrd; @@ -167,10 +167,7 @@ pub type MatrixCross = ") ) )] -#[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))] pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? diff --git a/src/base/mod.rs b/src/base/mod.rs index dfe7cc8d..4cbcff93 100644 --- a/src/base/mod.rs +++ b/src/base/mod.rs @@ -42,6 +42,9 @@ mod min_max; /// Mechanisms for working with values that may not be initialized. pub mod uninit; +#[cfg(feature = "rkyv-serialize-no-std")] +mod rkyv_wrappers; + pub use self::matrix::*; pub use self::norm::*; pub use self::scalar::*; diff --git a/src/base/rkyv_wrappers.rs b/src/base/rkyv_wrappers.rs new file mode 100644 index 00000000..b11c618a --- /dev/null +++ b/src/base/rkyv_wrappers.rs @@ -0,0 +1,72 @@ +//! Wrapper that allows changing the generic type of a PhantomData +//! +//! Copied from https://github.com/rkyv/rkyv_contrib (MIT-Apache2 licences) which isn’t 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. +/// +/// Example: +/// +/// ```rust +/// use std::marker::PhantomData; +/// use rkyv::{ +/// Archive, Serialize, Deserialize, Infallible, vec::ArchivedVec, Archived, with::With, +/// }; +/// use rkyv_wrappers::custom_phantom::CustomPhantom; +/// #[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq, Default)] +/// #[archive(as = "StructWithPhantom", bound(archive = " +/// T: Archive, +/// With, CustomPhantom>>: Archive>> +/// "))] +/// struct StructWithPhantom { +/// pub num: i32, +/// #[with(CustomPhantom)] +/// pub phantom: PhantomData, +/// } +/// let value = StructWithPhantom::>::default(); +/// let bytes = rkyv::to_bytes::<_, 1024>(&value).unwrap(); +/// let archived: &StructWithPhantom> = unsafe { rkyv::archived_root::>>(&bytes) }; +/// +/// let deserialized: StructWithPhantom> = archived.deserialize(&mut Infallible).unwrap(); +/// assert_eq!(deserialized, value); +/// ``` +pub struct CustomPhantom { + _data: PhantomData<*const NT>, +} + +impl ArchiveWith> for CustomPhantom { + type Archived = PhantomData; + type Resolver = (); + + #[inline] + unsafe fn resolve_with( + _: &PhantomData, + _: usize, + _: Self::Resolver, + _: *mut Self::Archived, + ) { + } +} + +impl SerializeWith, S> + for CustomPhantom +{ + #[inline] + fn serialize_with(_: &PhantomData, _: &mut S) -> Result { + Ok(()) + } +} + +impl + DeserializeWith, PhantomData, D> for CustomPhantom +{ + #[inline] + fn deserialize_with(_: &PhantomData, _: &mut D) -> Result, D::Error> { + Ok(PhantomData) + } +} diff --git a/src/base/unit.rs b/src/base/unit.rs index 3d6f677f..2fc51107 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -31,10 +31,7 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF ") ) )] -#[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))] pub struct Unit { pub(crate) value: T, diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 828f7116..bae04f46 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -51,10 +51,7 @@ use simba::scalar::{ClosedNeg, RealField}; ") ) )] -#[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))] pub struct DualQuaternion { /// The real component of the quaternion diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 0368ee6f..06d0b471 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -30,10 +30,7 @@ use crate::geometry::{Point3, Projective3}; ") ) )] -#[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))] #[derive(Copy, Clone)] pub struct Orthographic3 { diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 91e97efe..a5fc19a8 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -31,10 +31,7 @@ use crate::geometry::{Point3, Projective3}; ") ) )] -#[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))] #[derive(Copy, Clone)] pub struct Perspective3 { diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 9dfc3ec3..1b251b29 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -29,15 +29,12 @@ use crate::geometry::{Point3, Rotation}; archive( as = "Quaternion", bound(archive = " - T: rkyv::Archive, - Vector4: rkyv::Archive> - ") + T: rkyv::Archive, + Vector4: rkyv::Archive> + ") ) )] -#[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))] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 2774cb14..5eceec21 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -60,10 +60,7 @@ use crate::geometry::Point; ") ) )] -#[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))] #[derive(Copy, Clone)] pub struct Rotation { diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 93b2258f..36a68066 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -28,10 +28,7 @@ use crate::geometry::Point; ") ) )] -#[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))] #[derive(Copy, Clone)] pub struct Scale { diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index d44e891b..39fae3b6 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -28,10 +28,7 @@ use crate::geometry::Point; ") ) )] -#[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))] #[derive(Copy, Clone)] pub struct Translation { diff --git a/tests/core/rkyv.rs b/tests/core/rkyv.rs index 678e06d1..ffe9ed30 100644 --- a/tests/core/rkyv.rs +++ b/tests/core/rkyv.rs @@ -1,12 +1,11 @@ #![cfg(feature = "rkyv-serialize")] use na::{ - DMatrix, Isometry2, Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix2x3, Matrix3x4, Point2, - Point3, Quaternion, Rotation2, Rotation3, Similarity2, Similarity3, SimilarityMatrix2, - SimilarityMatrix3, Translation2, Translation3, Unit, Vector2, + Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, Quaternion, Rotation2, + Rotation3, Similarity3, SimilarityMatrix2, SimilarityMatrix3, Translation2, Translation3, }; use rand; -use rkyv::{Archive, Deserialize, Infallible, Serialize}; +use rkyv::{Deserialize, Infallible}; macro_rules! test_rkyv_same_type( ($($test: ident, $ty: ident);* $(;)*) => {$( @@ -52,9 +51,9 @@ test_rkyv_same_type!( rkyv_same_type_point2, Point2; rkyv_same_type_translation2, Translation2; rkyv_same_type_rotation2, Rotation2; - rkyv_same_type_isometry2, Isometry2; + // rkyv_same_type_isometry2, Isometry2; rkyv_same_type_isometry_matrix2, IsometryMatrix2; - rkyv_same_type_similarity2, Similarity2; + // rkyv_same_type_similarity2, Similarity2; rkyv_same_type_similarity_matrix2, SimilarityMatrix2; );