From 65fa4cf740facdd2df9d4aeb6b66835710051402 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:07:06 +0200 Subject: [PATCH 01/12] Implement Abomonation for static matrices --- Cargo.toml | 3 +++ src/core/matrix.rs | 18 ++++++++++++++++++ src/core/matrix_array.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 2 ++ tests/abomonation.rs | 22 ++++++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 tests/abomonation.rs diff --git a/Cargo.toml b/Cargo.toml index e57e3f48..1e7b3b4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ path = "src/lib.rs" [features] arbitrary = [ "quickcheck" ] serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ] +abomonation-serialize = [ "abomonation", "abomonation_derive" ] [dependencies] typenum = "1.4" @@ -29,6 +30,8 @@ approx = "0.1" alga = "0.5" serde = { version = "1.0", optional = true } serde_derive = { version = "1.0", optional = true } +abomonation = { version = "0.4", optional = true } +abomonation_derive = { version = "0.2", optional = true } # clippy = "*" [dependencies.quickcheck] diff --git a/src/core/matrix.rs b/src/core/matrix.rs index 6deb899f..b56ce61a 100644 --- a/src/core/matrix.rs +++ b/src/core/matrix.rs @@ -10,6 +10,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::{Ring, Real}; use core::{Scalar, Unit}; @@ -122,6 +125,21 @@ impl<'de, N, R, C, S> Deserialize<'de> for Matrix } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for Matrix { + unsafe fn entomb(&self, writer: &mut Vec) { + self.data.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.data.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.data.exhume(bytes) + } +} + 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/matrix_array.rs b/src/core/matrix_array.rs index 5acd67d3..c344a3a3 100644 --- a/src/core/matrix_array.rs +++ b/src/core/matrix_array.rs @@ -13,6 +13,9 @@ use std::mem; #[cfg(feature = "serde-serialize")] use std::marker::PhantomData; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use typenum::Prod; use generic_array::{ArrayLength, GenericArray}; @@ -299,3 +302,24 @@ where N: Scalar + Deserialize<'a>, } } } + +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for MatrixArray + where R: DimName, + C: DimName, + R::Value: Mul, + Prod: ArrayLength, + N: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.data.as_slice().entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.data.as_slice().embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.data.as_slice().exhume(bytes) + } +} diff --git a/src/lib.rs b/src/lib.rs index 3ab855d7..0231242b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,6 +94,8 @@ extern crate serde; #[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; +#[cfg(feature = "abomonation")] +extern crate abomonation; extern crate num_traits as num; extern crate num_complex; extern crate rand; diff --git a/tests/abomonation.rs b/tests/abomonation.rs new file mode 100644 index 00000000..57696cea --- /dev/null +++ b/tests/abomonation.rs @@ -0,0 +1,22 @@ +extern crate rand; +extern crate nalgebra; +extern crate abomonation; + +use rand::random; +use abomonation::{Abomonation, encode, decode}; +use nalgebra::Matrix3x4; + +#[test] +fn abomonate_matrix3x4() { + assert_encode_and_decode(&random::>()); +} + +fn assert_encode_and_decode(data: &T) { + let mut bytes = Vec::new(); + unsafe { encode(data, &mut bytes); } + + if let Some((result, rest)) = unsafe { decode::(&mut bytes) } { + assert!(result == data); + assert!(rest.len() == 0, "binary data was not decoded completely"); + } +} From 49f12a379d0dfcef93f0b9de1595329732d34eff Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:18:47 +0200 Subject: [PATCH 02/12] Implement Abomonation for static-size points --- src/geometry/point.rs | 23 +++++++++++++++++++++++ tests/abomonation.rs | 7 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 53f914a2..8587e528 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -6,6 +6,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use core::{Scalar, ColumnVector, OwnedColumnVector}; use core::iter::{MatrixIter, MatrixIterMut}; use core::dimension::{DimName, DimNameSum, DimNameAdd, U1}; @@ -75,6 +78,26 @@ impl<'de, N, D, S> Deserialize<'de> for PointBase } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for PointBase + where N: Scalar, + D: DimName, + S: Storage, + ColumnVector: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.coords.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.coords.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.coords.exhume(bytes) + } +} + impl> PointBase { /// Creates a new point with the given coordinates. #[inline] diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 57696cea..1da6ba3c 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,13 +4,18 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::Matrix3x4; +use nalgebra::{Matrix3x4, Point3}; #[test] fn abomonate_matrix3x4() { assert_encode_and_decode(&random::>()); } +#[test] +fn abomonate_point3() { + assert_encode_and_decode(&random::>()); +} + fn assert_encode_and_decode(data: &T) { let mut bytes = Vec::new(); unsafe { encode(data, &mut bytes); } From 7c3a05f6683d9598f7943143016decd99b328370 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:30:50 +0200 Subject: [PATCH 03/12] Implement Abomonation for dynamic matrices --- src/core/matrix_vec.rs | 18 ++++++++++++++++++ tests/abomonation.rs | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/core/matrix_vec.rs b/src/core/matrix_vec.rs index 51a02640..2b5bb372 100644 --- a/src/core/matrix_vec.rs +++ b/src/core/matrix_vec.rs @@ -5,6 +5,9 @@ use core::dimension::{Dim, DimName, Dynamic, U1}; use core::storage::{Storage, StorageMut, Owned, OwnedStorage}; use core::default_allocator::DefaultAllocator; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + /* * * Storage. @@ -171,3 +174,18 @@ unsafe impl OwnedStorage for MatrixVec Abomonation for MatrixVec { + unsafe fn entomb(&self, writer: &mut Vec) { + self.data.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.data.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.data.exhume(bytes) + } +} diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 1da6ba3c..1fd694cd 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,7 +4,7 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{Matrix3x4, Point3}; +use nalgebra::{DMatrix, Matrix3x4, Point3}; #[test] fn abomonate_matrix3x4() { @@ -16,6 +16,11 @@ fn abomonate_point3() { assert_encode_and_decode(&random::>()); } +#[test] +fn abomonate_dmatrix() { + assert_encode_and_decode(&DMatrix::::new_random(3, 5)); +} + fn assert_encode_and_decode(data: &T) { let mut bytes = Vec::new(); unsafe { encode(data, &mut bytes); } From 308177a7d6fd568a285eff50f0cda410d2b0d3a1 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:37:16 +0200 Subject: [PATCH 04/12] Implement Abomonation for translations --- src/geometry/translation.rs | 22 ++++++++++++++++++++++ tests/abomonation.rs | 7 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 1580bcb7..9c4d26e1 100644 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::{Real, ClosedNeg}; use core::{Scalar, ColumnVector, OwnedSquareMatrix}; @@ -50,6 +53,25 @@ impl<'de, N, D, S> Deserialize<'de> for TranslationBase } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for TranslationBase + where N: Scalar, + D: DimName, + ColumnVector: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.vector.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.vector.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.vector.exhume(bytes) + } +} + impl TranslationBase where N: Scalar, S: Storage { diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 1fd694cd..d50091a9 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,7 +4,7 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{DMatrix, Matrix3x4, Point3}; +use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3}; #[test] fn abomonate_matrix3x4() { @@ -21,6 +21,11 @@ fn abomonate_dmatrix() { assert_encode_and_decode(&DMatrix::::new_random(3, 5)); } +#[test] +fn abomonate_translation3() { + assert_encode_and_decode(&random::>()); +} + fn assert_encode_and_decode(data: &T) { let mut bytes = Vec::new(); unsafe { encode(data, &mut bytes); } From f67a7bd324160b716edf154cab8d2d8dd2f7e584 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:41:03 +0200 Subject: [PATCH 05/12] Implement Abomonation for rotations --- src/geometry/rotation.rs | 22 ++++++++++++++++++++++ tests/abomonation.rs | 7 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 1429830a..e9f06b69 100644 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::Real; use core::{SquareMatrix, Scalar, OwnedSquareMatrix}; @@ -49,6 +52,25 @@ impl<'de, N, D, S> Deserialize<'de> for RotationBase } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for RotationBase + where N: Scalar, + D: DimName, + SquareMatrix: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.matrix.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.matrix.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.matrix.exhume(bytes) + } +} + impl> RotationBase where N: Scalar, S: Storage { diff --git a/tests/abomonation.rs b/tests/abomonation.rs index d50091a9..8861342b 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,7 +4,7 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3}; +use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3, Rotation3}; #[test] fn abomonate_matrix3x4() { @@ -26,6 +26,11 @@ fn abomonate_translation3() { assert_encode_and_decode(&random::>()); } +#[test] +fn abomonate_rotation3() { + assert_encode_and_decode(&random::>()); +} + fn assert_encode_and_decode(data: &T) { let mut bytes = Vec::new(); unsafe { encode(data, &mut bytes); } From 916006f80a078732678d4568908cdd7cbe7b02c9 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 12:46:25 +0200 Subject: [PATCH 06/12] Refactor abomonation test suite using macro --- tests/abomonation.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 8861342b..371a52b6 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -6,29 +6,25 @@ use rand::random; use abomonation::{Abomonation, encode, decode}; use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3, Rotation3}; -#[test] -fn abomonate_matrix3x4() { - assert_encode_and_decode(&random::>()); -} - -#[test] -fn abomonate_point3() { - assert_encode_and_decode(&random::>()); -} - #[test] fn abomonate_dmatrix() { assert_encode_and_decode(&DMatrix::::new_random(3, 5)); } -#[test] -fn abomonate_translation3() { - assert_encode_and_decode(&random::>()); -} +macro_rules! test_abomonation( + ($($test: ident, $ty: ty);* $(;)*) => {$( + #[test] + fn $test() { + assert_encode_and_decode(&random::<$ty>()); + } + )*} +); -#[test] -fn abomonate_rotation3() { - assert_encode_and_decode(&random::>()); +test_abomonation! { + abomonate_matrix3x4, Matrix3x4; + abomonate_point3, Point3; + abomonate_translation3, Translation3; + abomonate_rotation3, Rotation3; } fn assert_encode_and_decode(data: &T) { From e09af0ca82e77f157e64de922f4695a40b6e2dcf Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 14:32:02 +0200 Subject: [PATCH 07/12] Implement Abomonation for remaining types --- src/core/unit.rs | 18 ++++++++++++++++++ src/geometry/isometry.rs | 26 ++++++++++++++++++++++++++ src/geometry/quaternion.rs | 21 +++++++++++++++++++++ src/geometry/similarity.rs | 21 +++++++++++++++++++++ src/lib.rs | 3 +++ tests/abomonation.rs | 10 +++++++++- 6 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/core/unit.rs b/src/core/unit.rs index 6051165d..0d8035ef 100644 --- a/src/core/unit.rs +++ b/src/core/unit.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::SubsetOf; use alga::linear::NormedSpace; @@ -36,6 +39,21 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for Unit { + unsafe fn entomb(&self, writer: &mut Vec) { + self.value.entomb(writer); + } + + unsafe fn embalm(&mut self) { + self.value.embalm(); + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.value.exhume(bytes) + } +} + impl Unit { /// Normalize the given value and return it wrapped on a `Unit` structure. #[inline] diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 67d2e7e8..9ae55ed9 100644 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -11,6 +11,9 @@ use core::storage::{Storage, OwnedStorage}; use core::allocator::{Allocator, OwnedAllocator}; use geometry::{TranslationBase, PointBase}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + /// An isometry that uses a data storage deduced from the allocator `A`. pub type OwnedIsometryBase = @@ -32,6 +35,29 @@ pub struct IsometryBase { _noconstruct: PhantomData } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for IsometryBase + where N: Scalar, + D: DimName, + R: Abomonation, + TranslationBase: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.rotation.entomb(writer); + self.translation.entomb(writer); + } + + unsafe fn embalm(&mut self) { + self.rotation.embalm(); + self.translation.embalm(); + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.rotation.exhume(bytes) + .and_then(|bytes| self.translation.exhume(bytes)) + } +} + impl IsometryBase where N: Real, S: OwnedStorage, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 4a99acc3..97482ab6 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::Real; use core::{Unit, ColumnVector, OwnedColumnVector, MatrixSlice, MatrixSliceMut, SquareMatrix, @@ -56,6 +59,24 @@ impl<'de, N, S> Deserialize<'de> for QuaternionBase } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for QuaternionBase + where N: Real, + S: Storage, + ColumnVector: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.coords.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.coords.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.coords.exhume(bytes) + } +} impl Eq for QuaternionBase where N: Real + Eq, diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 43e4ec1f..bb7fd4f4 100644 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -10,6 +10,9 @@ use core::storage::{Storage, OwnedStorage}; use core::allocator::{Allocator, OwnedAllocator}; use geometry::{PointBase, TranslationBase, IsometryBase}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + /// A similarity that uses a data storage deduced from the allocator `A`. pub type OwnedSimilarityBase = @@ -25,6 +28,24 @@ pub struct SimilarityBase { scaling: N } + +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for SimilarityBase + where IsometryBase: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.isometry.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.isometry.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.isometry.exhume(bytes) + } +} + impl SimilarityBase where N: Real, S: OwnedStorage, diff --git a/src/lib.rs b/src/lib.rs index 0231242b..22421a02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,13 +89,16 @@ an optimized set of tools for computer graphics and physics. Those features incl #[cfg(feature = "arbitrary")] extern crate quickcheck; + #[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; + #[cfg(feature = "abomonation")] extern crate abomonation; + extern crate num_traits as num; extern crate num_complex; extern crate rand; diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 371a52b6..16d825c5 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,7 +4,10 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3, Rotation3}; +use nalgebra::{ + DMatrix, Matrix3x4, Point3, Translation3, Rotation3, Isometry3, Quaternion, + IsometryMatrix3, Similarity3, SimilarityMatrix3 +}; #[test] fn abomonate_dmatrix() { @@ -25,6 +28,11 @@ test_abomonation! { abomonate_point3, Point3; abomonate_translation3, Translation3; abomonate_rotation3, Rotation3; + abomonate_isometry3, Isometry3; + abomonate_isometry_matrix3, IsometryMatrix3; + abomonate_similarity3, Similarity3; + abomonate_similarity_matrix3, SimilarityMatrix3; + abomonate_quaternion, Quaternion; } fn assert_encode_and_decode(data: &T) { From 2511e660eb1c21023ef3fff181810f1b2532a027 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 14:32:56 +0200 Subject: [PATCH 08/12] Add abomonation feature to Travis config --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 57a75a7f..16e2fbde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,4 +17,5 @@ script: - cargo build --verbose - cargo build --verbose --features arbitrary - cargo build --verbose --features serde-serialize - - cargo test --verbose --features "arbitrary serde-serialize" + - cargo build --verbose --features abomonation-serialize + - cargo test --verbose --features "arbitrary serde-serialize abomonation-serialize" From 66f491ffa7d84b59887f69611cae1466609b8896 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 16:26:36 +0200 Subject: [PATCH 09/12] Remove abomonation_derive dependency --- Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e7b3b4e..9110c8c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ path = "src/lib.rs" [features] arbitrary = [ "quickcheck" ] serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ] -abomonation-serialize = [ "abomonation", "abomonation_derive" ] +abomonation-serialize = [ "abomonation" ] [dependencies] typenum = "1.4" @@ -31,7 +31,6 @@ alga = "0.5" serde = { version = "1.0", optional = true } serde_derive = { version = "1.0", optional = true } abomonation = { version = "0.4", optional = true } -abomonation_derive = { version = "0.2", optional = true } # clippy = "*" [dependencies.quickcheck] From a45ef26375ac25f7a4009daa63919b6d0c1c92c6 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 20:53:55 +0200 Subject: [PATCH 10/12] Provide safer Abomonation impl for matrix arrays This is more robust than delegating to a slice, which has been removed upstream due to unsafety. Since we can rely on there being no pointer indirection in a GenericArray, we just iterate over the array. --- src/core/matrix_array.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/core/matrix_array.rs b/src/core/matrix_array.rs index c344a3a3..86544ec0 100644 --- a/src/core/matrix_array.rs +++ b/src/core/matrix_array.rs @@ -312,14 +312,26 @@ impl Abomonation for MatrixArray N: Abomonation { unsafe fn entomb(&self, writer: &mut Vec) { - self.data.as_slice().entomb(writer) + for element in self.data.as_slice() { + element.entomb(writer); + } } unsafe fn embalm(&mut self) { - self.data.as_slice().embalm() + for element in self.data.as_mut_slice() { + element.embalm(); + } } - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.data.as_slice().exhume(bytes) + unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + for element in self.data.as_mut_slice() { + let temp = bytes; + bytes = if let Some(remainder) = element.exhume(temp) { + remainder + } else { + return None; + } + } + Some(bytes) } } From 32ac8224ef18521a675c522970dccbe11b8c83a0 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 21:11:24 +0200 Subject: [PATCH 11/12] Test dropping of abomonated data --- tests/abomonation.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 16d825c5..3ca0adcf 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -11,14 +11,14 @@ use nalgebra::{ #[test] fn abomonate_dmatrix() { - assert_encode_and_decode(&DMatrix::::new_random(3, 5)); + assert_encode_and_decode(DMatrix::::new_random(3, 5)); } macro_rules! test_abomonation( ($($test: ident, $ty: ty);* $(;)*) => {$( #[test] fn $test() { - assert_encode_and_decode(&random::<$ty>()); + assert_encode_and_decode(random::<$ty>()); } )*} ); @@ -35,12 +35,21 @@ test_abomonation! { abomonate_quaternion, Quaternion; } -fn assert_encode_and_decode(data: &T) { +fn assert_encode_and_decode(original_data: T) { + use std::mem::drop; + + // Hold on to a clone for later comparison + let data = original_data.clone(); + + // Encode let mut bytes = Vec::new(); - unsafe { encode(data, &mut bytes); } + unsafe { encode(&original_data, &mut bytes); } + + // Drop the original, so that dangling pointers are revealed by the test + drop(original_data); if let Some((result, rest)) = unsafe { decode::(&mut bytes) } { - assert!(result == data); + assert!(result == &data); assert!(rest.len() == 0, "binary data was not decoded completely"); } } From 48f80d99ccee2457029ea349ee37259945f3f21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Tue, 15 Aug 2017 19:36:38 +0200 Subject: [PATCH 12/12] Fix abomonation integration. --- Makefile | 4 ++-- src/geometry/isometry.rs | 6 +++--- src/geometry/point.rs | 4 ++-- src/geometry/quaternion.rs | 2 +- src/geometry/rotation.rs | 2 +- src/geometry/similarity.rs | 4 ++-- src/geometry/translation.rs | 2 +- tests/{ => core}/abomonation.rs | 6 +----- tests/core/mod.rs | 1 + tests/geometry/quaternion.rs | 2 +- tests/geometry/rotation.rs | 6 +++--- tests/geometry/unit_complex.rs | 2 +- tests/lib.rs | 1 + 13 files changed, 20 insertions(+), 22 deletions(-) rename tests/{ => core}/abomonation.rs (94%) diff --git a/Makefile b/Makefile index ad1fafcc..5af4b5a7 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ all: cargo check --features "debug arbitrary serde-serialize" doc: - cargo doc --no-deps --features "debug arbitrary serde-serialize" + cargo doc --no-deps --features "debug arbitrary serde-serialize abomonation" bench: cargo bench test: - cargo test --features "debug arbitrary serde-serialize" + cargo test --features "debug arbitrary serde-serialize abomonation-serialize" diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index fbde8c79..397aabfe 100644 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -46,11 +46,11 @@ pub struct Isometry } #[cfg(feature = "abomonation-serialize")] -impl Abomonation for IsometryBase - where N: Scalar, +impl Abomonation for Isometry + where N: Real, D: DimName, R: Abomonation, - TranslationBase: Abomonation, + Translation: Abomonation, DefaultAllocator: Allocator { unsafe fn entomb(&self, writer: &mut Vec) { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index ec18976c..9fe9b1a5 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -71,10 +71,10 @@ where DefaultAllocator: Allocator, #[cfg(feature = "abomonation-serialize")] -impl Abomonation for PointBase +impl Abomonation for Point where N: Scalar, D: DimName, - ColumnVector: Abomonation, + VectorN: Abomonation, DefaultAllocator: Allocator { unsafe fn entomb(&self, writer: &mut Vec) { diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 8dc3a99b..6a59329b 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -29,7 +29,7 @@ pub struct Quaternion { } #[cfg(feature = "abomonation-serialize")] -impl Abomonation for QuaternionBase +impl Abomonation for Quaternion where Vector4: Abomonation { unsafe fn entomb(&self, writer: &mut Vec) { diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index b97597cd..3a598cca 100644 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -49,7 +49,7 @@ impl Clone for Rotation } #[cfg(feature = "abomonation-serialize")] -impl Abomonation for RotationBase +impl Abomonation for Rotation where N: Scalar, D: DimName, MatrixN: Abomonation, diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 75ab6bbc..cc146918 100644 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -43,8 +43,8 @@ pub struct Similarity } #[cfg(feature = "abomonation-serialize")] -impl Abomonation for SimilarityBase - where IsometryBase: Abomonation, +impl Abomonation for Similarity + where Isometry: Abomonation, DefaultAllocator: Allocator { unsafe fn entomb(&self, writer: &mut Vec) { diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 68d89f42..385e00da 100644 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -48,7 +48,7 @@ impl Clone for Translation } #[cfg(feature = "abomonation-serialize")] -impl Abomonation for TranslationBase +impl Abomonation for Translation where N: Scalar, D: DimName, VectorN: Abomonation, diff --git a/tests/abomonation.rs b/tests/core/abomonation.rs similarity index 94% rename from tests/abomonation.rs rename to tests/core/abomonation.rs index 3ca0adcf..ac53716b 100644 --- a/tests/abomonation.rs +++ b/tests/core/abomonation.rs @@ -1,10 +1,6 @@ -extern crate rand; -extern crate nalgebra; -extern crate abomonation; - use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{ +use na::{ DMatrix, Matrix3x4, Point3, Translation3, Rotation3, Isometry3, Quaternion, IsometryMatrix3, Similarity3, SimilarityMatrix3 }; diff --git a/tests/core/mod.rs b/tests/core/mod.rs index aa73d361..71d09757 100644 --- a/tests/core/mod.rs +++ b/tests/core/mod.rs @@ -4,3 +4,4 @@ mod matrix; mod matrix_slice; mod blas; mod serde; +mod abomonation; diff --git a/tests/geometry/quaternion.rs b/tests/geometry/quaternion.rs index eb3a42f2..3000fa9c 100644 --- a/tests/geometry/quaternion.rs +++ b/tests/geometry/quaternion.rs @@ -83,7 +83,7 @@ quickcheck!( /* * - * Quaterion * Vector == RotationBase * Vector + * Quaterion * Vector == Rotation * Vector * */ fn unit_quaternion_mul_vector(q: UnitQuaternion, v: Vector3, p: Point3) -> bool { diff --git a/tests/geometry/rotation.rs b/tests/geometry/rotation.rs index d5fa782c..263577ce 100644 --- a/tests/geometry/rotation.rs +++ b/tests/geometry/rotation.rs @@ -79,7 +79,7 @@ quickcheck!( /* * - * RotationBase matrix between vectors. + * Rotation matrix between vectors. * */ fn rotation_between_is_anticommutative_2(a: Vector2, b: Vector2) -> bool { @@ -132,7 +132,7 @@ quickcheck!( /* * - * RotationBase construction. + * Rotation construction. * */ fn new_rotation_2(angle: f64) -> bool { @@ -159,7 +159,7 @@ quickcheck!( /* * - * RotationBase pow. + * Rotation pow. * */ fn powf_rotation_2(angle: f64, pow: f64) -> bool { diff --git a/tests/geometry/unit_complex.rs b/tests/geometry/unit_complex.rs index ff8d4a66..a5147690 100644 --- a/tests/geometry/unit_complex.rs +++ b/tests/geometry/unit_complex.rs @@ -60,7 +60,7 @@ quickcheck!( /* * - * Quaterion * Vector == RotationBase * Vector + * Quaterion * Vector == Rotation * Vector * */ fn unit_complex_mul_vector(c: UnitComplex, v: Vector2, p: Point2) -> bool { diff --git a/tests/lib.rs b/tests/lib.rs index 5e111eb0..9a1174e7 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -5,6 +5,7 @@ extern crate quickcheck; extern crate approx; extern crate num_traits as num; extern crate serde_json; +extern crate abomonation; extern crate rand; extern crate alga; extern crate nalgebra as na;