Skip to content

Commit

Permalink
Auto merge of #267 - pizzaiter:optional_serde, r=nical
Browse files Browse the repository at this point in the history
Make serde optional

Closes #264

This PR makes serde support optional. At the moment it is disabled by default.

I am not very sure about my changes to `.travis.yml` but it seems to work.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/euclid/267)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Jan 30, 2018
2 parents 98b16d6 + 977e136 commit 7bb2617
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
16 changes: 12 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ language: rust
notifications:
webhooks: http://build.servo.org:54856/travis

rust:
- 1.23.0
- stable
- beta
- nightly

env:
- FEATURES=""
- FEATURES="--features serde"

matrix:
include:
- rust: 1.21.0
- rust: stable
- rust: beta
- rust: nightly
- rust: nightly
env: FEATURES="--features unstable"
- rust: nightly
env: FEATURES="--features unstable,serde"

script:
- cargo build $FEATURES
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "euclid"
version = "0.16.4"
version = "0.17.0"
authors = ["The Servo Project Developers"]
description = "Geometry primitives"
documentation = "https://docs.rs/euclid/"
Expand All @@ -14,7 +14,7 @@ unstable = []

[dependencies]
num-traits = {version = "0.1.32", default-features = false}
serde = { version = "1.0", features = ["serde_derive"] }
serde = { version = "1.0", features = ["serde_derive"], optional = true }

[dev-dependencies]
rand = "0.3.7"
Expand Down
30 changes: 19 additions & 11 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use num::Zero;

use num_traits::{NumCast, Saturating};
use num::One;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::Ordering;
use std::ops::{Add, Div, Mul, Neg, Sub};
Expand Down Expand Up @@ -42,6 +43,7 @@ impl<T: Clone, Unit> Clone for Length<T, Unit> {

impl<T: Copy, Unit> Copy for Length<T, Unit> {}

#[cfg(feature = "serde")]
impl<'de, Unit, T> Deserialize<'de> for Length<T, Unit>
where
T: Deserialize<'de>,
Expand All @@ -57,6 +59,7 @@ where
}
}

#[cfg(feature = "serde")]
impl<T, Unit> Serialize for Length<T, Unit>
where
T: Serialize,
Expand Down Expand Up @@ -260,15 +263,27 @@ mod tests {
use scale::TypedScale;
use std::f32::INFINITY;

extern crate serde_test;
use self::serde_test::Token;
use self::serde_test::assert_tokens;

enum Inch {}
enum Mm {}
enum Cm {}
enum Second {}

#[cfg(feature = "serde")]
mod serde {
use super::*;

extern crate serde_test;
use self::serde_test::Token;
use self::serde_test::assert_tokens;

#[test]
fn test_length_serde() {
let one_cm: Length<f32, Mm> = Length::new(10.0);

assert_tokens(&one_cm, &[Token::F32(10.0)]);
}
}

#[test]
fn test_clone() {
// A cloned Length is a separate length with the state matching the
Expand All @@ -282,13 +297,6 @@ mod tests {
assert_eq!(variable_length.get(), 24.0);
}

#[test]
fn test_length_serde() {
let one_cm: Length<f32, Mm> = Length::new(10.0);

assert_tokens(&one_cm, &[Token::F32(10.0)]);
}

#[test]
fn test_get_clones_length_value() {
// Calling get returns a clone of the Length's value.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
//! assert_eq!(p.x, p.x_typed().get());
//! ```

#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

Expand Down
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro_rules! define_matrix {

impl<T: Copy, $($phantom),+> Copy for $name<T, $($phantom),+> {}

#[cfg(feature = "serde")]
impl<'de, T, $($phantom),+> ::serde::Deserialize<'de> for $name<T, $($phantom),+>
where T: ::serde::Deserialize<'de>
{
Expand All @@ -47,6 +48,7 @@ macro_rules! define_matrix {
}
}

#[cfg(feature = "serde")]
impl<T, $($phantom),+> ::serde::Serialize for $name<T, $($phantom),+>
where T: ::serde::Serialize
{
Expand Down
3 changes: 3 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use side_offsets::TypedSideOffsets2D;
use size::TypedSize2D;

use num_traits::NumCast;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::PartialOrd;
use std::fmt;
Expand All @@ -33,6 +34,7 @@ pub struct TypedRect<T, U = UnknownUnit> {
/// The default rectangle type with no unit.
pub type Rect<T> = TypedRect<T, UnknownUnit>;

#[cfg(feature = "serde")]
impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedRect<T, U> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -43,6 +45,7 @@ impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedRect<T, U> {
}
}

#[cfg(feature = "serde")]
impl<T: Serialize, U> Serialize for TypedRect<T, U> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
3 changes: 2 additions & 1 deletion src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use {TypedPoint2D, TypedPoint3D, TypedVector2D, TypedVector3D, Vector3D, point2,
use {TypedTransform2D, TypedTransform3D, UnknownUnit};

/// An angle in radians
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle<T> {
pub radians: T,
}
Expand Down
3 changes: 3 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use num::One;

use num_traits::NumCast;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::ops::{Add, Div, Mul, Neg, Sub};
Expand Down Expand Up @@ -39,6 +40,7 @@ use {TypedPoint2D, TypedRect, TypedSize2D, TypedVector2D};
#[repr(C)]
pub struct TypedScale<T, Src, Dst>(pub T, PhantomData<(Src, Dst)>);

#[cfg(feature = "serde")]
impl<'de, T, Src, Dst> Deserialize<'de> for TypedScale<T, Src, Dst>
where
T: Deserialize<'de>,
Expand All @@ -54,6 +56,7 @@ where
}
}

#[cfg(feature = "serde")]
impl<T, Src, Dst> Serialize for TypedScale<T, Src, Dst>
where
T: Serialize,
Expand Down

0 comments on commit 7bb2617

Please sign in to comment.