Skip to content

Commit dd7cbe2

Browse files
author
bors-servo
authored
Auto merge of #139 - nox:stable, r=metajack
Make heapsize and serde support unconditional but without plugins <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/euclid/139) <!-- Reviewable:end -->
2 parents feaf1d2 + 85d5f47 commit dd7cbe2

11 files changed

+155
-76
lines changed

Cargo.toml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "euclid"
3-
version = "0.6.7"
3+
version = "0.6.8"
44
authors = ["The Servo Project Developers"]
55
description = "Geometry primitives"
66
documentation = "http://doc.servo.org/euclid/"
@@ -9,23 +9,15 @@ license = "MIT / Apache-2.0"
99

1010
[features]
1111
default = []
12-
plugins = ["serde", "serde_macros", "heapsize", "heapsize_plugin"]
12+
plugins = []
1313
unstable = []
1414

1515
[dependencies]
16+
heapsize = ">=0.2, <0.4"
1617
rustc-serialize = "0.3.2"
1718
num-traits = {version = "0.1.32", default-features = false}
1819
log = "0.3.1"
19-
serde = {version = ">=0.6, <0.8", optional = true}
20-
serde_macros = {version = ">=0.6, <0.8", optional = true}
21-
22-
[dependencies.heapsize]
23-
version = ">=0.2, <0.4"
24-
optional = true
25-
26-
[dependencies.heapsize_plugin]
27-
version = "0.1.3"
28-
optional = true
20+
serde = ">=0.6, <0.8"
2921

3022
[dev-dependencies]
3123
rand = "0.3.7"

src/length.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use scale_factor::ScaleFactor;
1212
use num::Zero;
1313

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

39-
#[cfg(feature = "plugins")]
40-
impl<Unit,T> Deserialize for Length<Unit,T> where T: Deserialize {
38+
impl<Unit, T: HeapSizeOf> HeapSizeOf for Length<Unit, T> {
39+
fn heap_size_of_children(&self) -> usize {
40+
self.0.heap_size_of_children()
41+
}
42+
}
43+
44+
impl<Unit, T> Deserialize for Length<Unit, T> where T: Deserialize {
4145
fn deserialize<D>(deserializer: &mut D) -> Result<Length<Unit,T>,D::Error>
4246
where D: Deserializer {
4347
Ok(Length(try!(Deserialize::deserialize(deserializer)), PhantomData))
4448
}
4549
}
4650

47-
#[cfg(feature = "plugins")]
48-
impl<Unit,T> Serialize for Length<Unit,T> where T: Serialize {
51+
impl<Unit, T> Serialize for Length<Unit, T> where T: Serialize {
4952
fn serialize<S>(&self, serializer: &mut S) -> Result<(),S::Error> where S: Serializer {
5053
self.0.serialize(serializer)
5154
}

src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@
99

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

12-
#![cfg_attr(feature = "plugins", feature(custom_derive, plugin))]
13-
#![cfg_attr(feature = "plugins", plugin(heapsize_plugin))]
14-
#![cfg_attr(feature = "plugins", plugin(serde_macros))]
15-
16-
#[cfg(feature = "plugins")]
17-
#[macro_use]
1812
extern crate heapsize;
1913

2014
#[macro_use]
2115
extern crate log;
2216
extern crate rustc_serialize;
23-
#[cfg(feature = "plugins")]
2417
extern crate serde;
2518

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

4336
pub mod approxeq;
4437
pub mod length;
38+
#[macro_use]
39+
mod macros;
4540
pub mod matrix;
4641
pub mod matrix2d;
4742
pub mod matrix4d;

src/macros.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
macro_rules! define_matrix {
11+
($(#[$attr:meta])* pub struct $name:ident <T> { $(pub $field:ident: T,)+ }) => (
12+
$(#[$attr])*
13+
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
14+
pub struct $name<T> {
15+
$(pub $field: T,)+
16+
}
17+
18+
impl<T: ::heapsize::HeapSizeOf> ::heapsize::HeapSizeOf for $name<T> {
19+
fn heap_size_of_children(&self) -> usize {
20+
$(self.$field.heap_size_of_children() +)+ 0
21+
}
22+
}
23+
24+
impl<T: ::serde::Deserialize> ::serde::Deserialize for $name<T> {
25+
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
26+
where D: ::serde::Deserializer
27+
{
28+
$(let $field = try!(T::deserialize(deserializer));)+
29+
Ok($name {
30+
$($field: $field,)+
31+
})
32+
}
33+
}
34+
35+
impl<T: ::serde::Serialize> ::serde::Serialize for $name<T> {
36+
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
37+
where S: ::serde::Serializer
38+
{
39+
$(try!(self.$field.serialize(serializer));)+
40+
Ok(())
41+
}
42+
}
43+
)
44+
}

src/matrix2d.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use rect::Rect;
1313
use size::Size2D;
1414
use std::ops::{Add, Mul, Sub};
1515

16-
#[derive(Clone, Copy)]
17-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
18-
pub struct Matrix2D<T> {
19-
m11: T, m12: T,
20-
m21: T, m22: T,
21-
m31: T, m32: T
16+
define_matrix! {
17+
pub struct Matrix2D<T> {
18+
pub m11: T, pub m12: T,
19+
pub m21: T, pub m22: T,
20+
pub m31: T, pub m32: T,
21+
}
2222
}
2323

2424
impl<T:Add<T, Output=T> +

src/matrix4d.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ use point::{Point2D, Point4D};
1313
use num::{One, Zero};
1414
use std::ops::{Add, Mul, Sub, Div, Neg};
1515

16-
#[derive(Debug, Copy, Clone, PartialEq)]
17-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
18-
pub struct Matrix4D<T> {
19-
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
20-
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
21-
pub m31: T, pub m32: T, pub m33: T, pub m34: T,
22-
pub m41: T, pub m42: T, pub m43: T, pub m44: T,
16+
define_matrix! {
17+
#[derive(Debug)]
18+
pub struct Matrix4D<T> {
19+
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
20+
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
21+
pub m31: T, pub m32: T, pub m33: T, pub m34: T,
22+
pub m41: T, pub m42: T, pub m43: T, pub m44: T,
23+
}
2324
}
2425

2526
impl <T:Add<T, Output=T> +

src/point.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use num_traits::{Float, NumCast};
1515
use std::fmt;
1616
use std::ops::{Add, Neg, Mul, Sub, Div};
1717

18-
#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
19-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
20-
pub struct Point2D<T> {
21-
pub x: T,
22-
pub y: T
18+
define_matrix! {
19+
#[derive(RustcDecodable, RustcEncodable)]
20+
pub struct Point2D<T> {
21+
pub x: T,
22+
pub y: T,
23+
}
2324
}
2425

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

167-
#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
168-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
169-
pub struct Point3D<T> {
170-
pub x: T,
171-
pub y: T,
172-
pub z: T,
168+
define_matrix! {
169+
#[derive(RustcDecodable, RustcEncodable)]
170+
pub struct Point3D<T> {
171+
pub x: T,
172+
pub y: T,
173+
pub z: T,
174+
}
173175
}
174176

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

260-
#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)]
261-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
262-
pub struct Point4D<T> {
263-
pub x: T,
264-
pub y: T,
265-
pub z: T,
266-
pub w: T,
262+
define_matrix! {
263+
#[derive(RustcDecodable, RustcEncodable)]
264+
pub struct Point4D<T> {
265+
pub x: T,
266+
pub y: T,
267+
pub z: T,
268+
pub w: T,
269+
}
267270
}
268271

269272
impl<T: Zero> Point4D<T> {

src/rect.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,48 @@ use num::Zero;
1212
use point::Point2D;
1313
use size::Size2D;
1414

15+
use heapsize::HeapSizeOf;
1516
use num_traits::NumCast;
17+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1618
use std::cmp::PartialOrd;
1719
use std::fmt;
1820
use std::ops::{Add, Sub, Mul, Div};
1921

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

28+
impl<T: HeapSizeOf> HeapSizeOf for Rect<T> {
29+
fn heap_size_of_children(&self) -> usize {
30+
self.origin.heap_size_of_children() + self.size.heap_size_of_children()
31+
}
32+
}
33+
34+
impl<T: Deserialize> Deserialize for Rect<T> {
35+
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
36+
where D: Deserializer
37+
{
38+
let origin = try!(Deserialize::deserialize(deserializer));
39+
let size = try!(Deserialize::deserialize(deserializer));
40+
Ok(Rect {
41+
origin: origin,
42+
size: size,
43+
})
44+
}
45+
}
46+
47+
impl<T: Serialize> Serialize for Rect<T> {
48+
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
49+
where S: Serializer
50+
{
51+
try!(self.origin.serialize(serializer));
52+
try!(self.size.serialize(serializer));
53+
Ok(())
54+
}
55+
}
56+
2757
impl<T: fmt::Debug> fmt::Debug for Rect<T> {
2858
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2959
write!(f, "Rect({:?} at {:?})", self.size, self.origin)

src/scale_factor.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
1111
use num::One;
1212

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

44-
#[cfg(feature = "plugins")]
45-
impl<Src,Dst,T> Deserialize for ScaleFactor<Src,Dst,T> where T: Deserialize {
43+
impl<Src, Dst, T: HeapSizeOf> HeapSizeOf for ScaleFactor<Src, Dst, T> {
44+
fn heap_size_of_children(&self) -> usize {
45+
self.0.heap_size_of_children()
46+
}
47+
}
48+
49+
impl<Src, Dst, T> Deserialize for ScaleFactor<Src, Dst, T> where T: Deserialize {
4650
fn deserialize<D>(deserializer: &mut D) -> Result<ScaleFactor<Src,Dst,T>,D::Error>
4751
where D: Deserializer {
4852
Ok(ScaleFactor(try!(Deserialize::deserialize(deserializer)), PhantomData))
4953
}
5054
}
51-
#[cfg(feature = "plugins")]
52-
impl<Src,Dst,T> Serialize for ScaleFactor<Src,Dst,T> where T: Serialize {
55+
56+
impl<Src, Dst, T> Serialize for ScaleFactor<Src, Dst, T> where T: Serialize {
5357
fn serialize<S>(&self, serializer: &mut S) -> Result<(),S::Error> where S: Serializer {
5458
self.0.serialize(serializer)
5559
}

src/side_offsets.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
//! A group of side offsets, which correspond to top/left/bottom/right for borders, padding,
1111
//! and margins in CSS.
1212
13+
use heapsize::HeapSizeOf;
1314
use num::Zero;
1415
use std::ops::Add;
1516

1617
/// A group of side offsets, which correspond to top/left/bottom/right for borders, padding,
1718
/// and margins in CSS.
18-
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
19-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
20-
pub struct SideOffsets2D<T> {
21-
pub top: T,
22-
pub right: T,
23-
pub bottom: T,
24-
pub left: T,
19+
define_matrix! {
20+
#[derive(Debug)]
21+
pub struct SideOffsets2D<T> {
22+
pub top: T,
23+
pub right: T,
24+
pub bottom: T,
25+
pub left: T,
26+
}
2527
}
2628

2729
impl<T> SideOffsets2D<T> {
@@ -78,14 +80,18 @@ impl<T: Zero> SideOffsets2D<T> {
7880
#[cfg(feature = "unstable")]
7981
#[derive(Clone, Copy, PartialEq)]
8082
#[repr(simd)]
81-
#[cfg_attr(feature = "plugins", derive(HeapSizeOf))]
8283
pub struct SideOffsets2DSimdI32 {
8384
pub top: i32,
8485
pub bottom: i32,
8586
pub right: i32,
8687
pub left: i32,
8788
}
8889

90+
#[cfg(feature = "unstable")]
91+
impl HeapSizeOf for SideOffsets2DSimdI32 {
92+
fn heap_size_of_children(&self) -> usize { 0 }
93+
}
94+
8995
#[cfg(feature = "unstable")]
9096
impl SideOffsets2DSimdI32 {
9197
#[inline]

0 commit comments

Comments
 (0)