Switch to distributive implementations

This commit is contained in:
David Koloski 2021-05-06 20:59:02 -04:00
parent b48b104d57
commit 38dd3345e0
8 changed files with 237 additions and 8 deletions

View File

@ -76,7 +76,7 @@ rand_distr = { version = "0.4", default-features = false, optional = true }
matrixmultiply = { version = "0.3", optional = true }
serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true }
abomonation = { version = "0.7", optional = true }
rkyv = { version = "~0.6.3", default-features = false, features = ["const_generics"], optional = true }
rkyv = { version = "~0.6.4", default-features = false, features = ["const_generics"], optional = true }
mint = { version = "0.5", optional = true }
glam = { version = "0.13", optional = true }
quickcheck = { version = "1", optional = true }

View File

@ -37,7 +37,6 @@ use crate::base::Scalar;
/// A array-based statically sized matrix data storage.
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))]
pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);
// TODO: remove this once the stdlib implements Default for arrays.
@ -303,3 +302,34 @@ where
self.as_slice().iter().fold(0, |acc, e| acc + e.extent())
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
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;
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit<Self::Archived>) {
self.0.resolve(
pos + rkyv::offset_of!(Self::Archived, 0),
resolver,
rkyv::project_struct!(out: Self::Archived => 0)
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Serialize<S>, S: rkyv::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> {
Ok(self.0.serialize(serializer)?)
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, D: rkyv::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)?))
}
}

View File

@ -200,7 +200,6 @@ dim_ops!(
);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))]
pub struct Const<const R: usize>;
/// Trait implemented exclusively by type-level integers.
@ -235,6 +234,29 @@ impl<'de, const D: usize> Deserialize<'de> for Const<D> {
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<const R: usize> Archive for Const<R> {
type Archived = Self;
type Resolver = ();
fn resolve(&self, _: usize, _: Self::Resolver, _: &mut core::mem::MaybeUninit<Self::Archived>) {
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<S: rkyv::Fallible + ?Sized, const R: usize> Serialize<S> for Const<R> {
fn serialize(&self, _: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(())
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<D: rkyv::Fallible + ?Sized, const R: usize> Deserialize<Self, D> for Const<R> {
fn deserialize(&self, _: &mut D) -> Result<Self, D::Error> {
Ok(Const)
}
}
pub trait ToConst {
type Const: DimName;
}

View File

@ -156,7 +156,6 @@ 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-no-std", derive(Archive, Deserialize, Serialize))]
pub struct Matrix<T, R, C, S> {
/// The data storage that contains all the matrix components. Disappointed?
///
@ -312,6 +311,40 @@ where
{
}
#[cfg(feature = "rkyv-serialize-no-std")]
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;
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit<Self::Archived>) {
self.data.resolve(
pos + rkyv::offset_of!(Self::Archived, data),
resolver,
rkyv::project_struct!(out: Self::Archived => data)
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, R: Archive, C: Archive, S: Serialize<_S>, _S: rkyv::Fallible + ?Sized> Serialize<_S> for Matrix<T, R, C, S> {
fn serialize(&self, serializer: &mut _S) -> Result<Self::Resolver, _S::Error> {
Ok(self.data.serialize(serializer)?)
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, R: Archive, C: Archive, S: Archive, D: rkyv::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,
})
}
}
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.

View File

@ -29,7 +29,6 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF
/// in their documentation, read their dedicated pages directly.
#[repr(transparent)]
#[derive(Clone, Hash, Debug, Copy)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))]
pub struct Unit<T> {
pub(crate) value: T,
}
@ -75,6 +74,39 @@ impl<T: Abomonation> Abomonation for Unit<T> {
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive> Archive for Unit<T> {
type Archived = Unit<T::Archived>;
type Resolver = T::Resolver;
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut ::core::mem::MaybeUninit<Self::Archived>) {
self.value.resolve(
pos + rkyv::offset_of!(Self::Archived, value),
resolver,
rkyv::project_struct!(out: Self::Archived => value),
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Serialize<S>, S: rkyv::Fallible + ?Sized> Serialize<S> for Unit<T> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(self.value.serialize(serializer)?)
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, D: rkyv::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)?,
})
}
}
impl<T, R, C, S> PartialEq for Unit<Matrix<T, R, C, S>>
where
T: Scalar + PartialEq,

View File

@ -71,7 +71,6 @@ use crate::geometry::{AbstractRotation, Point, Translation};
DefaultAllocator: Allocator<T, Const<D>>,
Owned<T, Const<D>>: Deserialize<'de>"))
)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Serialize, Deserialize))]
pub struct Isometry<T: Scalar, R, const D: usize> {
/// The pure rotational part of this isometry.
pub rotation: R,
@ -102,6 +101,55 @@ where
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
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);
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit<Self::Archived>) {
self.rotation.resolve(
pos + rkyv::offset_of!(Self::Archived, rotation),
resolver.0,
rkyv::project_struct!(out: Self::Archived => rotation)
);
self.translation.resolve(
pos + rkyv::offset_of!(Self::Archived, translation),
resolver.1,
rkyv::project_struct!(out: Self::Archived => translation)
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Scalar + Serialize<S>, R: Serialize<S>, S: rkyv::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)?,
))
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Scalar + Archive, R: Archive, _D: rkyv::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)?,
})
}
}
impl<T: Scalar + hash::Hash, R: hash::Hash, const D: usize> hash::Hash for Isometry<T, R, D>
where
Owned<T, Const<D>>: hash::Hash,

View File

@ -31,7 +31,6 @@ use crate::geometry::{Point3, Rotation};
/// that may be used as a rotation.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))]
pub struct Quaternion<T> {
/// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order.
pub coords: Vector4<T>,
@ -117,6 +116,39 @@ where
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive> Archive for Quaternion<T> {
type Archived = Quaternion<T::Archived>;
type Resolver = <Vector4<T> as Archive>::Resolver;
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit<Self::Archived>) {
self.coords.resolve(
pos + rkyv::offset_of!(Self::Archived, coords),
resolver,
rkyv::project_struct!(out: Self::Archived => coords)
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Serialize<S>, S: rkyv::Fallible + ?Sized> Serialize<S> for Quaternion<T> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(self.coords.serialize(serializer)?)
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, D: rkyv::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)?,
})
}
}
impl<T: SimdRealField> Quaternion<T>
where
T::Element: SimdRealField,

View File

@ -26,7 +26,6 @@ use crate::geometry::Point;
/// A translation.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "rkyv-serialize-no-std", derive(Archive, Deserialize, Serialize))]
pub struct Translation<T, const D: usize> {
/// The translation coordinates, i.e., how much is added to a point's coordinates when it is
/// translated.
@ -101,6 +100,39 @@ where
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
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;
fn resolve(&self, pos: usize, resolver: Self::Resolver, out: &mut core::mem::MaybeUninit<Self::Archived>) {
self.vector.resolve(
pos + rkyv::offset_of!(Self::Archived, vector),
resolver,
rkyv::project_struct!(out: Self::Archived => vector)
);
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Serialize<S>, S: rkyv::Fallible + ?Sized, const D: usize> Serialize<S> for Translation<T, D> {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(self.vector.serialize(serializer)?)
}
}
#[cfg(feature = "rkyv-serialize-no-std")]
impl<T: Archive, _D: rkyv::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)?,
})
}
}
impl<T: Scalar, const D: usize> Translation<T, D> {
/// Creates a new translation from the given vector.
#[inline]