Switch to derive macros for rkyv and bytecheck

This commit is contained in:
Sébastien Crozet 2022-04-30 10:32:10 +02:00 committed by Saurabh
parent edb1d1b86f
commit 27ce8ee40d
15 changed files with 77 additions and 470 deletions

View File

@ -27,6 +27,11 @@ use std::mem;
/// A array-based statically sized matrix data storage. /// A array-based statically sized matrix data storage.
#[repr(transparent)] #[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]); pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);
@ -273,59 +278,3 @@ unsafe impl<T: Scalar + Copy + bytemuck::Pod, const R: usize, const C: usize> by
for ArrayStorage<T, R, C> 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)
}
}
}

View File

@ -13,6 +13,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// Dim of dynamically-sized algebraic entities. /// Dim of dynamically-sized algebraic entities.
#[derive(Clone, Copy, Eq, PartialEq, Debug)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Dynamic { pub struct Dynamic {
value: usize, value: usize,
@ -198,6 +203,11 @@ dim_ops!(
); );
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Const<const R: usize>; pub struct Const<const R: usize>;
@ -233,46 +243,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 { pub trait ToConst {
type Const: DimName; type Const: DimName;
} }

View File

@ -150,6 +150,11 @@ pub type MatrixCross<T, R1, C1, R2, C2> =
/// some concrete types for `T` and a compatible data storage type `S`). /// some concrete types for `T` and a compatible data storage type `S`).
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Matrix<T, R, C, S> { pub struct Matrix<T, R, C, S> {
/// The data storage that contains all the matrix components. Disappointed? /// 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<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> { impl<T, R, C, S> Matrix<T, R, C, S> {
/// Creates a new matrix with the given data without statically checking that the matrix /// Creates a new matrix with the given data without statically checking that the matrix
/// dimension matches the storage dimension. /// dimension matches the storage dimension.

View File

@ -21,6 +21,11 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF
/// in their documentation, read their dedicated pages directly. /// in their documentation, read their dedicated pages directly.
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Hash, Copy)] #[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))] // #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Unit<T> { pub struct Unit<T> {
pub(crate) value: T, pub(crate) value: T,
@ -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")] #[cfg(feature = "cuda")]
unsafe impl<T: cust_core::DeviceCopy, R, C, S> cust_core::DeviceCopy for Unit<Matrix<T, R, C, S>> unsafe impl<T: cust_core::DeviceCopy, R, C, S> cust_core::DeviceCopy for Unit<Matrix<T, R, C, S>>
where where

View File

@ -40,6 +40,11 @@ use simba::scalar::{ClosedNeg, RealField};
/// See <https://github.com/dimforge/nalgebra/issues/487> /// See <https://github.com/dimforge/nalgebra/issues/487>
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct DualQuaternion<T> { pub struct DualQuaternion<T> {
/// The real component of the quaternion /// The real component of the quaternion

View File

@ -66,6 +66,11 @@ use crate::geometry::{AbstractRotation, Point, Translation};
Owned<T, Const<D>>: Deserialize<'de>, Owned<T, Const<D>>: Deserialize<'de>,
T: Scalar")) 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<T, R, const D: usize> { pub struct Isometry<T, R, const D: usize> {
/// The pure rotational part of this isometry. /// The pure rotational part of this isometry.
pub rotation: R, pub rotation: R,
@ -73,97 +78,6 @@ pub struct Isometry<T, R, const D: usize> {
pub translation: Translation<T, D>, pub translation: Translation<T, D>,
} }
#[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<T: Scalar + Archive, R: Archive, const D: usize> Archive for Isometry<T, R, D>
where
T::Archived: Scalar,
{
type Archived = Isometry<T::Archived, R::Archived, D>;
type Resolver = (R::Resolver, <Translation<T, D> 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<T: Scalar + Serialize<S>, R: Serialize<S>, S: Fallible + ?Sized, const D: usize>
Serialize<S> for Isometry<T, R, D>
where
T::Archived: Scalar,
{
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Ok((
self.rotation.serialize(serializer)?,
self.translation.serialize(serializer)?,
))
}
}
impl<T: Scalar + Archive, R: Archive, _D: Fallible + ?Sized, const D: usize>
Deserialize<Isometry<T, R, D>, _D> for Isometry<T::Archived, R::Archived, D>
where
T::Archived: Scalar + Deserialize<T, _D>,
R::Archived: Scalar + Deserialize<R, _D>,
{
fn deserialize(&self, deserializer: &mut _D) -> Result<Isometry<T, R, D>, _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<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> impl<T: Scalar + hash::Hash, R: hash::Hash, const D: usize> hash::Hash for Isometry<T, R, D>
where where
Owned<T, Const<D>>: hash::Hash, Owned<T, Const<D>>: hash::Hash,

View File

@ -19,6 +19,11 @@ use crate::geometry::{Point3, Projective3};
/// A 3D orthographic projection stored as a homogeneous 4x4 matrix. /// A 3D orthographic projection stored as a homogeneous 4x4 matrix.
#[repr(C)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Orthographic3<T> { pub struct Orthographic3<T> {

View File

@ -20,6 +20,11 @@ use crate::geometry::{Point3, Projective3};
/// A 3D perspective projection stored as a homogeneous 4x4 matrix. /// A 3D perspective projection stored as a homogeneous 4x4 matrix.
#[repr(C)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Perspective3<T> { pub struct Perspective3<T> {

View File

@ -36,6 +36,11 @@ use std::mem::MaybeUninit;
/// of said transformations for details. /// of said transformations for details.
#[repr(C)] #[repr(C)]
#[derive(Clone)] #[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<T: Scalar, D: DimName> pub struct OPoint<T: Scalar, D: DimName>
where where
DefaultAllocator: Allocator<T, D>, DefaultAllocator: Allocator<T, D>,

View File

@ -23,6 +23,11 @@ use crate::geometry::{Point3, Rotation};
/// that may be used as a rotation. /// that may be used as a rotation.
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct Quaternion<T> { pub struct Quaternion<T> {
/// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. /// 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<T: Archive> Archive for Quaternion<T> {
type Archived = Quaternion<T::Archived>;
type Resolver = <Vector4<T> 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<T: Serialize<S>, S: Fallible + ?Sized> Serialize<S> for Quaternion<T> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
self.coords.serialize(serializer)
}
}
impl<T: Archive, D: Fallible + ?Sized> Deserialize<Quaternion<T>, D> for Quaternion<T::Archived>
where
T::Archived: Deserialize<T, D>,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Quaternion<T>, 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<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> impl<T: SimdRealField> Quaternion<T>
where where
T::Element: SimdRealField, T::Element: SimdRealField,

View File

@ -49,6 +49,11 @@ use crate::geometry::Point;
/// * [Conversion to a matrix <span style="float:right;">`matrix`, `to_homogeneous`…</span>](#conversion-to-a-matrix) /// * [Conversion to a matrix <span style="float:right;">`matrix`, `to_homogeneous`…</span>](#conversion-to-a-matrix)
/// ///
#[repr(C)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Rotation<T, const D: usize> { pub struct Rotation<T, const D: usize> {

View File

@ -17,6 +17,11 @@ use crate::geometry::Point;
/// A scale which supports non-uniform scaling. /// A scale which supports non-uniform scaling.
#[repr(C)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Scale<T, const D: usize> { pub struct Scale<T, const D: usize> {
@ -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<T: Archive, const D: usize> Archive for Scale<T, D> {
type Archived = Scale<T::Archived, D>;
type Resolver = <SVector<T, D> 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<T: Serialize<S>, S: Fallible + ?Sized, const D: usize> Serialize<S> for Scale<T, D> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
self.vector.serialize(serializer)
}
}
impl<T: Archive, _D: Fallible + ?Sized, const D: usize> Deserialize<Scale<T, D>, _D>
for Scale<T::Archived, D>
where
T::Archived: Deserialize<T, _D>,
{
fn deserialize(&self, deserializer: &mut _D) -> Result<Scale<T, D>, _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<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> { impl<T: Scalar, const D: usize> Scale<T, D> {
/// Inverts `self`. /// Inverts `self`.
/// ///

View File

@ -34,6 +34,11 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation};
DefaultAllocator: Allocator<T, Const<D>>, DefaultAllocator: Allocator<T, Const<D>>,
Owned<T, Const<D>>: Deserialize<'de>")) Owned<T, Const<D>>: 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<T, R, const D: usize> { pub struct Similarity<T, R, const D: usize> {
/// The part of this similarity that does not include the scaling factor. /// The part of this similarity that does not include the scaling factor.
pub isometry: Isometry<T, R, D>, pub isometry: Isometry<T, R, D>,

View File

@ -17,6 +17,11 @@ use crate::geometry::Point;
/// A translation. /// A translation.
#[repr(C)] #[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))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Translation<T, const D: usize> { pub struct Translation<T, const D: usize> {
@ -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<T: Archive, const D: usize> Archive for Translation<T, D> {
type Archived = Translation<T::Archived, D>;
type Resolver = <SVector<T, D> 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<T: Serialize<S>, S: Fallible + ?Sized, const D: usize> Serialize<S> for Translation<T, D> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
self.vector.serialize(serializer)
}
}
impl<T: Archive, _D: Fallible + ?Sized, const D: usize> Deserialize<Translation<T, D>, _D>
for Translation<T::Archived, D>
where
T::Archived: Deserialize<T, _D>,
{
fn deserialize(&self, deserializer: &mut _D) -> Result<Translation<T, D>, _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<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> { impl<T: Scalar, const D: usize> Translation<T, D> {
/// Creates a new translation from the given vector. /// Creates a new translation from the given vector.
#[inline] #[inline]

View File

@ -78,12 +78,13 @@ an optimized set of tools for computer graphics and physics. Those features incl
unused_mut, unused_mut,
unused_parens, unused_parens,
unused_qualifications, unused_qualifications,
unused_results,
rust_2018_idioms, rust_2018_idioms,
rust_2018_compatibility, rust_2018_compatibility,
future_incompatible, future_incompatible,
missing_copy_implementations 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( #![doc(
html_favicon_url = "https://nalgebra.org/img/favicon.ico", html_favicon_url = "https://nalgebra.org/img/favicon.ico",
html_root_url = "https://docs.rs/nalgebra/0.25.0" html_root_url = "https://docs.rs/nalgebra/0.25.0"