Skip to content

Commit

Permalink
Make serde optional behind the "serde-serialize" feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Feb 15, 2017
1 parent 896ad19 commit 42b4856
Show file tree
Hide file tree
Showing 24 changed files with 284 additions and 36 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ language: rust
script:
- rustc --version
- cargo --version
- cargo build --verbose
- cargo build --verbose --features arbitrary
- cargo test --verbose --features arbitrary
- cargo build --verbose --features serde-serialize
- cargo test --verbose --features arbitrary serde-serialize
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ overview of all the added/modified features.

This version is a major rewrite of the library. Major changes are:
* Algebraic traits are now defined by the [alga](https://crates.io/crates/alga) crate.
All other mathematical traits, except `Axpy` have been removed from
**nalgebra**.
* Methods are now preferred to free functions because they do not require any
trait to be used any more.
* Most algebraic entities can be parametrized by type-level integers
Expand All @@ -19,7 +21,8 @@ This version is a major rewrite of the library. Major changes are:
* More transformation types have been added: unit-sized complex numbers (for
2D rotations), affine/projective/general transformations with `Affine2/3`,
`Projective2/3`, and `Transform2/3`.
* Serde serialization is now supported instead of `rustc_serialize`.
* Serde serialization is now supported instead of `rustc_serialize`. Enable
it with the `serde-serialize` feature.
* Matrix **slices** are now implemented.

### Added
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/lib.rs"

[features]
arbitrary = [ "quickcheck" ]
serde-serialize = [ "serde", "serde_derive" ]

[dependencies]
typenum = "1.4"
Expand All @@ -25,9 +26,9 @@ rand = "0.3"
num-traits = "0.1"
num-complex = "0.1"
approx = "0.1"
alga = "0.4"
serde = "0.9"
serde_derive = "0.9"
alga = "0.5"
serde = { version = "0.9", optional = true }
serde_derive = { version = "0.9", optional = true }
# clippy = "*"

[dependencies.quickcheck]
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
all:
CARGO_INCREMENTAL=1 cargo build --features "arbitrary"
CARGO_INCREMENTAL=1 cargo build --features "arbitrary serde-serialize"

doc:
CARGO_INCREMENTAL=1 cargo doc --no-deps
CARGO_INCREMENTAL=1 cargo doc --no-deps --features "arbitrary serde-serialize"

bench:
cargo bench

test:
CARGO_INCREMENTAL=1 cargo test --features "arbitrary"
CARGO_INCREMENTAL=1 cargo test --features "arbitrary serde-serialize"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</p>
<p align = "center">
<strong>
<a href="http://nalgebra.org">Users guide</a> | <a href="http://nalgebra.org/rustdoc/nalgebra/index.html">Documentation</a> | <a href="http://users.nphysics.org">Forum</a>
<a href="http://nalgebra.org">Users guide</a> | <a href="http://nalgebra.org/rustdoc/nalgebra/index.html">Documentation</a> | <a href="http://users.nphysics.org/c/nalgebra">Forum</a>
</strong>
</p>

Expand Down
3 changes: 2 additions & 1 deletion src/core/coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ macro_rules! coords_impl(
/// Data structure used to provide access to matrix and vector coordinates with the dot
/// notation, e.g., `v.x` is the same as `v[0]` for a vector.
#[repr(C)]
#[derive(Eq, PartialEq, Clone, Hash, Debug, Copy, Serialize, Deserialize)]
#[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct $T<N: Scalar> {
$(pub $comps: N),*
}
Expand Down
9 changes: 6 additions & 3 deletions src/core/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::ops::{Add, Sub, Mul, Div};
use typenum::{self, Unsigned, UInt, B1, Bit, UTerm, Sum, Prod, Diff, Quot};

/// Dim of dynamically-sized algebraic entities.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Dynamic {
value: usize
}
Expand Down Expand Up @@ -161,7 +162,8 @@ pub trait NamedDim: Sized + Any + Unsigned {
type Name: DimName<Value = Self>;
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct U1;

impl Dim for U1 {
Expand Down Expand Up @@ -197,7 +199,8 @@ impl NamedDim for typenum::U1{

macro_rules! named_dimension(
($($D: ident),* $(,)*) => {$(
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct $D;

impl Dim for $D {
Expand Down
5 changes: 3 additions & 2 deletions src/core/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ Matrix<NNew, R, C, <<S as Storage<NOld, R, C>>::Alloc as Allocator<NNew, R, C>>:
/// dynamically-sized column vector should be represented as a `Matrix<N, Dynamic, U1, S>` (given
/// some concrete types for `N` and a compatible data storage type `S`).
#[repr(C)]
#[derive(Serialize, Deserialize, Hash, Debug, Clone, Copy)]
#[derive(Hash, Debug, Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Matrix<N: Scalar, R: Dim, C: Dim, S> {
/// The data storage that contains all the matrix components and informations about its number
/// of rows and column (if needed).
pub data: S,

#[serde(skip_serializing, skip_deserializing)]
#[cfg_attr(feature = "serde-serialize", serde(skip_serializing, skip_deserializing))]
_phantoms: PhantomData<(N, R, C)>
}

Expand Down
15 changes: 13 additions & 2 deletions src/core/matrix_array.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::mem;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut, Mul};
use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};

#[cfg(feature = "serde-serialize")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
#[cfg(feature = "serde-serialize")]
use serde::ser::SerializeSeq;
#[cfg(feature = "serde-serialize")]
use serde::de::{SeqVisitor, Visitor};
#[cfg(feature = "serde-serialize")]
use std::mem;
#[cfg(feature = "serde-serialize")]
use std::marker::PhantomData;

use typenum::Prod;
use generic_array::{ArrayLength, GenericArray};
Expand Down Expand Up @@ -199,6 +205,7 @@ unsafe impl<N, R, C> OwnedStorage<N, R, C> for MatrixArray<N, R, C>
*
*/
// XXX: open an issue for GenericArray so that it implements serde traits?
#[cfg(feature = "serde-serialize")]
impl<N, R, C> Serialize for MatrixArray<N, R, C>
where N: Scalar + Serialize,
R: DimName,
Expand All @@ -220,6 +227,7 @@ where N: Scalar + Serialize,
}


#[cfg(feature = "serde-serialize")]
impl<N, R, C> Deserialize for MatrixArray<N, R, C>
where N: Scalar + Deserialize,
R: DimName,
Expand All @@ -237,11 +245,13 @@ where N: Scalar + Deserialize,
}


#[cfg(feature = "serde-serialize")]
/// A visitor that produces a matrix array.
struct MatrixArrayVisitor<N, R, C> {
marker: PhantomData<(N, R, C)>
}

#[cfg(feature = "serde-serialize")]
impl<N, R, C> MatrixArrayVisitor<N, R, C>
where N: Scalar,
R: DimName,
Expand All @@ -257,6 +267,7 @@ where N: Scalar,
}
}

#[cfg(feature = "serde-serialize")]
impl<N, R, C> Visitor for MatrixArrayVisitor<N, R, C>
where N: Scalar + Deserialize,
R: DimName,
Expand Down
3 changes: 2 additions & 1 deletion src/core/matrix_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use core::default_allocator::DefaultAllocator;
*/
/// A Vec-based matrix data storage. It may be dynamically-sized.
#[repr(C)]
#[derive(Eq, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Eq, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct MatrixVec<N, R: Dim, C: Dim> {
data: Vec<N>,
nrows: R,
Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Data structures for vector and matrix computations.
//! [Reexported at the root of this crate.] Data structures for vector and matrix computations.
pub mod dimension;
pub mod constraint;
Expand Down
3 changes: 2 additions & 1 deletion src/core/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ 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, Serialize, Deserialize)]
#[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Unit<T> {
value: T
}
Expand Down
5 changes: 3 additions & 2 deletions src/geometry/isometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub type OwnedIsometryBase<N, D, A, R> =

/// A direct isometry, i.e., a rotation followed by a translation.
#[repr(C)]
#[derive(Hash, Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Hash, Debug, Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct IsometryBase<N: Scalar, D: DimName, S, R> {
/// The pure rotational part of this isometry.
pub rotation: R,
Expand All @@ -27,7 +28,7 @@ pub struct IsometryBase<N: Scalar, D: DimName, S, R> {


// One dummy private field just to prevent explicit construction.
#[serde(skip_serializing, skip_deserializing)]
#[cfg_attr(feature = "serde-serialize", serde(skip_serializing, skip_deserializing))]
_noconstruct: PhantomData<N>
}

Expand Down
3 changes: 2 additions & 1 deletion src/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Data structures for points and usual transformations (rotations, isometries, etc.)
//! [Reexported at the root of this crate.] Data structures for points and usual transformations
//! (rotations, isometries, etc.)
mod op_macros;

Expand Down
3 changes: 2 additions & 1 deletion src/geometry/orthographic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use core::helper;
use geometry::{PointBase, OwnedPoint};

/// A 3D orthographic projection stored as an homogeneous 4x4 matrix.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] // FIXME: Hash
#[derive(Debug, Clone, Copy)] // FIXME: Hash
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct OrthographicBase<N: Scalar, S: Storage<N, U4, U4>> {
matrix: SquareMatrix<N, U4, S>
}
Expand Down
3 changes: 2 additions & 1 deletion src/geometry/perspective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use core::helper;
use geometry::{PointBase, OwnedPoint};

/// A 3D perspective projection stored as an homogeneous 4x4 matrix.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] // FIXME: Hash
#[derive(Debug, Clone, Copy)] // FIXME: Hash
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct PerspectiveBase<N: Scalar, S: Storage<N, U4, U4>> {
matrix: SquareMatrix<N, U4, S>
}
Expand Down
3 changes: 2 additions & 1 deletion src/geometry/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub type OwnedPoint<N, D, A> = PointBase<N, D, <A as Allocator<N, D, U1>>::Buffe

/// A point in a n-dimensional euclidean space.
#[repr(C)]
#[derive(Hash, Debug, Serialize, Deserialize)]
#[derive(Hash, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct PointBase<N: Scalar, D: DimName, S: Storage<N, D, U1>> {
/// The coordinates of this point, i.e., the shift from the origin.
pub coords: ColumnVector<N, D, S>
Expand Down
Loading

0 comments on commit 42b4856

Please sign in to comment.