add bytecheck impls

This commit is contained in:
zyansheep 2022-03-21 21:03:17 -04:00 committed by Sébastien Crozet
parent 1b6f2b83f7
commit 24b97932e1
8 changed files with 173 additions and 5 deletions

View File

@ -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<T, R, C>
where
T: CheckBytes<__C>,
{
type Error = ArrayCheckError<ArrayCheckError<<T as CheckBytes<__C>>::Error>>;
unsafe fn check_bytes<'a>(
value: *const ArrayStorage<T, R, C>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = <[[T; R]; C] as CheckBytes<__C>>::check_bytes(addr_of!((*value).0), context)?;
Ok(&*value)
}
}
}

View File

@ -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<R> {
type Error = core::convert::Infallible;
unsafe fn check_bytes<'a>(
value: *const Const<R>,
_context: &mut __C,
) -> Result<&'a Self, Self::Error> {
Ok(&*value)
}
}
}
pub trait ToConst {
type Const: DimName;

View File

@ -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<T: Archive, R: Archive, C: Archive, S: Archive> Archive for Matrix<T, R, C, S> {
type Archived = Matrix<T::Archived, R::Archived, C::Archived, S::Archived>;
@ -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<T, R, C, S>
use super::Matrix;
impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> CheckBytes<__C> for Matrix<T, R, C, S>
where
S: CheckBytes<__C>,
{
@ -338,7 +342,7 @@ mod rkyv_impl {
value: *const Matrix<T, R, C, S>,
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)
}
}

View File

@ -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<T>
where
T: CheckBytes<__C>,
{
type Error = <T as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Unit<T>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = T::check_bytes(addr_of!((*value).value), context)?;
Ok(&*value)
}
}
}
#[cfg(feature = "cuda")]
unsafe impl<T: cust_core::DeviceCopy, R, C, S> cust_core::DeviceCopy for Unit<Matrix<T, R, C, S>>

View File

@ -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<T, R> {
Rotation(R),
Translation(T),
}
impl<T, R> fmt::Display for IsometryCheckBytesError<T, R> {
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<T: Error, R: Error> Error for IsometryCheckBytesError<T, R> {}
impl<__C: ?Sized, T: Scalar + CheckBytes<__C>, R: CheckBytes<__C>, const D: usize>
CheckBytes<__C> for Isometry<T, R, D>
where
T: CheckBytes<__C>,
{
type Error = IsometryCheckBytesError<
<Translation<T, D> as CheckBytes<__C>>::Error,
<R as CheckBytes<__C>>::Error,
>;
unsafe fn check_bytes<'a>(
value: *const Isometry<T, R, D>,
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::<T, D>::check_bytes(addr_of!((*value).translation), context)
.map_err(|e| IsometryCheckBytesError::Translation(e))?;
Ok(&*value)
}
}
}
impl<T: Scalar + hash::Hash, R: hash::Hash, const D: usize> hash::Hash for Isometry<T, R, D>
where

View File

@ -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<T>
where
T: CheckBytes<__C>,
{
type Error = <Vector4<T> as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Quaternion<T>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = Vector4::check_bytes(addr_of!((*value).coords), context)?;
Ok(&*value)
}
}
}
impl<T: SimdRealField> Quaternion<T>
where

View File

@ -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<T, D>
where
T: CheckBytes<__C>,
{
type Error = <SVector<T, D> as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Scale<T, D>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = SVector::<T, D>::check_bytes(addr_of!((*value).vector), context)?;
Ok(&*value)
}
}
}
impl<T: Scalar, const D: usize> Scale<T, D> {
/// Inverts `self`.

View File

@ -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<T, D>
where
T: CheckBytes<__C>,
{
type Error = <SVector<T, D> as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Translation<T, D>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = SVector::<T, D>::check_bytes(addr_of!((*value).vector), context)?;
Ok(&*value)
}
}
}
impl<T: Scalar, const D: usize> Translation<T, D> {
/// Creates a new translation from the given vector.