From 27ce8ee40d89beee7174cfe5e16a1ebd63557a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 30 Apr 2022 10:32:10 +0200 Subject: [PATCH] Switch to derive macros for rkyv and bytecheck --- src/base/array_storage.rs | 61 ++------------------- src/base/dimension.rs | 50 ++++------------- src/base/matrix.rs | 65 ++-------------------- src/base/unit.rs | 59 ++------------------ src/geometry/dual_quaternion.rs | 5 ++ src/geometry/isometry.rs | 96 ++------------------------------- src/geometry/orthographic.rs | 5 ++ src/geometry/perspective.rs | 5 ++ src/geometry/point.rs | 5 ++ src/geometry/quaternion.rs | 61 ++------------------- src/geometry/rotation.rs | 5 ++ src/geometry/scale.rs | 61 ++------------------- src/geometry/similarity.rs | 5 ++ src/geometry/translation.rs | 61 ++------------------- src/lib.rs | 3 +- 15 files changed, 77 insertions(+), 470 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 78cb6dcd..3bc71e1a 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -27,6 +27,11 @@ 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) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct ArrayStorage(pub [[T; R]; C]); @@ -273,59 +278,3 @@ unsafe impl by for ArrayStorage { } - -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::ArrayStorage; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for ArrayStorage { - type Archived = ArrayStorage; - type Resolver = <[[T; R]; C] as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.0); - self.0.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const R: usize, const C: usize> Serialize - for ArrayStorage - { - fn serialize(&self, serializer: &mut S) -> Result { - 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)?)) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::{ArrayCheckError, CheckBytes}; - - use super::ArrayStorage; - - impl<__C: ?Sized, T, const R: usize, const C: usize> CheckBytes<__C> for ArrayStorage - where - T: CheckBytes<__C>, - { - type Error = ArrayCheckError>::Error>>; - unsafe fn check_bytes<'a>( - value: *const ArrayStorage, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = <[[T; R]; C] as CheckBytes<__C>>::check_bytes(addr_of!((*value).0), context)?; - Ok(&*value) - } - } -} diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 18c12803..4be97586 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -13,6 +13,11 @@ 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, @@ -198,6 +203,11 @@ 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; @@ -233,46 +243,6 @@ impl<'de, const D: usize> Deserialize<'de> for Const { } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Const; - use rkyv::{Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Const { - type Archived = Self; - type Resolver = (); - - unsafe fn resolve(&self, _: usize, _: Self::Resolver, _: *mut Self::Archived) {} - } - - impl Serialize for Const { - fn serialize(&self, _: &mut S) -> Result { - Ok(()) - } - } - - impl Deserialize for Const { - fn deserialize(&self, _: &mut D) -> Result { - Ok(Const) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use bytecheck::CheckBytes; - - use super::Const; - impl<__C: ?Sized, const R: usize> CheckBytes<__C> for Const { - type Error = core::convert::Infallible; - unsafe fn check_bytes<'a>( - value: *const Const, - _context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - Ok(&*value) - } - } -} - pub trait ToConst { type Const: DimName; } diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 2c481d37..8f8786c1 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -150,6 +150,11 @@ 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(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? @@ -288,66 +293,6 @@ where { } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Matrix; - use core::marker::PhantomData; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Matrix { - type Archived = Matrix; - type Resolver = S::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.data); - self.data.resolve(pos + fp, resolver, fo); - } - } - - impl, _S: Fallible + ?Sized> Serialize<_S> - for Matrix - { - fn serialize(&self, serializer: &mut _S) -> Result { - self.data.serialize(serializer) - } - } - - 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, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use bytecheck::CheckBytes; - use std::ptr::addr_of; - - use super::Matrix; - - impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> CheckBytes<__C> for Matrix - where - S: CheckBytes<__C>, - { - type Error = >::Error; - unsafe fn check_bytes<'a>( - value: *const Matrix, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = S::check_bytes(addr_of!((*value).data), context)?; - Ok(&*value) - } - } -} - 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 6828aa02..6fc00092 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -21,6 +21,11 @@ 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) +)] // #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Unit { pub(crate) value: T, @@ -58,60 +63,6 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Unit; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Unit { - type Archived = Unit; - type Resolver = T::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.value); - self.value.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized> Serialize for Unit { - fn serialize(&self, serializer: &mut S) -> Result { - self.value.serialize(serializer) - } - } - - 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)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use super::Unit; - impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Unit - where - T: CheckBytes<__C>, - { - type Error = >::Error; - unsafe fn check_bytes<'a>( - value: *const Unit, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = T::check_bytes(addr_of!((*value).value), context)?; - Ok(&*value) - } - } -} - #[cfg(feature = "cuda")] unsafe impl cust_core::DeviceCopy for Unit> where diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 719ae13d..6f1b7053 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -40,6 +40,11 @@ use simba::scalar::{ClosedNeg, RealField}; /// See #[repr(C)] #[derive(Debug, Copy, Clone)] +#[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 DualQuaternion { /// The real component of the quaternion diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 7f7175dd..92169742 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -66,6 +66,11 @@ use crate::geometry::{AbstractRotation, Point, Translation}; Owned>: Deserialize<'de>, T: Scalar")) )] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct Isometry { /// The pure rotational part of this isometry. pub rotation: R, @@ -73,97 +78,6 @@ pub struct Isometry { pub translation: Translation, } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Isometry; - use crate::{base::Scalar, geometry::Translation}; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Isometry - where - T::Archived: Scalar, - { - type Archived = Isometry; - type Resolver = (R::Resolver, as Archive>::Resolver); - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.rotation); - self.rotation.resolve(pos + fp, resolver.0, fo); - let (fp, fo) = out_field!(out.translation); - self.translation.resolve(pos + fp, resolver.1, fo); - } - } - - 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)?, - )) - } - } - - 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)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use crate::{Isometry, Scalar, Translation}; - use bytecheck::CheckBytes; - use std::{error::Error, fmt, ptr::addr_of}; - - #[derive(Debug)] - pub enum IsometryCheckBytesError { - Rotation(R), - Translation(T), - } - impl fmt::Display for IsometryCheckBytesError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Rotation(_) => write!(f, "failed to check bytes of isometry rotation"), - Self::Translation(_) => write!(f, "failed to check bytes of isometry translation"), - } - } - } - impl Error for IsometryCheckBytesError {} - - impl<__C: ?Sized, T: Scalar + CheckBytes<__C>, R: CheckBytes<__C>, const D: usize> - CheckBytes<__C> for Isometry - where - T: CheckBytes<__C>, - { - type Error = IsometryCheckBytesError< - as CheckBytes<__C>>::Error, - >::Error, - >; - unsafe fn check_bytes<'a>( - value: *const Isometry, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = R::check_bytes(addr_of!((*value).rotation), context) - .map_err(|e| IsometryCheckBytesError::Rotation(e))?; - let _ = Translation::::check_bytes(addr_of!((*value).translation), context) - .map_err(|e| IsometryCheckBytesError::Translation(e))?; - Ok(&*value) - } - } -} - impl hash::Hash for Isometry where Owned>: hash::Hash, diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 1119d4e3..7348f676 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -19,6 +19,11 @@ use crate::geometry::{Point3, Projective3}; /// A 3D orthographic projection stored as a homogeneous 4x4 matrix. #[repr(C)] +#[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))] #[derive(Copy, Clone)] pub struct Orthographic3 { diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 8ebab3e4..351960bb 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -20,6 +20,11 @@ use crate::geometry::{Point3, Projective3}; /// A 3D perspective projection stored as a homogeneous 4x4 matrix. #[repr(C)] +#[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))] #[derive(Copy, Clone)] pub struct Perspective3 { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index cdc590fa..306c18e5 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -36,6 +36,11 @@ use std::mem::MaybeUninit; /// of said transformations for details. #[repr(C)] #[derive(Clone)] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct OPoint where DefaultAllocator: Allocator, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 71e38c4c..f38dca6f 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -23,6 +23,11 @@ use crate::geometry::{Point3, Rotation}; /// that may be used as a rotation. #[repr(C)] #[derive(Copy, Clone)] +#[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 Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. @@ -97,62 +102,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Quaternion; - use crate::base::Vector4; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Quaternion { - type Archived = Quaternion; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.coords); - self.coords.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized> Serialize for Quaternion { - fn serialize(&self, serializer: &mut S) -> Result { - self.coords.serialize(serializer) - } - } - - 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)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use super::Quaternion; - use crate::Vector4; - impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Quaternion - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Quaternion, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = Vector4::check_bytes(addr_of!((*value).coords), context)?; - Ok(&*value) - } - } -} - impl Quaternion where T::Element: SimdRealField, diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 4dbcfb43..2a8bf427 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -49,6 +49,11 @@ use crate::geometry::Point; /// * [Conversion to a matrix `matrix`, `to_homogeneous`…](#conversion-to-a-matrix) /// #[repr(C)] +#[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))] #[derive(Copy, Clone)] pub struct Rotation { diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 333b63cb..37da1ef0 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -17,6 +17,11 @@ use crate::geometry::Point; /// A scale which supports non-uniform scaling. #[repr(C)] +#[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))] #[derive(Copy, Clone)] pub struct Scale { @@ -84,62 +89,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Scale; - use crate::base::SVector; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Scale { - type Archived = Scale; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.vector); - self.vector.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const D: usize> Serialize for Scale { - fn serialize(&self, serializer: &mut S) -> Result { - self.vector.serialize(serializer) - } - } - - impl Deserialize, _D> - for Scale - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Scale { - vector: self.vector.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use crate::{SVector, Scale}; - impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Scale - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Scale, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; - Ok(&*value) - } - } -} - impl Scale { /// Inverts `self`. /// diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 9658685e..8c38ff1e 100755 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -34,6 +34,11 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation}; DefaultAllocator: Allocator>, Owned>: Deserialize<'de>")) )] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[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/src/geometry/translation.rs b/src/geometry/translation.rs index 7af263bf..bef66f68 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -17,6 +17,11 @@ use crate::geometry::Point; /// A translation. #[repr(C)] +#[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))] #[derive(Copy, Clone)] pub struct Translation { @@ -84,62 +89,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Translation; - use crate::base::SVector; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Translation { - type Archived = Translation; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.vector); - self.vector.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const D: usize> Serialize for Translation { - fn serialize(&self, serializer: &mut S) -> Result { - self.vector.serialize(serializer) - } - } - - 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)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use crate::{SVector, Translation}; - impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Translation - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Translation, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; - Ok(&*value) - } - } -} - impl Translation { /// Creates a new translation from the given vector. #[inline] diff --git a/src/lib.rs b/src/lib.rs index 92b28dcb..1ee1a3ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,12 +78,13 @@ an optimized set of tools for computer graphics and physics. Those features incl unused_mut, unused_parens, unused_qualifications, - unused_results, rust_2018_idioms, rust_2018_compatibility, future_incompatible, 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))] #![doc( html_favicon_url = "https://nalgebra.org/img/favicon.ico", html_root_url = "https://docs.rs/nalgebra/0.25.0"