Skip to content

Commit

Permalink
Switch to derive macros for rkyv and bytecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Apr 30, 2022
1 parent ed41097 commit 05d4cb6
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 470 deletions.
61 changes: 5 additions & 56 deletions src/base/array_storage.rs
Expand Up @@ -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))]
)]
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
feature = "rkyv-serialize-no-std",
#[cfg_attr(
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);

Expand Down Expand Up @@ -273,59 +278,3 @@ unsafe impl<T: Scalar + Copy + bytemuck::Pod, const R: usize, const C: usize> by
for ArrayStorage<T, R, C>
{
}

#[cfg(feature = "rkyv-serialize-no-std")]
mod rkyv_impl {
use super::ArrayStorage;
use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize};

impl<T: Archive, const R: usize, const C: usize> Archive for ArrayStorage<T, R, C> {
type Archived = ArrayStorage<T::Archived, R, C>;
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<T: Serialize<S>, S: Fallible + ?Sized, const R: usize, const C: usize> Serialize<S>
for ArrayStorage<T, R, C>
{
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
self.0.serialize(serializer)
}
}

impl<T: Archive, D: Fallible + ?Sized, const R: usize, const C: usize>
Deserialize<ArrayStorage<T, R, C>, D> for ArrayStorage<T::Archived, R, C>
where
T::Archived: Deserialize<T, D>,
{
fn deserialize(&self, deserializer: &mut D) -> Result<ArrayStorage<T, R, C>, 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<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)
}
}
}
46 changes: 6 additions & 40 deletions src/base/dimension.rs
Expand Up @@ -13,6 +13,7 @@ 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 = "cuda", derive(cust_core::DeviceCopy))]
pub struct Dynamic {
value: usize,
Expand Down Expand Up @@ -198,6 +199,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<const R: usize>;

Expand Down Expand Up @@ -233,46 +239,6 @@ impl<'de, const D: usize> Deserialize<'de> for Const<D> {
}
}

#[cfg(feature = "rkyv-serialize-no-std")]
mod rkyv_impl {
use super::Const;
use rkyv::{Archive, Deserialize, Fallible, Serialize};

impl<const R: usize> Archive for Const<R> {
type Archived = Self;
type Resolver = ();

unsafe fn resolve(&self, _: usize, _: Self::Resolver, _: *mut Self::Archived) {}
}

impl<S: Fallible + ?Sized, const R: usize> Serialize<S> for Const<R> {
fn serialize(&self, _: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(())
}
}

impl<D: Fallible + ?Sized, const R: usize> Deserialize<Self, D> for Const<R> {
fn deserialize(&self, _: &mut D) -> Result<Self, D::Error> {
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<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;
}
Expand Down
65 changes: 5 additions & 60 deletions src/base/matrix.rs
Expand Up @@ -150,6 +150,11 @@ pub type MatrixCross<T, R1, C1, R2, C2> =
/// 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))]
)]
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
feature = "rkyv-serialize-no-std",
#[cfg_attr(
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Matrix<T, R, C, S> {
/// The data storage that contains all the matrix components. Disappointed?
Expand Down Expand Up @@ -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<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>;
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<T: Archive, R: Archive, C: Archive, S: Serialize<_S>, _S: Fallible + ?Sized> Serialize<_S>
for Matrix<T, R, C, S>
{
fn serialize(&self, serializer: &mut _S) -> Result<Self::Resolver, _S::Error> {
self.data.serialize(serializer)
}
}

impl<T: Archive, R: Archive, C: Archive, S: Archive, D: Fallible + ?Sized>
Deserialize<Matrix<T, R, C, S>, D>
for Matrix<T::Archived, R::Archived, C::Archived, S::Archived>
where
S::Archived: Deserialize<S, D>,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Matrix<T, R, C, S>, 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<T, R, C, S>
where
S: CheckBytes<__C>,
{
type Error = <S as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Matrix<T, R, C, S>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = S::check_bytes(addr_of!((*value).data), context)?;
Ok(&*value)
}
}
}

impl<T, R, C, S> Matrix<T, R, C, S> {
/// Creates a new matrix with the given data without statically checking that the matrix
/// dimension matches the storage dimension.
Expand Down
59 changes: 5 additions & 54 deletions src/base/unit.rs
Expand Up @@ -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))]
)]
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
feature = "rkyv-serialize-no-std",
#[cfg_attr(
// #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Unit<T> {
pub(crate) value: T,
Expand Down Expand Up @@ -58,60 +63,6 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit<T> {
}
}

#[cfg(feature = "rkyv-serialize-no-std")]
mod rkyv_impl {
use super::Unit;
use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize};

impl<T: Archive> Archive for Unit<T> {
type Archived = Unit<T::Archived>;
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<T: Serialize<S>, S: Fallible + ?Sized> Serialize<S> for Unit<T> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
self.value.serialize(serializer)
}
}

impl<T: Archive, D: Fallible + ?Sized> Deserialize<Unit<T>, D> for Unit<T::Archived>
where
T::Archived: Deserialize<T, D>,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Unit<T>, 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<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>>
where
Expand Down
5 changes: 5 additions & 0 deletions src/geometry/dual_quaternion.rs
Expand Up @@ -40,6 +40,11 @@ use simba::scalar::{ClosedNeg, RealField};
/// See <https://github.com/dimforge/nalgebra/issues/487>
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
)]
feature = "rkyv-serialize-no-std",
#[cfg_attr(
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct DualQuaternion<T> {
/// The real component of the quaternion
Expand Down

0 comments on commit 05d4cb6

Please sign in to comment.