diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index d6dab40a..78cb6dcd 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -307,3 +307,25 @@ mod rkyv_impl { } } } +#[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 39e0459d..18c12803 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -257,6 +257,21 @@ mod rkyv_impl { } } } +#[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 63fbd0ce..2c481d37 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -293,7 +293,6 @@ mod rkyv_impl { use super::Matrix; use core::marker::PhantomData; use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - use bytecheck::CheckBytes; impl Archive for Matrix { type Archived = Matrix; @@ -326,10 +325,15 @@ mod rkyv_impl { }) } } +} +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use bytecheck::CheckBytes; + use std::ptr::addr_of; - impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> - CheckBytes<__C> - for Matrix + use super::Matrix; + + impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> CheckBytes<__C> for Matrix where S: CheckBytes<__C>, { @@ -338,7 +342,7 @@ mod rkyv_impl { value: *const Matrix, context: &mut __C, ) -> Result<&'a Self, Self::Error> { - let _ = S::check_bytes(::core::ptr::addr_of!((*value).data), context)?; + let _ = S::check_bytes(addr_of!((*value).data), context)?; Ok(&*value) } } diff --git a/src/base/unit.rs b/src/base/unit.rs index d82c67ec..6828aa02 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -90,6 +90,27 @@ mod rkyv_impl { } } } +#[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> diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 1b4eb26f..7f7175dd 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -121,6 +121,48 @@ mod rkyv_impl { } } } +#[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 diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 43f31e2e..71e38c4c 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -130,6 +130,28 @@ mod rkyv_impl { } } } +#[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 diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 23265bba..333b63cb 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -118,6 +118,27 @@ mod rkyv_impl { } } } +#[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/translation.rs b/src/geometry/translation.rs index e7dc5ee8..7af263bf 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -118,6 +118,27 @@ mod rkyv_impl { } } } +#[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.