Skip to content
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

Make heapsize and serde support unconditional but without plugins #139

Merged
merged 2 commits into from Jun 10, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -1,6 +1,6 @@
[package]
name = "euclid"
version = "0.6.7"
version = "0.6.8"
authors = ["The Servo Project Developers"]
description = "Geometry primitives"
documentation = "http://doc.servo.org/euclid/"
@@ -9,23 +9,15 @@ license = "MIT / Apache-2.0"

[features]
default = []
plugins = ["serde", "serde_macros", "heapsize", "heapsize_plugin"]
plugins = []
unstable = []

[dependencies]
heapsize = ">=0.2, <0.4"
rustc-serialize = "0.3.2"
num-traits = {version = "0.1.32", default-features = false}
log = "0.3.1"
serde = {version = ">=0.6, <0.8", optional = true}
serde_macros = {version = ">=0.6, <0.8", optional = true}

[dependencies.heapsize]
version = ">=0.2, <0.4"
optional = true

[dependencies.heapsize_plugin]
version = "0.1.3"
optional = true
serde = ">=0.6, <0.8"

[dev-dependencies]
rand = "0.3.7"
@@ -11,8 +11,8 @@
use scale_factor::ScaleFactor;
use num::Zero;

use heapsize::HeapSizeOf;
use num_traits::NumCast;
#[cfg(feature = "plugins")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::Ordering;
use std::ops::{Add, Sub, Mul, Div, Neg};
@@ -33,19 +33,22 @@ use std::marker::PhantomData;
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
#[derive(Copy, RustcDecodable, RustcEncodable, Debug)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct Length<Unit, T>(pub T, PhantomData<Unit>);

#[cfg(feature = "plugins")]
impl<Unit,T> Deserialize for Length<Unit,T> where T: Deserialize {
impl<Unit, T: HeapSizeOf> HeapSizeOf for Length<Unit, T> {
fn heap_size_of_children(&self) -> usize {
self.0.heap_size_of_children()
}
}

impl<Unit, T> Deserialize for Length<Unit, T> where T: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<Length<Unit,T>,D::Error>
where D: Deserializer {
Ok(Length(try!(Deserialize::deserialize(deserializer)), PhantomData))
}
}

#[cfg(feature = "plugins")]
impl<Unit,T> Serialize for Length<Unit,T> where T: Serialize {
impl<Unit, T> Serialize for Length<Unit, T> where T: Serialize {
fn serialize<S>(&self, serializer: &mut S) -> Result<(),S::Error> where S: Serializer {
self.0.serialize(serializer)
}
@@ -9,18 +9,11 @@

#![cfg_attr(feature = "unstable", feature(asm, repr_simd, test))]

#![cfg_attr(feature = "plugins", feature(custom_derive, plugin))]
#![cfg_attr(feature = "plugins", plugin(heapsize_plugin))]
#![cfg_attr(feature = "plugins", plugin(serde_macros))]

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

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

#[cfg(test)]
@@ -42,6 +35,8 @@ pub use size::Size2D;

pub mod approxeq;
pub mod length;
#[macro_use]
mod macros;
pub mod matrix;
pub mod matrix2d;
pub mod matrix4d;
@@ -0,0 +1,44 @@
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! define_matrix {
($(#[$attr:meta])* pub struct $name:ident <T> { $(pub $field:ident: T,)+ }) => (
$(#[$attr])*
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct $name<T> {
$(pub $field: T,)+
}

impl<T: ::heapsize::HeapSizeOf> ::heapsize::HeapSizeOf for $name<T> {
fn heap_size_of_children(&self) -> usize {
$(self.$field.heap_size_of_children() +)+ 0
}
}

impl<T: ::serde::Deserialize> ::serde::Deserialize for $name<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: ::serde::Deserializer
{
$(let $field = try!(T::deserialize(deserializer));)+
Ok($name {
$($field: $field,)+
})
}
}

impl<T: ::serde::Serialize> ::serde::Serialize for $name<T> {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: ::serde::Serializer
{
$(try!(self.$field.serialize(serializer));)+
Ok(())
}
}
)
}
@@ -13,12 +13,12 @@ use rect::Rect;
use size::Size2D;
use std::ops::{Add, Mul, Sub};

#[derive(Clone, Copy)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Matrix2D<T> {
m11: T, m12: T,
m21: T, m22: T,
m31: T, m32: T
define_matrix! {
pub struct Matrix2D<T> {
pub m11: T, pub m12: T,
pub m21: T, pub m22: T,
pub m31: T, pub m32: T,
}
}

impl<T:Add<T, Output=T> +
@@ -13,13 +13,14 @@ use point::{Point2D, Point4D};
use num::{One, Zero};
use std::ops::{Add, Mul, Sub, Div, Neg};

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Matrix4D<T> {
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
pub m31: T, pub m32: T, pub m33: T, pub m34: T,
pub m41: T, pub m42: T, pub m43: T, pub m44: T,
define_matrix! {
#[derive(Debug)]
pub struct Matrix4D<T> {
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
pub m31: T, pub m32: T, pub m33: T, pub m34: T,
pub m41: T, pub m42: T, pub m43: T, pub m44: T,
}
}

impl <T:Add<T, Output=T> +
@@ -15,11 +15,12 @@ use num_traits::{Float, NumCast};
use std::fmt;
use std::ops::{Add, Neg, Mul, Sub, Div};

#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Point2D<T> {
pub x: T,
pub y: T
define_matrix! {
#[derive(RustcDecodable, RustcEncodable)]
pub struct Point2D<T> {
pub x: T,
pub y: T,
}
}

impl<T: Zero> Point2D<T> {
@@ -164,12 +165,13 @@ impl<Unit, T: NumCast + Clone> Point2D<Length<Unit, T>> {
}
}

#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct Point3D<T> {
pub x: T,
pub y: T,
pub z: T,
define_matrix! {
#[derive(RustcDecodable, RustcEncodable)]
pub struct Point3D<T> {
pub x: T,
pub y: T,
pub z: T,
}
}

impl<T: Zero> Point3D<T> {
@@ -257,13 +259,14 @@ impl<T: Float> Point3D<T> {
}
}

#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct Point4D<T> {
pub x: T,
pub y: T,
pub z: T,
pub w: T,
define_matrix! {
#[derive(RustcDecodable, RustcEncodable)]
pub struct Point4D<T> {
pub x: T,
pub y: T,
pub z: T,
pub w: T,
}
}

impl<T: Zero> Point4D<T> {
@@ -12,18 +12,48 @@ use num::Zero;
use point::Point2D;
use size::Size2D;

use heapsize::HeapSizeOf;
use num_traits::NumCast;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::PartialOrd;
use std::fmt;
use std::ops::{Add, Sub, Mul, Div};

#[derive(Clone, Copy, Eq, RustcDecodable, RustcEncodable, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Rect<T> {
pub origin: Point2D<T>,
pub size: Size2D<T>,
}

impl<T: HeapSizeOf> HeapSizeOf for Rect<T> {
fn heap_size_of_children(&self) -> usize {
self.origin.heap_size_of_children() + self.size.heap_size_of_children()
}
}

impl<T: Deserialize> Deserialize for Rect<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer
{
let origin = try!(Deserialize::deserialize(deserializer));
let size = try!(Deserialize::deserialize(deserializer));
Ok(Rect {
origin: origin,
size: size,
})
}
}

impl<T: Serialize> Serialize for Rect<T> {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer
{
try!(self.origin.serialize(serializer));
try!(self.size.serialize(serializer));
Ok(())
}
}

impl<T: fmt::Debug> fmt::Debug for Rect<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Rect({:?} at {:?})", self.size, self.origin)
@@ -10,8 +10,8 @@

use num::One;

use heapsize::HeapSizeOf;
use num_traits::NumCast;
#[cfg(feature = "plugins")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::ops::{Add, Mul, Sub, Div};
use std::marker::PhantomData;
@@ -38,18 +38,22 @@ use std::marker::PhantomData;
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
#[derive(Copy, RustcDecodable, RustcEncodable, Debug)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct ScaleFactor<Src, Dst, T>(pub T, PhantomData<(Src, Dst)>);

#[cfg(feature = "plugins")]
impl<Src,Dst,T> Deserialize for ScaleFactor<Src,Dst,T> where T: Deserialize {
impl<Src, Dst, T: HeapSizeOf> HeapSizeOf for ScaleFactor<Src, Dst, T> {
fn heap_size_of_children(&self) -> usize {
self.0.heap_size_of_children()
}
}

impl<Src, Dst, T> Deserialize for ScaleFactor<Src, Dst, T> where T: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<ScaleFactor<Src,Dst,T>,D::Error>
where D: Deserializer {
Ok(ScaleFactor(try!(Deserialize::deserialize(deserializer)), PhantomData))
}
}
#[cfg(feature = "plugins")]
impl<Src,Dst,T> Serialize for ScaleFactor<Src,Dst,T> where T: Serialize {

impl<Src, Dst, T> Serialize for ScaleFactor<Src, Dst, T> where T: Serialize {
fn serialize<S>(&self, serializer: &mut S) -> Result<(),S::Error> where S: Serializer {
self.0.serialize(serializer)
}
@@ -10,18 +10,20 @@
//! A group of side offsets, which correspond to top/left/bottom/right for borders, padding,
//! and margins in CSS.

use heapsize::HeapSizeOf;
use num::Zero;
use std::ops::Add;

/// A group of side offsets, which correspond to top/left/bottom/right for borders, padding,
/// and margins in CSS.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct SideOffsets2D<T> {
pub top: T,
pub right: T,
pub bottom: T,
pub left: T,
define_matrix! {
#[derive(Debug)]
pub struct SideOffsets2D<T> {
pub top: T,
pub right: T,
pub bottom: T,
pub left: T,
}
}

impl<T> SideOffsets2D<T> {
@@ -78,14 +80,18 @@ impl<T: Zero> SideOffsets2D<T> {
#[cfg(feature = "unstable")]
#[derive(Clone, Copy, PartialEq)]
#[repr(simd)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
pub struct SideOffsets2DSimdI32 {
pub top: i32,
pub bottom: i32,
pub right: i32,
pub left: i32,
}

#[cfg(feature = "unstable")]
impl HeapSizeOf for SideOffsets2DSimdI32 {
fn heap_size_of_children(&self) -> usize { 0 }
}

#[cfg(feature = "unstable")]
impl SideOffsets2DSimdI32 {
#[inline]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.