From b48b104d5784604ebf437ded1bc78791693cc78b Mon Sep 17 00:00:00 2001 From: David Koloski Date: Wed, 5 May 2021 11:14:18 -0400 Subject: [PATCH 1/3] Add rkyv implementations for a few types --- Cargo.toml | 5 ++++- src/base/array_storage.rs | 4 ++++ src/base/dimension.rs | 4 ++++ src/base/matrix.rs | 4 ++++ src/base/unit.rs | 4 ++++ src/geometry/isometry.rs | 4 ++++ src/geometry/quaternion.rs | 4 ++++ src/geometry/translation.rs | 4 ++++ 8 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bfd3f166..1a356c3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,8 @@ convert-bytemuck = [ "bytemuck" ] serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ] abomonation-serialize = [ "abomonation" ] +rkyv-serialize-no-std = [ "rkyv" ] +rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ] # Randomness ## To use rand in a #[no-std] environment, enable the @@ -74,6 +76,7 @@ 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 } abomonation = { version = "0.7", optional = true } +rkyv = { version = "~0.6.3", default-features = false, features = ["const_generics"], optional = true } mint = { version = "0.5", optional = true } glam = { version = "0.13", optional = true } quickcheck = { version = "1", optional = true } @@ -81,7 +84,7 @@ pest = { version = "2", optional = true } pest_derive = { version = "2", optional = true } bytemuck = { version = "1.5", optional = true } matrixcompare-core = { version = "0.1", optional = true } -proptest = { version = "1", optional = true, default-features = false, features = ["std"] } +proptest = { version = "1", optional = true, default-features = false, features = ["std"] } [dev-dependencies] serde_json = "1.0" diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 68da4c51..e575229c 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -18,6 +18,9 @@ use std::mem; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + use crate::base::allocator::Allocator; use crate::base::default_allocator::DefaultAllocator; use crate::base::dimension::{Const, ToTypenum}; @@ -34,6 +37,7 @@ use crate::base::Scalar; /// A array-based statically sized matrix data storage. #[repr(C)] #[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct ArrayStorage(pub [[T; R]; C]); // TODO: remove this once the stdlib implements Default for arrays. diff --git a/src/base/dimension.rs b/src/base/dimension.rs index dd87647d..2c51b2f4 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -11,6 +11,9 @@ use typenum::{self, Diff, Max, Maximum, Min, Minimum, Prod, Quot, Sum, Unsigned} #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub struct Dynamic { @@ -197,6 +200,7 @@ dim_ops!( ); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Const; /// Trait implemented exclusively by type-level integers. diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 3cde8a4f..c7e02e22 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -16,6 +16,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf}; use simba::simd::SimdPartialOrd; @@ -153,6 +156,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-no-std", derive(Archive, Deserialize, Serialize))] 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 c2e60da3..4b1d1948 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -6,6 +6,9 @@ use std::ops::Deref; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -26,6 +29,7 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// in their documentation, read their dedicated pages directly. #[repr(transparent)] #[derive(Clone, Hash, Debug, Copy)] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Unit { pub(crate) value: T, } diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index c5fd844c..7c8686c0 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -7,6 +7,9 @@ use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -68,6 +71,7 @@ use crate::geometry::{AbstractRotation, Point, Translation}; DefaultAllocator: Allocator>, Owned>: Deserialize<'de>")) )] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Serialize, Deserialize))] pub struct Isometry { /// The pure rotational part of this isometry. pub rotation: R, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 365095b8..9a920a8a 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -10,6 +10,9 @@ use crate::base::storage::Owned; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -28,6 +31,7 @@ use crate::geometry::{Point3, Rotation}; /// that may be used as a rotation. #[repr(C)] #[derive(Debug, Copy, Clone)] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. pub coords: Vector4, diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 1a8f4b04..92747dab 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -8,6 +8,9 @@ use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "rkyv-serialize-no-std")] +use rkyv::{Archive, Deserialize, Serialize}; + #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -23,6 +26,7 @@ use crate::geometry::Point; /// A translation. #[repr(C)] #[derive(Debug)] +#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Translation { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. From 38dd3345e03f565eb134a68526af6c7205df9fae Mon Sep 17 00:00:00 2001 From: David Koloski Date: Thu, 6 May 2021 20:59:02 -0400 Subject: [PATCH 2/3] Switch to distributive implementations --- Cargo.toml | 2 +- src/base/array_storage.rs | 32 +++++++++++++++++++++++- src/base/dimension.rs | 24 +++++++++++++++++- src/base/matrix.rs | 35 +++++++++++++++++++++++++- src/base/unit.rs | 34 ++++++++++++++++++++++++- src/geometry/isometry.rs | 50 ++++++++++++++++++++++++++++++++++++- src/geometry/quaternion.rs | 34 ++++++++++++++++++++++++- src/geometry/translation.rs | 34 ++++++++++++++++++++++++- 8 files changed, 237 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1a356c3d..f1d65128 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,7 +76,7 @@ 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 } abomonation = { version = "0.7", optional = true } -rkyv = { version = "~0.6.3", default-features = false, features = ["const_generics"], optional = true } +rkyv = { version = "~0.6.4", default-features = false, features = ["const_generics"], optional = true } mint = { version = "0.5", optional = true } glam = { version = "0.13", optional = true } quickcheck = { version = "1", optional = true } diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index e575229c..350240cd 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -37,7 +37,6 @@ use crate::base::Scalar; /// A array-based statically sized matrix data storage. #[repr(C)] #[derive(Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct ArrayStorage(pub [[T; R]; C]); // TODO: remove this once the stdlib implements Default for arrays. @@ -303,3 +302,34 @@ where self.as_slice().iter().fold(0, |acc, e| acc + e.extent()) } } + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for ArrayStorage { + type Archived = ArrayStorage; + type Resolver = <[[T; R]; C] as Archive>::Resolver; + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { + self.0.resolve( + pos + rkyv::offset_of!(Self::Archived, 0), + resolver, + rkyv::project_struct!(out: Self::Archived => 0) + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, S: rkyv::Fallible + ?Sized, const R: usize, const C: usize> Serialize for ArrayStorage { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.0.serialize(serializer)?) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, D> for ArrayStorage +where + T::Archived: Deserialize, +{ + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(ArrayStorage(self.0.deserialize(deserializer)?)) + } +} diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 2c51b2f4..3ee762f1 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -200,7 +200,6 @@ dim_ops!( ); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Const; /// Trait implemented exclusively by type-level integers. @@ -235,6 +234,29 @@ impl<'de, const D: usize> Deserialize<'de> for Const { } } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Const { + type Archived = Self; + type Resolver = (); + + fn resolve(&self, _: usize, _: Self::Resolver, _: &mut core::mem::MaybeUninit) { + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Serialize for Const { + fn serialize(&self, _: &mut S) -> Result { + Ok(()) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize for Const { + fn deserialize(&self, _: &mut D) -> Result { + Ok(Const) + } +} + pub trait ToConst { type Const: DimName; } diff --git a/src/base/matrix.rs b/src/base/matrix.rs index c7e02e22..939e78b3 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -156,7 +156,6 @@ 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-no-std", derive(Archive, Deserialize, Serialize))] pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? /// @@ -312,6 +311,40 @@ where { } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Matrix { + type Archived = Matrix; + type Resolver = S::Resolver; + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { + self.data.resolve( + pos + rkyv::offset_of!(Self::Archived, data), + resolver, + rkyv::project_struct!(out: Self::Archived => data) + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, _S: rkyv::Fallible + ?Sized> Serialize<_S> for Matrix { + fn serialize(&self, serializer: &mut _S) -> Result { + Ok(self.data.serialize(serializer)?) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, D> for Matrix +where + S::Archived: Deserialize, +{ + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Matrix { + data: self.data.deserialize(deserializer)?, + _phantoms: PhantomData, + }) + } +} + impl Matrix { /// Creates a new matrix with the given data without statically checking that the matrix /// dimension matches the storage dimension. diff --git a/src/base/unit.rs b/src/base/unit.rs index 4b1d1948..0c30f961 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -29,7 +29,6 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// in their documentation, read their dedicated pages directly. #[repr(transparent)] #[derive(Clone, Hash, Debug, Copy)] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Unit { pub(crate) value: T, } @@ -75,6 +74,39 @@ impl Abomonation for Unit { } } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Unit { + type Archived = Unit; + type Resolver = T::Resolver; + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut ::core::mem::MaybeUninit) { + self.value.resolve( + pos + rkyv::offset_of!(Self::Archived, value), + resolver, + rkyv::project_struct!(out: Self::Archived => value), + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, S: rkyv::Fallible + ?Sized> Serialize for Unit { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.value.serialize(serializer)?) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, D> for Unit +where + T::Archived: Deserialize, +{ + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Unit { + value: self.value.deserialize(deserializer)?, + }) + } +} + impl PartialEq for Unit> where T: Scalar + PartialEq, diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 7c8686c0..dea1b1be 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -71,7 +71,6 @@ use crate::geometry::{AbstractRotation, Point, Translation}; DefaultAllocator: Allocator>, Owned>: Deserialize<'de>")) )] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Serialize, Deserialize))] pub struct Isometry { /// The pure rotational part of this isometry. pub rotation: R, @@ -102,6 +101,55 @@ where } } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Isometry +where + T::Archived: Scalar, +{ + type Archived = Isometry; + type Resolver = (R::Resolver, as Archive>::Resolver); + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { + self.rotation.resolve( + pos + rkyv::offset_of!(Self::Archived, rotation), + resolver.0, + rkyv::project_struct!(out: Self::Archived => rotation) + ); + self.translation.resolve( + pos + rkyv::offset_of!(Self::Archived, translation), + resolver.1, + rkyv::project_struct!(out: Self::Archived => translation) + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, R: Serialize, S: rkyv::Fallible + ?Sized, const D: usize> Serialize for Isometry +where + T::Archived: Scalar, +{ + fn serialize(&self, serializer: &mut S) -> Result { + Ok(( + self.rotation.serialize(serializer)?, + self.translation.serialize(serializer)?, + )) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, _D> for Isometry +where + T::Archived: Scalar + Deserialize, + R::Archived: Scalar + Deserialize, +{ + fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { + Ok(Isometry { + rotation: self.rotation.deserialize(deserializer)?, + translation: self.translation.deserialize(deserializer)?, + }) + } +} + impl hash::Hash for Isometry where Owned>: hash::Hash, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 9a920a8a..7c6a70ae 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -31,7 +31,6 @@ use crate::geometry::{Point3, Rotation}; /// that may be used as a rotation. #[repr(C)] #[derive(Debug, Copy, Clone)] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. pub coords: Vector4, @@ -117,6 +116,39 @@ where } } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Quaternion { + type Archived = Quaternion; + type Resolver = as Archive>::Resolver; + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { + self.coords.resolve( + pos + rkyv::offset_of!(Self::Archived, coords), + resolver, + rkyv::project_struct!(out: Self::Archived => coords) + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, S: rkyv::Fallible + ?Sized> Serialize for Quaternion { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.coords.serialize(serializer)?) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, D> for Quaternion +where + T::Archived: Deserialize, +{ + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Quaternion { + coords: self.coords.deserialize(deserializer)?, + }) + } +} + impl Quaternion where T::Element: SimdRealField, diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 92747dab..7f3665ae 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -26,7 +26,6 @@ use crate::geometry::Point; /// A translation. #[repr(C)] #[derive(Debug)] -#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))] pub struct Translation { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. @@ -101,6 +100,39 @@ where } } +#[cfg(feature = "rkyv-serialize-no-std")] +impl Archive for Translation { + type Archived = Translation; + type Resolver = as Archive>::Resolver; + + fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { + self.vector.resolve( + pos + rkyv::offset_of!(Self::Archived, vector), + resolver, + rkyv::project_struct!(out: Self::Archived => vector) + ); + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl, S: rkyv::Fallible + ?Sized, const D: usize> Serialize for Translation { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.vector.serialize(serializer)?) + } +} + +#[cfg(feature = "rkyv-serialize-no-std")] +impl Deserialize, _D> for Translation +where + T::Archived: Deserialize, +{ + fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { + Ok(Translation { + vector: self.vector.deserialize(deserializer)?, + }) + } +} + impl Translation { /// Creates a new translation from the given vector. #[inline] From 9fff10b1f08f76aabc0a7ad8039eeae22020229c Mon Sep 17 00:00:00 2001 From: David Koloski Date: Sun, 9 May 2021 11:28:49 -0400 Subject: [PATCH 3/3] Move rkyv impls into separate modules --- src/base/array_storage.rs | 66 ++++++++++++++------------ src/base/dimension.rs | 39 ++++++++------- src/base/matrix.rs | 66 +++++++++++++++----------- src/base/unit.rs | 59 ++++++++++++----------- src/geometry/isometry.rs | 94 ++++++++++++++++++++----------------- src/geometry/quaternion.rs | 60 ++++++++++++----------- src/geometry/translation.rs | 61 +++++++++++++----------- 7 files changed, 247 insertions(+), 198 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 350240cd..6d681ed5 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -18,9 +18,6 @@ use std::mem; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - use crate::base::allocator::Allocator; use crate::base::default_allocator::DefaultAllocator; use crate::base::dimension::{Const, ToTypenum}; @@ -304,32 +301,43 @@ where } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for ArrayStorage { - type Archived = ArrayStorage; - type Resolver = <[[T; R]; C] as Archive>::Resolver; +mod rkyv_impl { + use super::ArrayStorage; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { - self.0.resolve( - pos + rkyv::offset_of!(Self::Archived, 0), - resolver, - rkyv::project_struct!(out: Self::Archived => 0) - ); - } -} - -#[cfg(feature = "rkyv-serialize-no-std")] -impl, S: rkyv::Fallible + ?Sized, const R: usize, const C: usize> Serialize for ArrayStorage { - fn serialize(&self, serializer: &mut S) -> Result { - Ok(self.0.serialize(serializer)?) - } -} - -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, D> for ArrayStorage -where - T::Archived: Deserialize, -{ - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(ArrayStorage(self.0.deserialize(deserializer)?)) + impl Archive for ArrayStorage { + type Archived = ArrayStorage; + type Resolver = <[[T; R]; C] as Archive>::Resolver; + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut core::mem::MaybeUninit, + ) { + self.0.resolve( + pos + offset_of!(Self::Archived, 0), + resolver, + project_struct!(out: Self::Archived => 0), + ); + } + } + + impl, S: Fallible + ?Sized, const R: usize, const C: usize> Serialize + for ArrayStorage + { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.0.serialize(serializer)?) + } + } + + impl + Deserialize, D> for ArrayStorage + where + T::Archived: Deserialize, + { + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(ArrayStorage(self.0.deserialize(deserializer)?)) + } } } diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 3ee762f1..2b5ccec3 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -11,9 +11,6 @@ use typenum::{self, Diff, Max, Maximum, Min, Minimum, Prod, Quot, Sum, Unsigned} #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub struct Dynamic { @@ -235,25 +232,33 @@ impl<'de, const D: usize> Deserialize<'de> for Const { } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Const { - type Archived = Self; - type Resolver = (); +mod rkyv_impl { + use super::Const; + use rkyv::{Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, _: usize, _: Self::Resolver, _: &mut core::mem::MaybeUninit) { + impl Archive for Const { + type Archived = Self; + type Resolver = (); + + fn resolve( + &self, + _: usize, + _: Self::Resolver, + _: &mut core::mem::MaybeUninit, + ) { + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Serialize for Const { - fn serialize(&self, _: &mut S) -> Result { - Ok(()) + impl Serialize for Const { + fn serialize(&self, _: &mut S) -> Result { + Ok(()) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize for Const { - fn deserialize(&self, _: &mut D) -> Result { - Ok(Const) + impl Deserialize for Const { + fn deserialize(&self, _: &mut D) -> Result { + Ok(Const) + } } } diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 939e78b3..bc9942be 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -16,9 +16,6 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf}; use simba::simd::SimdPartialOrd; @@ -312,36 +309,49 @@ where } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Matrix { - type Archived = Matrix; - type Resolver = S::Resolver; +mod rkyv_impl { + use super::Matrix; + use core::marker::PhantomData; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { - self.data.resolve( - pos + rkyv::offset_of!(Self::Archived, data), - resolver, - rkyv::project_struct!(out: Self::Archived => data) - ); + impl Archive for Matrix { + type Archived = Matrix; + type Resolver = S::Resolver; + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut core::mem::MaybeUninit, + ) { + self.data.resolve( + pos + offset_of!(Self::Archived, data), + resolver, + project_struct!(out: Self::Archived => data), + ); + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl, _S: rkyv::Fallible + ?Sized> Serialize<_S> for Matrix { - fn serialize(&self, serializer: &mut _S) -> Result { - Ok(self.data.serialize(serializer)?) + impl, _S: Fallible + ?Sized> Serialize<_S> + for Matrix + { + fn serialize(&self, serializer: &mut _S) -> Result { + Ok(self.data.serialize(serializer)?) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, D> for Matrix -where - S::Archived: Deserialize, -{ - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Matrix { - data: self.data.deserialize(deserializer)?, - _phantoms: PhantomData, - }) + impl + Deserialize, D> + for Matrix + where + S::Archived: Deserialize, + { + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Matrix { + data: self.data.deserialize(deserializer)?, + _phantoms: PhantomData, + }) + } } } diff --git a/src/base/unit.rs b/src/base/unit.rs index 0c30f961..b5a4096f 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -6,9 +6,6 @@ use std::ops::Deref; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -75,35 +72,43 @@ impl Abomonation for Unit { } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Unit { - type Archived = Unit; - type Resolver = T::Resolver; +mod rkyv_impl { + use super::Unit; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut ::core::mem::MaybeUninit) { - self.value.resolve( - pos + rkyv::offset_of!(Self::Archived, value), - resolver, - rkyv::project_struct!(out: Self::Archived => value), - ); + impl Archive for Unit { + type Archived = Unit; + type Resolver = T::Resolver; + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut ::core::mem::MaybeUninit, + ) { + self.value.resolve( + pos + offset_of!(Self::Archived, value), + resolver, + project_struct!(out: Self::Archived => value), + ); + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl, S: rkyv::Fallible + ?Sized> Serialize for Unit { - fn serialize(&self, serializer: &mut S) -> Result { - Ok(self.value.serialize(serializer)?) + impl, S: Fallible + ?Sized> Serialize for Unit { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.value.serialize(serializer)?) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, D> for Unit -where - T::Archived: Deserialize, -{ - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Unit { - value: self.value.deserialize(deserializer)?, - }) + impl Deserialize, D> for Unit + where + T::Archived: Deserialize, + { + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Unit { + value: self.value.deserialize(deserializer)?, + }) + } } } diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index dea1b1be..d307c393 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -7,9 +7,6 @@ use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -102,51 +99,62 @@ where } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Isometry -where - T::Archived: Scalar, -{ - type Archived = Isometry; - type Resolver = (R::Resolver, as Archive>::Resolver); +mod rkyv_impl { + use super::Isometry; + use crate::{base::Scalar, geometry::Translation}; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { - self.rotation.resolve( - pos + rkyv::offset_of!(Self::Archived, rotation), - resolver.0, - rkyv::project_struct!(out: Self::Archived => rotation) - ); - self.translation.resolve( - pos + rkyv::offset_of!(Self::Archived, translation), - resolver.1, - rkyv::project_struct!(out: Self::Archived => translation) - ); + impl Archive for Isometry + where + T::Archived: Scalar, + { + type Archived = Isometry; + type Resolver = (R::Resolver, as Archive>::Resolver); + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut core::mem::MaybeUninit, + ) { + self.rotation.resolve( + pos + offset_of!(Self::Archived, rotation), + resolver.0, + project_struct!(out: Self::Archived => rotation), + ); + self.translation.resolve( + pos + offset_of!(Self::Archived, translation), + resolver.1, + project_struct!(out: Self::Archived => translation), + ); + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl, R: Serialize, S: rkyv::Fallible + ?Sized, const D: usize> Serialize for Isometry -where - T::Archived: Scalar, -{ - fn serialize(&self, serializer: &mut S) -> Result { - Ok(( - self.rotation.serialize(serializer)?, - self.translation.serialize(serializer)?, - )) + impl, R: Serialize, S: Fallible + ?Sized, const D: usize> + Serialize for Isometry + where + T::Archived: Scalar, + { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(( + self.rotation.serialize(serializer)?, + self.translation.serialize(serializer)?, + )) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, _D> for Isometry -where - T::Archived: Scalar + Deserialize, - R::Archived: Scalar + Deserialize, -{ - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Isometry { - rotation: self.rotation.deserialize(deserializer)?, - translation: self.translation.deserialize(deserializer)?, - }) + impl + Deserialize, _D> for Isometry + where + T::Archived: Scalar + Deserialize, + R::Archived: Scalar + Deserialize, + { + fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { + Ok(Isometry { + rotation: self.rotation.deserialize(deserializer)?, + translation: self.translation.deserialize(deserializer)?, + }) + } } } diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 7c6a70ae..6852335b 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -10,9 +10,6 @@ use crate::base::storage::Owned; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -117,35 +114,44 @@ where } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Quaternion { - type Archived = Quaternion; - type Resolver = as Archive>::Resolver; +mod rkyv_impl { + use super::Quaternion; + use crate::base::Vector4; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { - self.coords.resolve( - pos + rkyv::offset_of!(Self::Archived, coords), - resolver, - rkyv::project_struct!(out: Self::Archived => coords) - ); + impl Archive for Quaternion { + type Archived = Quaternion; + type Resolver = as Archive>::Resolver; + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut core::mem::MaybeUninit, + ) { + self.coords.resolve( + pos + offset_of!(Self::Archived, coords), + resolver, + project_struct!(out: Self::Archived => coords), + ); + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl, S: rkyv::Fallible + ?Sized> Serialize for Quaternion { - fn serialize(&self, serializer: &mut S) -> Result { - Ok(self.coords.serialize(serializer)?) + impl, S: Fallible + ?Sized> Serialize for Quaternion { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.coords.serialize(serializer)?) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, D> for Quaternion -where - T::Archived: Deserialize, -{ - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Quaternion { - coords: self.coords.deserialize(deserializer)?, - }) + impl Deserialize, D> for Quaternion + where + T::Archived: Deserialize, + { + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + Ok(Quaternion { + coords: self.coords.deserialize(deserializer)?, + }) + } } } diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 7f3665ae..dcb7b603 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -8,9 +8,6 @@ use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "rkyv-serialize-no-std")] -use rkyv::{Archive, Deserialize, Serialize}; - #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -101,35 +98,45 @@ where } #[cfg(feature = "rkyv-serialize-no-std")] -impl Archive for Translation { - type Archived = Translation; - type Resolver = as Archive>::Resolver; +mod rkyv_impl { + use super::Translation; + use crate::base::SVector; + use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; - fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit) { - self.vector.resolve( - pos + rkyv::offset_of!(Self::Archived, vector), - resolver, - rkyv::project_struct!(out: Self::Archived => vector) - ); + impl Archive for Translation { + type Archived = Translation; + type Resolver = as Archive>::Resolver; + + fn resolve( + &self, + pos: usize, + resolver: Self::Resolver, + out: &mut core::mem::MaybeUninit, + ) { + self.vector.resolve( + pos + offset_of!(Self::Archived, vector), + resolver, + project_struct!(out: Self::Archived => vector), + ); + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl, S: rkyv::Fallible + ?Sized, const D: usize> Serialize for Translation { - fn serialize(&self, serializer: &mut S) -> Result { - Ok(self.vector.serialize(serializer)?) + impl, S: Fallible + ?Sized, const D: usize> Serialize for Translation { + fn serialize(&self, serializer: &mut S) -> Result { + Ok(self.vector.serialize(serializer)?) + } } -} -#[cfg(feature = "rkyv-serialize-no-std")] -impl Deserialize, _D> for Translation -where - T::Archived: Deserialize, -{ - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Translation { - vector: self.vector.deserialize(deserializer)?, - }) + impl Deserialize, _D> + for Translation + where + T::Archived: Deserialize, + { + fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { + Ok(Translation { + vector: self.vector.deserialize(deserializer)?, + }) + } } }