From 0979896dbdd18a304d5dc130501b184e500fe86c Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Wed, 3 May 2017 19:02:30 -0700 Subject: [PATCH] Remove gratuitous indirection in serialization impls --- src/core/dimension.rs | 22 +++++++++++++++++++++- src/core/matrix.rs | 33 +++++++++++++++++++++++++++++++-- src/core/unit.rs | 22 +++++++++++++++++++++- src/geometry/orthographic.rs | 30 +++++++++++++++++++++++++++++- src/geometry/perspective.rs | 30 +++++++++++++++++++++++++++++- src/geometry/point.rs | 32 +++++++++++++++++++++++++++++++- src/geometry/quaternion.rs | 31 ++++++++++++++++++++++++++++++- src/geometry/rotation.rs | 30 +++++++++++++++++++++++++++++- src/geometry/transform.rs | 36 +++++++++++++++++++++++++++++++----- src/geometry/translation.rs | 30 +++++++++++++++++++++++++++++- tests/serde.rs | 11 ++++++++++- 11 files changed, 291 insertions(+), 16 deletions(-) diff --git a/src/core/dimension.rs b/src/core/dimension.rs index d3bd819d..ec8f3c57 100644 --- a/src/core/dimension.rs +++ b/src/core/dimension.rs @@ -7,9 +7,11 @@ use std::any::Any; use std::ops::{Add, Sub, Mul, Div}; use typenum::{self, Unsigned, UInt, B1, Bit, UTerm, Sum, Prod, Diff, Quot}; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct Dynamic { value: usize } @@ -24,6 +26,24 @@ impl Dynamic { } } +#[cfg(feature = "serde-serialize")] +impl Serialize for Dynamic { + fn serialize(&self, serializer: S) -> Result + where S: Serializer + { + self.value.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de> Deserialize<'de> for Dynamic { + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de> + { + usize::deserialize(deserializer).map(|x| Dynamic { value: x }) + } +} + /// Trait implemented by `Dynamic`. pub trait IsDynamic { } /// Trait implemented by `Dynamic` and type-level integers different from `U1`. diff --git a/src/core/matrix.rs b/src/core/matrix.rs index d87ddb3f..24db051a 100644 --- a/src/core/matrix.rs +++ b/src/core/matrix.rs @@ -7,6 +7,9 @@ use std::any::TypeId; use std::mem; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::{Ring, Real}; use core::{Scalar, Unit}; @@ -80,16 +83,42 @@ Matrix>::Alloc as Allocator>: /// some concrete types for `N` and a compatible data storage type `S`). #[repr(C)] #[derive(Hash, Debug, Clone, Copy)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct Matrix { /// The data storage that contains all the matrix components and informations about its number /// of rows and column (if needed). pub data: S, - #[cfg_attr(feature = "serde-serialize", serde(skip_serializing, skip_deserializing))] _phantoms: PhantomData<(N, R, C)> } +#[cfg(feature = "serde-serialize")] +impl Serialize for Matrix + where N: Scalar, + R: Dim, + C: Dim, + S: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.data.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, R, C, S> Deserialize<'de> for Matrix + where N: Scalar, + R: Dim, + C: Dim, + S: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de> + { + S::deserialize(deserializer).map(|x| Matrix { data: x, _phantoms: PhantomData }) + } +} + impl Matrix { /// Creates a new matrix with the given data without statically checking that the matrix /// dimension matches the storage dimension. diff --git a/src/core/unit.rs b/src/core/unit.rs index cc7ea1df..6051165d 100644 --- a/src/core/unit.rs +++ b/src/core/unit.rs @@ -2,6 +2,9 @@ use std::mem; use std::ops::{Neg, Deref}; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::SubsetOf; use alga::linear::NormedSpace; @@ -11,11 +14,28 @@ use alga::linear::NormedSpace; /// Use `.as_ref()` or `.unwrap()` to obtain the undelying value by-reference or by-move. #[repr(C)] #[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct Unit { value: T } +#[cfg(feature = "serde-serialize")] +impl Serialize for Unit { + fn serialize(&self, serializer: S) -> Result + where S: Serializer + { + self.value.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de> + { + T::deserialize(deserializer).map(|x| Unit { value: x }) + } +} + impl Unit { /// Normalize the given value and return it wrapped on a `Unit` structure. #[inline] diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 3936ccef..c907de6a 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -2,6 +2,9 @@ use quickcheck::{Arbitrary, Gen}; use rand::{Rand, Rng}; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::Real; use core::{Scalar, SquareMatrix, OwnedSquareMatrix, ColumnVector, OwnedColumnVector, MatrixArray}; @@ -14,11 +17,36 @@ use geometry::{PointBase, OwnedPoint}; /// A 3D orthographic projection stored as an homogeneous 4x4 matrix. #[derive(Debug, Clone, Copy)] // FIXME: Hash -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct OrthographicBase> { matrix: SquareMatrix } +#[cfg(feature = "serde-serialize")] +impl Serialize for OrthographicBase + where N: Scalar, + S: Storage, + SquareMatrix: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.matrix.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, S> Deserialize<'de> for OrthographicBase + where N: Scalar, + S: Storage, + SquareMatrix: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + SquareMatrix::deserialize(deserializer).map(|x| OrthographicBase { matrix: x }) + } +} + /// A 3D orthographic projection stored as a static homogeneous 4x4 matrix. pub type Orthographic3 = OrthographicBase>; diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 9c518546..ba1eced4 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -2,6 +2,9 @@ use quickcheck::{Arbitrary, Gen}; use rand::{Rand, Rng}; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::Real; use core::{Scalar, SquareMatrix, OwnedSquareMatrix, ColumnVector, OwnedColumnVector, MatrixArray}; @@ -14,11 +17,36 @@ use geometry::{PointBase, OwnedPoint}; /// A 3D perspective projection stored as an homogeneous 4x4 matrix. #[derive(Debug, Clone, Copy)] // FIXME: Hash -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct PerspectiveBase> { matrix: SquareMatrix } +#[cfg(feature = "serde-serialize")] +impl Serialize for PerspectiveBase + where N: Scalar, + S: Storage, + SquareMatrix: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.matrix.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, S> Deserialize<'de> for PerspectiveBase + where N: Scalar, + S: Storage, + SquareMatrix: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + SquareMatrix::deserialize(deserializer).map(|x| PerspectiveBase { matrix: x }) + } +} + /// A 3D perspective projection stored as a static homogeneous 4x4 matrix. pub type Perspective3 = PerspectiveBase>; diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 370fc3db..53f914a2 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -3,6 +3,9 @@ use std::fmt; use std::cmp::Ordering; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use core::{Scalar, ColumnVector, OwnedColumnVector}; use core::iter::{MatrixIter, MatrixIterMut}; use core::dimension::{DimName, DimNameSum, DimNameAdd, U1}; @@ -24,7 +27,6 @@ pub type OwnedPoint = PointBase>::Buffe /// A point in a n-dimensional euclidean space. #[repr(C)] #[derive(Hash, Debug)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct PointBase> { /// The coordinates of this point, i.e., the shift from the origin. pub coords: ColumnVector @@ -45,6 +47,34 @@ impl Clone for PointBase } } +#[cfg(feature = "serde-serialize")] +impl Serialize for PointBase + where N: Scalar, + D: DimName, + S: Storage, + ColumnVector: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.coords.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, D, S> Deserialize<'de> for PointBase + where N: Scalar, + D: DimName, + S: Storage, + ColumnVector: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + ColumnVector::deserialize(deserializer).map(|x| PointBase { coords: x }) + } +} + impl> PointBase { /// Creates a new point with the given coordinates. #[inline] diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 784dc20a..4a99acc3 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -2,6 +2,9 @@ use std::fmt; use num::Zero; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::Real; use core::{Unit, ColumnVector, OwnedColumnVector, MatrixSlice, MatrixSliceMut, SquareMatrix, @@ -22,12 +25,38 @@ pub type OwnedUnitQuaternionBase = UnitQuaternionBase> { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. pub coords: ColumnVector } +#[cfg(feature = "serde-serialize")] +impl Serialize for QuaternionBase + where N: Real, + S: Storage, + ColumnVector: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.coords.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, S> Deserialize<'de> for QuaternionBase + where N: Real, + S: Storage, + ColumnVector: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de> + { + ColumnVector::deserialize(deserializer).map(|x| QuaternionBase { coords: x }) + } +} + + impl Eq for QuaternionBase where N: Real + Eq, S: Storage { diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index a1063cbb..1429830a 100644 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -2,6 +2,9 @@ use num::{Zero, One}; use std::fmt; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::Real; use core::{SquareMatrix, Scalar, OwnedSquareMatrix}; @@ -16,11 +19,36 @@ pub type OwnedRotation = RotationBase>:: /// A rotation matrix. #[repr(C)] #[derive(Hash, Debug, Clone, Copy)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct RotationBase { matrix: SquareMatrix } +#[cfg(feature = "serde-serialize")] +impl Serialize for RotationBase + where N: Scalar, + D: DimName, + SquareMatrix: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.matrix.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, D, S> Deserialize<'de> for RotationBase + where N: Scalar, + D: DimName, + SquareMatrix: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + SquareMatrix::deserialize(deserializer).map(|x| RotationBase { matrix: x }) + } +} + impl> RotationBase where N: Scalar, S: Storage { diff --git a/src/geometry/transform.rs b/src/geometry/transform.rs index 23563d80..8d311375 100644 --- a/src/geometry/transform.rs +++ b/src/geometry/transform.rs @@ -3,6 +3,9 @@ use std::fmt::Debug; use std::marker::PhantomData; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::Field; use core::{Scalar, SquareMatrix, OwnedSquareMatrix}; @@ -53,17 +56,14 @@ where T1: TCategory, /// Tag representing the most general (not necessarily inversible) `Transform` type. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub enum TGeneral { } /// Tag representing the most general inversible `Transform` type. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub enum TProjective { } /// Tag representing an affine `Transform`. Its bottom-row is equal to `(0, 0 ... 0, 1)`. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub enum TAffine { } impl TCategory for TGeneral { @@ -159,14 +159,40 @@ pub type OwnedTransform /// 3D transformation. #[repr(C)] #[derive(Debug, Clone, Copy)] // FIXME: Hash -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct TransformBase, S, C: TCategory> { matrix: SquareMatrix, S>, - #[cfg_attr(feature = "serde-serialize", serde(skip_serializing, skip_deserializing))] _phantom: PhantomData } +#[cfg(feature = "serde-serialize")] +impl Serialize for TransformBase + where N: Scalar, + D: DimNameAdd, + C: TCategory, + SquareMatrix, S>: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.matrix.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, D, S, C> Deserialize<'de> for TransformBase + where N: Scalar, + D: DimNameAdd, + C: TCategory, + SquareMatrix, S>: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + SquareMatrix::deserialize(deserializer).map(|x| TransformBase { matrix: x, _phantom: PhantomData }) + } +} + // XXX: for some reasons, implementing Clone and Copy manually causes an ICEā€¦ impl Eq for TransformBase diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index ac7b1fd8..1580bcb7 100644 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -2,6 +2,9 @@ use num::{Zero, One}; use std::fmt; use approx::ApproxEq; +#[cfg(feature = "serde-serialize")] +use serde::{Serialize, Serializer, Deserialize, Deserializer}; + use alga::general::{Real, ClosedNeg}; use core::{Scalar, ColumnVector, OwnedSquareMatrix}; @@ -15,13 +18,38 @@ pub type OwnedTranslation = TranslationBase*/> { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is /// translated. pub vector: ColumnVector } +#[cfg(feature = "serde-serialize")] +impl Serialize for TranslationBase + where N: Scalar, + D: DimName, + ColumnVector: Serialize, +{ + fn serialize(&self, serializer: T) -> Result + where T: Serializer + { + self.vector.serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'de, N, D, S> Deserialize<'de> for TranslationBase + where N: Scalar, + D: DimName, + ColumnVector: Deserialize<'de>, +{ + fn deserialize(deserializer: T) -> Result + where T: Deserializer<'de> + { + ColumnVector::deserialize(deserializer).map(|x| TranslationBase { vector: x }) + } +} + impl TranslationBase where N: Scalar, S: Storage { diff --git a/tests/serde.rs b/tests/serde.rs index f55512c5..fadcc694 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -13,7 +13,8 @@ use na::{ IsometryMatrix3, Similarity3, SimilarityMatrix3, - Quaternion + Quaternion, + Unit, }; macro_rules! test_serde( @@ -45,3 +46,11 @@ test_serde!( serde_similarity_matrix3, SimilarityMatrix3; serde_quaternion, Quaternion; ); + +#[test] +fn serde_flat() { + // The actual storage is hidden behind three layers of wrapper types that shouldn't appear in serialized form. + let v = Unit::new_normalize(Quaternion::new(0., 0., 0., 1.)); + let serialized = serde_json::to_string(&v).unwrap(); + assert_eq!(serialized, "[0.0,0.0,1.0,0.0]"); +}