From 525bc63de286713d98b22a9e78aad6a96365bfac Mon Sep 17 00:00:00 2001 From: zyansheep Date: Sun, 17 Jul 2022 16:51:20 -0400 Subject: [PATCH] use patch for num-complex & refactor --- Cargo.toml | 12 +++++---- src/base/array_storage.rs | 2 +- src/base/dimension.rs | 10 -------- src/base/matrix.rs | 2 +- src/base/unit.rs | 2 +- src/geometry/isometry.rs | 2 +- src/geometry/point.rs | 13 ++++++++-- src/geometry/similarity.rs | 2 +- tests/core/rkyv.rs | 52 ++++++++++++++++++++------------------ 9 files changed, 51 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e27d862b..0d1795a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,8 +52,8 @@ convert-glam020 = [ "glam020" ] ## `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" ] -rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "rkyv/validation", "bytecheck" ] +rkyv-serialize-no-std = [ "rkyv", "rkyv_wrappers", "num-complex/rkyv" ] +rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "rkyv/validation", "bytecheck", "num-complex/bytecheck" ] # Randomness ## To use rand in a #[no-std] environment, enable the @@ -71,15 +71,15 @@ nalgebra-macros = { version = "0.1", path = "nalgebra-macros", optional = true } typenum = "1.12" rand-package = { package = "rand", version = "0.8", optional = true, default-features = false } num-traits = { version = "0.2", default-features = false } -num-complex = { version = "0.4", git = "https://github.com/zyansheep/num-complex", default-features = false } # { version = "0.4", default-features = false } +num-complex = { version = "0.4", default-features = false } num-rational = { version = "0.4", default-features = false } approx = { version = "0.5", default-features = false } -simba = { version = "0.7.0", default-features = false } +simba = { version = "0.7", default-features = false } alga = { version = "0.9", default-features = false, optional = true } 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", 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 } @@ -134,3 +134,5 @@ lto = true # Enable certain features when building docs for docs.rs features = [ "proptest-support", "compare", "macros", "rand" ] +[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 5c165399..1104bf2e 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -27,6 +27,7 @@ use std::mem; /// A array-based statically sized matrix data storage. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), @@ -38,7 +39,6 @@ use std::mem; ") ) )] -#[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 4be97586..25ed637e 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -13,11 +13,6 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] -#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] -#[cfg_attr( - feature = "rkyv-serialize-no-std", - derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) -)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Dynamic { value: usize, @@ -203,11 +198,6 @@ 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", 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 fac84963..23d684f2 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -155,6 +155,7 @@ pub type MatrixCross = /// some concrete types for `T` and a compatible data storage type `S`). #[repr(C)] #[derive(Clone, Copy)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", derive(Archive, rkyv::Serialize, rkyv::Deserialize), @@ -167,7 +168,6 @@ pub type MatrixCross = ") ) )] -#[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/unit.rs b/src/base/unit.rs index 2fc51107..5381caaf 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -21,6 +21,7 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// in their documentation, read their dedicated pages directly. #[repr(transparent)] #[derive(Clone, Hash, Copy)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), @@ -31,7 +32,6 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF ") ) )] -#[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/isometry.rs b/src/geometry/isometry.rs index ca2aeb6d..66a3031b 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -66,6 +66,7 @@ use crate::geometry::{AbstractRotation, Point, Translation}; Owned>: Deserialize<'de>, T: Scalar")) )] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), @@ -78,7 +79,6 @@ use crate::geometry::{AbstractRotation, Point, Translation}; ") ) )] -#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct Isometry { /// The pure rotational part of this isometry. pub rotation: R, diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 306c18e5..28bd251c 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -36,11 +36,20 @@ use std::mem::MaybeUninit; /// of said transformations for details. #[repr(C)] #[derive(Clone)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", - derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), + archive( + as = "OPoint", + bound(archive = " + T: rkyv::Archive, + T::Archived: Scalar, + OVector: rkyv::Archive>, + DefaultAllocator: Allocator, + ") + ) )] -#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct OPoint where DefaultAllocator: Allocator, diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index b2ce1e1c..0d404c1f 100755 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -34,6 +34,7 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation}; DefaultAllocator: Allocator>, Owned>: Deserialize<'de>")) )] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr( feature = "rkyv-serialize-no-std", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), @@ -46,7 +47,6 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation}; ") ) )] -#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct Similarity { /// The part of this similarity that does not include the scaling factor. pub isometry: Isometry, diff --git a/tests/core/rkyv.rs b/tests/core/rkyv.rs index 12d85f56..678e06d1 100644 --- a/tests/core/rkyv.rs +++ b/tests/core/rkyv.rs @@ -8,7 +8,7 @@ use na::{ use rand; use rkyv::{Archive, Deserialize, Infallible, Serialize}; -macro_rules! test_rkyv_archived_impls( +macro_rules! test_rkyv_same_type( ($($test: ident, $ty: ident);* $(;)*) => {$( #[test] fn $test() { @@ -16,44 +16,48 @@ macro_rules! test_rkyv_archived_impls( let bytes = rkyv::to_bytes::<_, 256>(&value).unwrap(); let archived = rkyv::check_archived_root::<$ty>(&bytes[..]).unwrap(); + // Compare archived and non-archived assert_eq!(archived, &value); + // Make sure Debug implementations are the same for Archived and non-Archived versions. assert_eq!(format!("{:?}", value), format!("{:?}", archived)); } )*} ); -macro_rules! test_rkyv( +macro_rules! test_rkyv_diff_type( ($($test: ident, $ty: ident);* $(;)*) => {$( #[test] fn $test() { - let value: $ty = rand::random(); + let value: $ty = Default::default(); let bytes = rkyv::to_bytes::<_, 256>(&value).unwrap(); - let archived = rkyv::check_archived_root::<$ty>(&bytes[..]).unwrap(); - let deserialized: $ty = archived.deserialize(&mut Infallible).unwrap(); + let archived = rkyv::check_archived_root::<$ty>(&bytes[..]).unwrap(); + let deserialized: $ty = archived.deserialize(&mut Infallible).unwrap(); assert_eq!(deserialized, value); } )*} ); -test_rkyv_archived_impls!( - rkyv_matrix3x4, Matrix3x4; +// Tests to make sure +test_rkyv_same_type!( + rkyv_same_type_matrix3x4, Matrix3x4; + rkyv_same_type_point3, Point3; + rkyv_same_type_translation3, Translation3; + rkyv_same_type_rotation3, Rotation3; + rkyv_same_type_isometry3, Isometry3; + rkyv_same_type_isometry_matrix3, IsometryMatrix3; + rkyv_same_type_similarity3, Similarity3; + rkyv_same_type_similarity_matrix3, SimilarityMatrix3; + rkyv_same_type_quaternion, Quaternion; + rkyv_same_type_point2, Point2; + rkyv_same_type_translation2, Translation2; + rkyv_same_type_rotation2, Rotation2; + rkyv_same_type_isometry2, Isometry2; + rkyv_same_type_isometry_matrix2, IsometryMatrix2; + rkyv_same_type_similarity2, Similarity2; + rkyv_same_type_similarity_matrix2, SimilarityMatrix2; ); -test_rkyv!( - // rkyv_point3, Point3; - rkyv_translation3, Translation3; - /* rkyv_rotation3, Rotation3; - rkyv_isometry3, Isometry3; - rkyv_isometry_matrix3, IsometryMatrix3; - rkyv_similarity3, Similarity3; - rkyv_similarity_matrix3, SimilarityMatrix3; - rkyv_quaternion, Quaternion; */ - // rkyv_point2, Point2; - rkyv_translation2, Translation2; - /* rkyv_rotation2, Rotation2; - rkyv_isometry2, Isometry2; - rkyv_isometry_matrix2, IsometryMatrix2; - rkyv_similarity2, Similarity2; - rkyv_similarity_matrix2, SimilarityMatrix2; */ -); +test_rkyv_diff_type! { + rkyv_diff_type_matrix3x4, Matrix3x4; +}