From 2550ee7cf8192dbd41dd2f3ffd5c62884cac2a29 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 13 Aug 2015 19:23:58 -0700 Subject: [PATCH 1/3] Add `HeapSizeOf` support. Part of servo/heapsize#5. --- Cargo.toml | 13 +++++++++++++ src/length.rs | 1 + src/lib.rs | 5 +++++ src/matrix.rs | 1 + src/matrix2d.rs | 1 + src/point.rs | 3 +++ src/rect.rs | 1 + src/scale_factor.rs | 1 + src/side_offsets.rs | 2 ++ src/size.rs | 1 + 10 files changed, 29 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index b9baaab3..70c35c8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,9 @@ documentation = "http://doc.servo.org/euclid/" repository = "https://github.com/servo/euclid" license = "MIT / Apache-2.0" +[features] +heap_size = [ "heapsize", "heapsize_plugin" ] + [dependencies] rustc-serialize = "0.3.2" rand = "0.3.7" @@ -14,3 +17,13 @@ num = "0.1.24" log = "0.3.1" serde = "*" serde_macros = "*" + +[dependencies.heapsize] +#version = "0.1.1" +path = "../heapsize" +optional = true + +[dependencies.heapsize_plugin] +version = "0.0.1" +optional = true + diff --git a/src/length.rs b/src/length.rs index 15f98fe4..59c24461 100644 --- a/src/length.rs +++ b/src/length.rs @@ -29,6 +29,7 @@ use std::marker::PhantomData; /// You can multiply a Length by a `scale_factor::ScaleFactor` to convert it from one unit to /// another. See the ScaleFactor docs for an example. #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Length(pub T, PhantomData); impl Deserialize for Length where T: Deserialize { diff --git a/src/lib.rs b/src/lib.rs index ff5e8645..f433d328 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,11 @@ #![plugin(serde_macros)] +#![cfg_attr(feature = "heap_size", plugin(heapsize_plugin))] + +#[cfg(feature = "heap_size")] +extern crate heapsize; + #[macro_use] extern crate log; extern crate rustc_serialize; diff --git a/src/matrix.rs b/src/matrix.rs index 7cf8afe1..6db23617 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -12,6 +12,7 @@ use point::{Point2D, Point4D}; #[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Matrix4 { pub m11: f32, pub m12: f32, pub m13: f32, pub m14: f32, pub m21: f32, pub m22: f32, pub m23: f32, pub m24: f32, diff --git a/src/matrix2d.rs b/src/matrix2d.rs index f20c52da..e6cf5aa8 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -14,6 +14,7 @@ use size::Size2D; use std::ops::{Add, Mul, Sub}; #[derive(Clone, Copy, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Matrix2D { m11: T, m12: T, m21: T, m22: T, diff --git a/src/point.rs b/src/point.rs index 903d0281..a7274f1c 100644 --- a/src/point.rs +++ b/src/point.rs @@ -16,6 +16,7 @@ use std::fmt::{self, Formatter}; use std::ops::{Add, Neg, Mul, Sub, Div}; #[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Point2D { pub x: T, pub y: T @@ -139,6 +140,7 @@ impl Point2D> { } #[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Point3D { pub x: T, pub y: T, @@ -198,6 +200,7 @@ impl > Neg for Point3D { } #[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Point4D { pub x: T, pub y: T, diff --git a/src/rect.rs b/src/rect.rs index 51bd6727..de183de6 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -18,6 +18,7 @@ use std::fmt::{self, Formatter}; use std::ops::{Add, Sub, Mul, Div}; #[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Rect { pub origin: Point2D, pub size: Size2D, diff --git a/src/scale_factor.rs b/src/scale_factor.rs index 9aceb7ef..e23c422d 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -35,6 +35,7 @@ use std::marker::PhantomData; /// let one_foot_in_mm: Length = one_foot * mm_per_inch; /// ``` #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct ScaleFactor(pub T, PhantomData<(Src, Dst)>); impl Deserialize for ScaleFactor where T: Deserialize { diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 3e44b635..c0811041 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -16,6 +16,7 @@ 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, PartialEq, Debug, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct SideOffsets2D { pub top: T, pub right: T, @@ -76,6 +77,7 @@ impl SideOffsets2D { /// A SIMD enabled version of SideOffsets2D specialized for i32. #[derive(Clone, Copy, PartialEq)] #[simd] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct SideOffsets2DSimdI32 { pub top: i32, pub bottom: i32, diff --git a/src/size.rs b/src/size.rs index 2234aed5..434cd032 100644 --- a/src/size.rs +++ b/src/size.rs @@ -15,6 +15,7 @@ use std::fmt::{self, Formatter}; use std::ops::{Mul, Div}; #[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Size2D { pub width: T, pub height: T From b2c61603b54b1875207ca30935c315427e300401 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 13 Aug 2015 19:34:58 -0700 Subject: [PATCH 2/3] Make it work with the current version of heapsize. --- Cargo.toml | 3 +-- src/length.rs | 6 +++++- src/lib.rs | 1 + src/scale_factor.rs | 7 ++++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70c35c8f..2be51ee7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,7 @@ serde = "*" serde_macros = "*" [dependencies.heapsize] -#version = "0.1.1" -path = "../heapsize" +version = "0.1.1" optional = true [dependencies.heapsize_plugin] diff --git a/src/length.rs b/src/length.rs index 59c24461..9daf1f67 100644 --- a/src/length.rs +++ b/src/length.rs @@ -28,9 +28,13 @@ use std::marker::PhantomData; /// /// You can multiply a Length by a `scale_factor::ScaleFactor` to convert it from one unit to /// another. See the ScaleFactor docs for an example. +// Uncomment the derive, and remove the macro call, once heapsize gets +// PhantomData support. #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] -#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] +//#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Length(pub T, PhantomData); +#[cfg(feature = "heap_size")] +known_heap_size!(0, Length); impl Deserialize for Length where T: Deserialize { fn deserialize(deserializer: &mut D) -> Result,D::Error> diff --git a/src/lib.rs b/src/lib.rs index f433d328..fbe96bf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ #![cfg_attr(feature = "heap_size", plugin(heapsize_plugin))] #[cfg(feature = "heap_size")] +#[macro_use] extern crate heapsize; #[macro_use] diff --git a/src/scale_factor.rs b/src/scale_factor.rs index e23c422d..fbddb41f 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -34,9 +34,14 @@ use std::marker::PhantomData; /// let one_foot: Length = Length::new(12.0); /// let one_foot_in_mm: Length = one_foot * mm_per_inch; /// ``` +// Uncomment the derive, and remove the macro call, once heapsize gets +// PhantomData support. #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] -#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] +//#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct ScaleFactor(pub T, PhantomData<(Src, Dst)>); +#[cfg(feature = "heap_size")] +known_heap_size!(0, ScaleFactor); + impl Deserialize for ScaleFactor where T: Deserialize { fn deserialize(deserializer: &mut D) -> Result,D::Error> From 850f8a5165f222d0e67376fb232c3cac35b393fe Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 14 Aug 2015 09:24:51 -0700 Subject: [PATCH 3/3] Upgrade to heapsize 0.1.2. --- Cargo.toml | 2 +- src/length.rs | 4 +--- src/scale_factor.rs | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2be51ee7..43400990 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ serde = "*" serde_macros = "*" [dependencies.heapsize] -version = "0.1.1" +version = "0.1.2" optional = true [dependencies.heapsize_plugin] diff --git a/src/length.rs b/src/length.rs index 9daf1f67..9eac5417 100644 --- a/src/length.rs +++ b/src/length.rs @@ -31,10 +31,8 @@ use std::marker::PhantomData; // Uncomment the derive, and remove the macro call, once heapsize gets // PhantomData support. #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] -//#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct Length(pub T, PhantomData); -#[cfg(feature = "heap_size")] -known_heap_size!(0, Length); impl Deserialize for Length where T: Deserialize { fn deserialize(deserializer: &mut D) -> Result,D::Error> diff --git a/src/scale_factor.rs b/src/scale_factor.rs index fbddb41f..6aa146e9 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -37,10 +37,8 @@ use std::marker::PhantomData; // Uncomment the derive, and remove the macro call, once heapsize gets // PhantomData support. #[derive(Copy, RustcDecodable, RustcEncodable, Debug)] -//#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] +#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))] pub struct ScaleFactor(pub T, PhantomData<(Src, Dst)>); -#[cfg(feature = "heap_size")] -known_heap_size!(0, ScaleFactor); impl Deserialize for ScaleFactor where T: Deserialize {