From 9fff10b1f08f76aabc0a7ad8039eeae22020229c Mon Sep 17 00:00:00 2001 From: David Koloski Date: Sun, 9 May 2021 11:28:49 -0400 Subject: [PATCH] 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)?, + }) + } } }