Skip to content

Support for serialization using abomonation #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ script:
- cargo build --verbose
- cargo build --verbose --features arbitrary
- cargo build --verbose --features serde-serialize
- cargo test --verbose --features "arbitrary debug serde-serialize"
- cargo build --verbose --features abomonation-serialize
- cargo test --verbose --features "arbitrary serde-serialize abomonation-serialize"
- cd nalgebra-lapack; cargo test --verbose

env:
matrix:
- CARGO_FEATURE_SYSTEM_NETLIB=1 CARGO_FEATURE_EXCLUDE_LAPACKE=1 CARGO_FEATURE_EXCLUDE_CBLAS=1
- CARGO_FEATURE_SYSTEM_NETLIB=1 CARGO_FEATURE_EXCLUDE_LAPACKE=1 CARGO_FEATURE_EXCLUDE_CBLAS=1
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/lib.rs"
[features]
arbitrary = [ "quickcheck" ]
serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ]
abomonation-serialize = [ "abomonation" ]
debug = [ ]

[dependencies]
Expand All @@ -31,6 +32,7 @@ alga = "0.5"
matrixmultiply = "0.1"
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
abomonation = { version = "0.4", optional = true }

[dependencies.quickcheck]
optional = true
Expand Down
18 changes: 18 additions & 0 deletions src/core/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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, DefaultAllocator, Unit, VectorN, MatrixMN};
Expand Down Expand Up @@ -99,6 +102,21 @@ impl<'de, N, R, C, S> Deserialize<'de> for Matrix<N, R, C, S>
}
}

#[cfg(feature = "abomonation-serialize")]
impl<N: Scalar, R: Dim, C: Dim, S: Abomonation> Abomonation for Matrix<N, R, C, S> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N: Scalar, R: Dim, C: Dim, S> Matrix<N, R, C, S> {
/// Creates a new matrix with the given data without statically checking that the matrix
/// dimension matches the storage dimension.
Expand Down
36 changes: 36 additions & 0 deletions src/core/matrix_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -319,3 +322,36 @@ where N: Scalar + Deserialize<'a>,
}
}
}

#[cfg(feature = "abomonation-serialize")]
impl<N, R, C> Abomonation for MatrixArray<N, R, C>
where R: DimName,
C: DimName,
R::Value: Mul<C::Value>,
Prod<R::Value, C::Value>: ArrayLength<N>,
N: Abomonation
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
for element in self.data.as_slice() {
element.entomb(writer);
}
}

unsafe fn embalm(&mut self) {
for element in self.data.as_mut_slice() {
element.embalm();
}
}

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)
}
}
18 changes: 18 additions & 0 deletions src/core/matrix_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use core::storage::{Storage, StorageMut, Owned, ContiguousStorage, ContiguousSto
use core::allocator::Allocator;
use core::default_allocator::DefaultAllocator;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

/*
*
* Storage.
Expand Down Expand Up @@ -213,6 +216,21 @@ unsafe impl<N: Scalar, R: DimName> StorageMut<N, R, Dynamic> for MatrixVec<N, R,
}
}

#[cfg(feature = "abomonation-serialize")]
impl<N: Abomonation, R: Dim, C: Dim> Abomonation for MatrixVec<N, R, C> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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)
}
}

unsafe impl<N: Scalar, R: DimName> ContiguousStorage<N, R, Dynamic> for MatrixVec<N, R, Dynamic>
where DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self> {
}
Expand Down
18 changes: 18 additions & 0 deletions src/core/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,6 +39,21 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit<T> {
}
}

#[cfg(feature = "abomonation-serialize")]
impl<T: Abomonation> Abomonation for Unit<T> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<T: NormedSpace> Unit<T> {
/// Normalize the given value and return it wrapped on a `Unit` structure.
#[inline]
Expand Down
28 changes: 28 additions & 0 deletions src/geometry/isometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use approx::ApproxEq;
#[cfg(feature = "serde-serialize")]
use serde;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

use alga::general::{Real, SubsetOf};
use alga::linear::Rotation;

Expand Down Expand Up @@ -42,6 +45,30 @@ pub struct Isometry<N: Real, D: DimName, R>
_noconstruct: PhantomData<N>
}

#[cfg(feature = "abomonation-serialize")]
impl<N, D, R> Abomonation for IsometryBase<N, D, R>
where N: Scalar,
D: DimName,
R: Abomonation,
TranslationBase<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D>
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N: Real + hash::Hash, D: DimName + hash::Hash, R: hash::Hash> hash::Hash for Isometry<N, D, R>
where DefaultAllocator: Allocator<N, D>,
Owned<N, D>: hash::Hash {
Expand All @@ -66,6 +93,7 @@ impl<N: Real, D: DimName, R: Rotation<Point<N, D>> + Clone> Clone for Isometry<N

impl<N: Real, D: DimName, R: Rotation<Point<N, D>>> Isometry<N, D, R>
where DefaultAllocator: Allocator<N, D> {

/// Creates a new isometry from its rotational and translational parts.
#[inline]
pub fn from_parts(translation: Translation<N, D>, rotation: R) -> Isometry<N, D, R> {
Expand Down
24 changes: 24 additions & 0 deletions src/geometry/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use approx::ApproxEq;
#[cfg(feature = "serde-serialize")]
use serde;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

use core::{DefaultAllocator, Scalar, VectorN};
use core::iter::{MatrixIter, MatrixIterMut};
use core::dimension::{DimName, DimNameSum, DimNameAdd, U1};
Expand Down Expand Up @@ -66,6 +69,27 @@ where DefaultAllocator: Allocator<N, D>,
}
}


#[cfg(feature = "abomonation-serialize")]
impl<N, D> Abomonation for PointBase<N, D>
where N: Scalar,
D: DimName,
ColumnVector<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D>
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N: Scalar, D: DimName> Point<N, D>
where DefaultAllocator: Allocator<N, D> {

Expand Down
21 changes: 20 additions & 1 deletion src/geometry/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use serde;
#[cfg(feature = "serde-serialize")]
use core::storage::Owned;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

use alga::general::Real;

use core::{Unit, Vector3, Vector4, MatrixSlice, MatrixSliceMut, SquareMatrix, MatrixN};
Expand All @@ -25,9 +28,25 @@ pub struct Quaternion<N: Real> {
pub coords: Vector4<N>
}

impl<N: Real + Eq> Eq for Quaternion<N> {
#[cfg(feature = "abomonation-serialize")]
impl<N: Real> Abomonation for QuaternionBase<N>
where Vector4<N>: Abomonation
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N: Real + Eq> Eq for Quaternion<N> { }

impl<N: Real> PartialEq for Quaternion<N> {
fn eq(&self, rhs: &Self) -> bool {
self.coords == rhs.coords ||
Expand Down
23 changes: 23 additions & 0 deletions src/geometry/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use serde;
#[cfg(feature = "serde-serialize")]
use core::storage::Owned;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

use alga::general::Real;

use core::{DefaultAllocator, Scalar, MatrixN};
Expand Down Expand Up @@ -45,6 +48,26 @@ impl<N: Scalar, D: DimName> Clone for Rotation<N, D>
}
}

#[cfg(feature = "abomonation-serialize")]
impl<N, D> Abomonation for RotationBase<N, D>
where N: Scalar,
D: DimName,
MatrixN<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D, D>
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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)
}
}

#[cfg(feature = "serde-serialize")]
impl<N: Scalar, D: DimName> serde::Serialize for Rotation<N, D>
where DefaultAllocator: Allocator<N, D, D>,
Expand Down
22 changes: 22 additions & 0 deletions src/geometry/similarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use approx::ApproxEq;
#[cfg(feature = "serde-serialize")]
use serde;

#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

use alga::general::{Real, SubsetOf};
use alga::linear::Rotation;

Expand All @@ -15,6 +18,7 @@ use core::allocator::Allocator;
use geometry::{Point, Translation, Isometry};



/// A similarity, i.e., an uniform scaling, followed by a rotation, followed by a translation.
#[repr(C)]
#[derive(Debug)]
Expand All @@ -38,6 +42,24 @@ pub struct Similarity<N: Real, D: DimName, R>
scaling: N
}

#[cfg(feature = "abomonation-serialize")]
impl<N: Scalar, D: DimName, R> Abomonation for SimilarityBase<N, D, R>
where IsometryBase<N, D, R>: Abomonation,
DefaultAllocator: Allocator<N, D>
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N: Real + hash::Hash, D: DimName + hash::Hash, R: hash::Hash> hash::Hash for Similarity<N, D, R>
where DefaultAllocator: Allocator<N, D>,
Owned<N, D>: hash::Hash {
Expand Down
Loading