From 35eafa03371e126bff28f035a7bba61af08732cb Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 02:39:17 +0100 Subject: [PATCH 01/13] Add unit test for more general PartialEq trait impl. for Matrix type --- tests/core/matrix.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index e4fb4d0c..163049ab 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1,7 +1,7 @@ use num::{One, Zero}; use std::cmp::Ordering; -use na::dimension::{U15, U8}; +use na::dimension::{U15, U8, U2}; use na::{ self, DMatrix, DVector, Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4, Matrix4, Matrix4x3, Matrix4x5, Matrix5, Matrix6, MatrixMN, RowVector3, RowVector4, RowVector5, @@ -1047,3 +1047,28 @@ mod finite_dim_inner_space_tests { true } } + +#[test] +fn partial_eq() { + let dynamic_mat = DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]); + let static_mat = Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8); + + let dyn_static_slice = dynamic_mat.fixed_slice::(0, 0); + let dyn_dyn_slice = dynamic_mat.slice((0, 0), (2, 2)); + let static_static_slice = static_mat.fixed_slice::(0, 0); + let static_dyn_slice = static_mat.slice((0, 0), (2, 2)); + + let larger_slice = static_mat.slice((0, 0), (2, 3)); + + assert_eq!(dynamic_mat, static_mat); + + assert_eq!(dyn_static_slice, dyn_dyn_slice); + assert_eq!(dyn_static_slice, static_static_slice); + assert_eq!(dyn_static_slice, static_dyn_slice); + assert_eq!(dyn_dyn_slice, static_static_slice); + assert_eq!(dyn_dyn_slice, static_dyn_slice); + assert_eq!(static_static_slice, static_dyn_slice); + + assert_ne!(dynamic_mat, static_dyn_slice); + assert_ne!(static_dyn_slice, larger_slice); +} From 215df7948e1e9373e90b4ebbf998ce2bd70afd65 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 03:01:31 +0100 Subject: [PATCH 02/13] Modify PartialEq test to take into account typenum UInt dimensions --- tests/core/matrix.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 163049ab..74fc6153 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -7,6 +7,9 @@ use na::{ Matrix4x3, Matrix4x5, Matrix5, Matrix6, MatrixMN, RowVector3, RowVector4, RowVector5, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6, }; +use typenum::{UInt, UTerm}; +use serde_json::error::Category::Data; +use typenum::bit::{B0, B1}; #[test] fn iter() { @@ -1053,6 +1056,9 @@ fn partial_eq() { let dynamic_mat = DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]); let static_mat = Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8); + type TypeNumInt = typenum::UInt, B1>; + let typenum_static_mat = MatrixMN::::new(1, 2, 3, 4, 5, 6, 7, 8); + let dyn_static_slice = dynamic_mat.fixed_slice::(0, 0); let dyn_dyn_slice = dynamic_mat.slice((0, 0), (2, 2)); let static_static_slice = static_mat.fixed_slice::(0, 0); @@ -1061,6 +1067,8 @@ fn partial_eq() { let larger_slice = static_mat.slice((0, 0), (2, 3)); assert_eq!(dynamic_mat, static_mat); + assert_eq!(dynamic_mat, typenum_static_mat); + assert_eq!(typenum_static_mat, static_mat); assert_eq!(dyn_static_slice, dyn_dyn_slice); assert_eq!(dyn_static_slice, static_static_slice); From 39a433c0e798da14e957a4196ac0b342ad1b6ae8 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 03:02:05 +0100 Subject: [PATCH 03/13] Broader PartialEq implementation for types implementing Dim trait --- src/base/dimension.rs | 45 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 5d1d1bd9..cabd8a1c 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -14,7 +14,7 @@ use typenum::{ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Dim of dynamically-sized algebraic entities. -#[derive(Clone, Copy, Eq, PartialEq, Debug)] +#[derive(Clone, Copy, Eq, Debug)] pub struct Dynamic { value: usize, } @@ -107,6 +107,12 @@ impl Sub for Dynamic { } } +impl PartialEq for Dynamic { + fn eq(&self, other: &T) -> bool { + self.value() == other.value() + } +} + /* * * Operations. @@ -244,7 +250,7 @@ impl NamedDim for typenum::U1 { macro_rules! named_dimension( ($($D: ident),* $(,)*) => {$( /// A type level dimension. - #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] + #[derive(Debug, Copy, Clone, Hash, Eq)] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct $D; @@ -280,6 +286,12 @@ macro_rules! named_dimension( } impl IsNotStaticOne for $D { } + + impl PartialEq for $D { + fn eq(&self, other: &T) -> bool { + self.value() == other.value() + } + } )*} ); @@ -367,6 +379,23 @@ impl< { } +impl< + T: Dim, + A: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + B: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + C: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + D: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + E: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + F: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + G: Bit + Any + Debug + Copy + PartialEq + Send + Sync, + > PartialEq + for UInt, A>, B>, C>, D>, E>, F>, G> +{ + fn eq(&self, other: &T) -> bool { + self.value() == other.value() + } +} + impl NamedDim for UInt { @@ -408,3 +437,15 @@ impl { } + +impl< + T: Dim, + U: Unsigned + DimName, + B: Bit + Any + Debug + Copy + PartialEq + Send + Sync + > PartialEq + for UInt +{ + fn eq(&self, other: &T) -> bool { + self.value() == other.value() + } +} From 2b8410e08bc0432868cacae8e9c4655edbb7c893 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 03:06:00 +0100 Subject: [PATCH 04/13] Fix bug - PartialEq for Matrix no longer panics when shapes do not match --- src/base/matrix.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 95dda818..449d76cd 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1350,12 +1350,11 @@ where S: Storage, { #[inline] - fn eq(&self, right: &Matrix) -> bool { - assert!( - self.shape() == right.shape(), - "Matrix equality test dimension mismatch." - ); - self.iter().zip(right.iter()).all(|(l, r)| l == r) + fn eq(&self, right: &Matrix) -> bool { + if self.shape() == right.shape() { + return self.iter().zip(right.iter()).all(|(l, r)| l == r) + } + false } } From 50ea55e877c50c5ea126fa265fee29492f437b88 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 03:06:36 +0100 Subject: [PATCH 05/13] Modify PartialEq for Matrix to allow comparison with all types of Matrix --- src/base/matrix.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 449d76cd..5d60b970 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1344,10 +1344,15 @@ where S: Storage, {} -impl PartialEq for Matrix +impl PartialEq> for Matrix where - N: Scalar, + N: Scalar + PartialEq, + C: Dim + PartialEq, + C2: Dim, + R: Dim + PartialEq, + R2: Dim, S: Storage, + S2: Storage { #[inline] fn eq(&self, right: &Matrix) -> bool { From 12c2efdb668f534ef3899d3b089b7982868b81ed Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 16:21:32 +0100 Subject: [PATCH 06/13] Separate test for shape mismatch and for PartialEq on different types --- tests/core/matrix.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 74fc6153..2dbac323 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1,7 +1,7 @@ use num::{One, Zero}; use std::cmp::Ordering; -use na::dimension::{U15, U8, U2}; +use na::dimension::{U15, U8, U2, U4}; use na::{ self, DMatrix, DVector, Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4, Matrix4, Matrix4x3, Matrix4x5, Matrix5, Matrix6, MatrixMN, RowVector3, RowVector4, RowVector5, @@ -1052,7 +1052,16 @@ mod finite_dim_inner_space_tests { } #[test] -fn partial_eq() { +fn partial_eq_shape_mismatch() { + let a = Matrix2::new(1, 2, 3, 4); + let b = Matrix2x3::new(1, 2, 3, 4, 5, 6); + assert_ne!(a, b); + assert_ne!(b, a); +} + +#[test] +fn partial_eq_different_types() { + // Ensure comparability of several types of Matrices let dynamic_mat = DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]); let static_mat = Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8); From 703ba3e716f8a203fce349a36c4f6b925e004cc3 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 16:23:50 +0100 Subject: [PATCH 07/13] Refactor var names and add symmetric assert_eq!()s in test; add TODO --- tests/core/matrix.rs | 56 ++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 2dbac323..6fad5e8c 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1065,27 +1065,47 @@ fn partial_eq_different_types() { let dynamic_mat = DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]); let static_mat = Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8); - type TypeNumInt = typenum::UInt, B1>; - let typenum_static_mat = MatrixMN::::new(1, 2, 3, 4, 5, 6, 7, 8); + let mut typenum_static_mat = MatrixMN::::zeros(); + let mut slice = typenum_static_mat.slice_mut((0,0), (2, 4)); + slice += static_mat; - let dyn_static_slice = dynamic_mat.fixed_slice::(0, 0); - let dyn_dyn_slice = dynamic_mat.slice((0, 0), (2, 2)); - let static_static_slice = static_mat.fixed_slice::(0, 0); - let static_dyn_slice = static_mat.slice((0, 0), (2, 2)); + let fslice_of_dmat = dynamic_mat.fixed_slice::(0, 0); + let dslice_of_dmat = dynamic_mat.slice((0, 0), (2, 2)); + let fslice_of_smat = static_mat.fixed_slice::(0, 0); + let dslice_of_smat = static_mat.slice((0, 0), (2, 2)); - let larger_slice = static_mat.slice((0, 0), (2, 3)); - assert_eq!(dynamic_mat, static_mat); - assert_eq!(dynamic_mat, typenum_static_mat); - assert_eq!(typenum_static_mat, static_mat); + assert_eq!(static_mat, dynamic_mat); - assert_eq!(dyn_static_slice, dyn_dyn_slice); - assert_eq!(dyn_static_slice, static_static_slice); - assert_eq!(dyn_static_slice, static_dyn_slice); - assert_eq!(dyn_dyn_slice, static_static_slice); - assert_eq!(dyn_dyn_slice, static_dyn_slice); - assert_eq!(static_static_slice, static_dyn_slice); + assert_eq!(dynamic_mat, slice); + assert_eq!(slice, dynamic_mat); + + assert_eq!(static_mat, slice); + assert_eq!(slice, static_mat); + + assert_eq!(fslice_of_dmat, dslice_of_dmat); + assert_eq!(dslice_of_dmat, fslice_of_dmat); + + assert_eq!(fslice_of_dmat, fslice_of_smat); + assert_eq!(fslice_of_smat, fslice_of_dmat); + + assert_eq!(fslice_of_dmat, dslice_of_smat); + assert_eq!(dslice_of_smat, fslice_of_dmat); + + assert_eq!(dslice_of_dmat, fslice_of_smat); + assert_eq!(fslice_of_smat, dslice_of_dmat); + + assert_eq!(dslice_of_dmat, dslice_of_smat); + assert_eq!(dslice_of_smat, dslice_of_dmat); + + assert_eq!(fslice_of_smat, dslice_of_smat); + assert_eq!(dslice_of_smat, fslice_of_smat); + + assert_ne!(dynamic_mat, dslice_of_smat); + assert_ne!(dslice_of_smat, dynamic_mat); + + // TODO - implement those comparisons + // assert_ne!(static_mat, typenum_static_mat); + //assert_ne!(typenum_static_mat, static_mat); - assert_ne!(dynamic_mat, static_dyn_slice); - assert_ne!(static_dyn_slice, larger_slice); } From 8a3f6a12cdfe7afc8f06c8ff571e6836d35149af Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sun, 19 Jan 2020 16:24:26 +0100 Subject: [PATCH 08/13] Refactor and move named_dimension! macro --- src/base/dimension.rs | 147 +++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 88 deletions(-) diff --git a/src/base/dimension.rs b/src/base/dimension.rs index cabd8a1c..17462d41 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -247,66 +247,6 @@ impl NamedDim for typenum::U1 { type Name = U1; } -macro_rules! named_dimension( - ($($D: ident),* $(,)*) => {$( - /// A type level dimension. - #[derive(Debug, Copy, Clone, Hash, Eq)] - #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] - pub struct $D; - - impl Dim for $D { - #[inline] - fn try_to_usize() -> Option { - Some(typenum::$D::to_usize()) - } - - #[inline] - fn from_usize(dim: usize) -> Self { - assert!(dim == typenum::$D::to_usize(), "Mismatched dimension."); - $D - } - - #[inline] - fn value(&self) -> usize { - typenum::$D::to_usize() - } - } - - impl DimName for $D { - type Value = typenum::$D; - - #[inline] - fn name() -> Self { - $D - } - } - - impl NamedDim for typenum::$D { - type Name = $D; - } - - impl IsNotStaticOne for $D { } - - impl PartialEq for $D { - fn eq(&self, other: &T) -> bool { - self.value() == other.value() - } - } - )*} -); - -// We give explicit names to all Unsigned in [0, 128[ -named_dimension!( - U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, - U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, - U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, - U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, - U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, - U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, - U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, - U127 -); - // For values greater than U1023, just use the typenum binary representation directly. impl< A: Bit + Any + Debug + Copy + PartialEq + Send + Sync, @@ -379,23 +319,6 @@ impl< { } -impl< - T: Dim, - A: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - B: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - C: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - D: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - E: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - F: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - G: Bit + Any + Debug + Copy + PartialEq + Send + Sync, - > PartialEq - for UInt, A>, B>, C>, D>, E>, F>, G> -{ - fn eq(&self, other: &T) -> bool { - self.value() == other.value() - } -} - impl NamedDim for UInt { @@ -438,14 +361,62 @@ impl PartialEq - for UInt -{ - fn eq(&self, other: &T) -> bool { - self.value() == other.value() - } -} +macro_rules! named_dimension( + ($($D: ident),* $(,)*) => {$( + /// A type level dimension. + #[derive(Debug, Copy, Clone, Hash, Eq)] + #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] + pub struct $D; + + impl Dim for $D { + #[inline] + fn try_to_usize() -> Option { + Some(typenum::$D::to_usize()) + } + + #[inline] + fn from_usize(dim: usize) -> Self { + assert!(dim == typenum::$D::to_usize(), "Mismatched dimension."); + $D + } + + #[inline] + fn value(&self) -> usize { + typenum::$D::to_usize() + } + } + + impl DimName for $D { + type Value = typenum::$D; + + #[inline] + fn name() -> Self { + $D + } + } + + impl NamedDim for typenum::$D { + type Name = $D; + } + + impl IsNotStaticOne for $D { } + + impl PartialEq for $D { + fn eq(&self, other: &T) -> bool { + self.value() == other.value() + } + } + )*} +); + +// We give explicit names to all Unsigned in [0, 128[ +named_dimension!( + U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, + U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, + U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, + U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, + U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, + U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, + U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, + U127, +); From 403e63dc5eb7af2db8cdca93b6c1f070eb736456 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sat, 25 Jan 2020 12:56:17 +0100 Subject: [PATCH 09/13] Remove trailing whitespace --- src/base/matrix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 5d60b970..2a87d697 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1137,7 +1137,7 @@ impl + IsNotStaticOne, S: Storage where DefaultAllocator: Allocator, DimSum> { assert!(self.is_square(), "Only square matrices can currently be transformed to homogeneous coordinates."); let dim = DimSum::::from_usize(self.nrows() + 1); - let mut res = MatrixN::identity_generic(dim, dim); + let mut res = MatrixN::identity_generic(dim, dim); res.generic_slice_mut::((0, 0), self.data.shape()).copy_from(&self); res } From d35ef06f445af4f0e9dffc9190f4324a866c1f94 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sat, 25 Jan 2020 12:57:15 +0100 Subject: [PATCH 10/13] Remove unnecessary PartialEq trait use in PartialEq impl for Matrix --- src/base/matrix.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 2a87d697..01105524 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1347,9 +1347,9 @@ where impl PartialEq> for Matrix where N: Scalar + PartialEq, - C: Dim + PartialEq, + C: Dim, C2: Dim, - R: Dim + PartialEq, + R: Dim, R2: Dim, S: Storage, S2: Storage From 08d990f2760a4a4b3d490c4ac5e27aeaa2bfa0d3 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sat, 25 Jan 2020 12:58:05 +0100 Subject: [PATCH 11/13] Make shape check a one-liner in impl PartialEq for Matrix --- src/base/matrix.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 01105524..8bc02b21 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1356,10 +1356,7 @@ where { #[inline] fn eq(&self, right: &Matrix) -> bool { - if self.shape() == right.shape() { - return self.iter().zip(right.iter()).all(|(l, r)| l == r) - } - false + self.shape() == right.shape() && self.iter().zip(right.iter()).all(|(l, r)| l == r) } } From 6d608cb99f4f3d1125b50ae5d5caf488794508b9 Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sat, 25 Jan 2020 20:17:49 +0100 Subject: [PATCH 12/13] Revert "Broader PartialEq implementation for types implementing Dim trait" This reverts commit 6f5c9c0f --- src/base/dimension.rs | 122 +++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 67 deletions(-) diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 17462d41..112996d3 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -14,7 +14,7 @@ use typenum::{ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Dim of dynamically-sized algebraic entities. -#[derive(Clone, Copy, Eq, Debug)] +#[derive(Clone, Copy, Eq, PartialEq, Debug)] pub struct Dynamic { value: usize, } @@ -107,12 +107,6 @@ impl Sub for Dynamic { } } -impl PartialEq for Dynamic { - fn eq(&self, other: &T) -> bool { - self.value() == other.value() - } -} - /* * * Operations. @@ -247,6 +241,60 @@ impl NamedDim for typenum::U1 { type Name = U1; } +macro_rules! named_dimension ( + ($($D: ident),* $(,)*) => {$( + /// A type level dimension. + #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] + #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] + pub struct $D; + + impl Dim for $D { + #[inline] + fn try_to_usize() -> Option { + Some(typenum::$D::to_usize()) + } + + #[inline] + fn from_usize(dim: usize) -> Self { + assert!(dim == typenum::$D::to_usize(), "Mismatched dimension."); + $D + } + + #[inline] + fn value(&self) -> usize { + typenum::$D::to_usize() + } + } + + impl DimName for $D { + type Value = typenum::$D; + + #[inline] + fn name() -> Self { + $D + } + } + + impl NamedDim for typenum::$D { + type Name = $D; + } + + impl IsNotStaticOne for $D { } + )*} +); + +// We give explicit names to all Unsigned in [0, 128[ +named_dimension!( + U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, + U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, + U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, + U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, + U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, + U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, + U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, + U127 +); + // For values greater than U1023, just use the typenum binary representation directly. impl< A: Bit + Any + Debug + Copy + PartialEq + Send + Sync, @@ -360,63 +408,3 @@ impl { } - -macro_rules! named_dimension( - ($($D: ident),* $(,)*) => {$( - /// A type level dimension. - #[derive(Debug, Copy, Clone, Hash, Eq)] - #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] - pub struct $D; - - impl Dim for $D { - #[inline] - fn try_to_usize() -> Option { - Some(typenum::$D::to_usize()) - } - - #[inline] - fn from_usize(dim: usize) -> Self { - assert!(dim == typenum::$D::to_usize(), "Mismatched dimension."); - $D - } - - #[inline] - fn value(&self) -> usize { - typenum::$D::to_usize() - } - } - - impl DimName for $D { - type Value = typenum::$D; - - #[inline] - fn name() -> Self { - $D - } - } - - impl NamedDim for typenum::$D { - type Name = $D; - } - - impl IsNotStaticOne for $D { } - - impl PartialEq for $D { - fn eq(&self, other: &T) -> bool { - self.value() == other.value() - } - } - )*} -); - -// We give explicit names to all Unsigned in [0, 128[ -named_dimension!( - U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, - U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, - U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, - U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, - U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, - U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, - U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, - U127, -); From 4d97447cbd29765d10bb24c2d87f7979cd4ecbff Mon Sep 17 00:00:00 2001 From: "S.Brandeis" Date: Sat, 25 Jan 2020 20:24:21 +0100 Subject: [PATCH 13/13] Add type annotations in serde tests --- tests/core/serde.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/core/serde.rs b/tests/core/serde.rs index c209e35c..781ace77 100644 --- a/tests/core/serde.rs +++ b/tests/core/serde.rs @@ -14,7 +14,8 @@ macro_rules! test_serde( fn $test() { let v: $ty = rand::random(); let serialized = serde_json::to_string(&v).unwrap(); - assert_eq!(v, serde_json::from_str(&serialized).unwrap()); + let deserialized: $ty = serde_json::from_str(&serialized).unwrap(); + assert_eq!(v, deserialized); } )*} ); @@ -23,7 +24,8 @@ macro_rules! test_serde( fn serde_dmatrix() { let v: DMatrix = DMatrix::new_random(3, 4); let serialized = serde_json::to_string(&v).unwrap(); - assert_eq!(v, serde_json::from_str(&serialized).unwrap()); + let deserialized: DMatrix = serde_json::from_str(&serialized).unwrap(); + assert_eq!(v, deserialized); } test_serde!(