diff --git a/Cargo.toml b/Cargo.toml index 99a7cd2..64f9756 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "euclid" -version = "0.8.1" +version = "0.9.0" authors = ["The Servo Project Developers"] description = "Geometry primitives" documentation = "http://doc.servo.org/euclid/" diff --git a/src/length.rs b/src/length.rs index 95f5c2d..b9e48ee 100644 --- a/src/length.rs +++ b/src/length.rs @@ -36,9 +36,17 @@ pub struct UnknownUnit; /// another. See the `ScaleFactor` docs for an example. // Uncomment the derive, and remove the macro call, once heapsize gets // PhantomData support. -#[derive(Clone, Copy, RustcDecodable, RustcEncodable)] +#[derive(RustcDecodable, RustcEncodable)] pub struct Length(pub T, PhantomData); +impl Clone for Length { + fn clone(&self) -> Self { + Length(self.0.clone(), PhantomData) + } +} + +impl Copy for Length {} + impl HeapSizeOf for Length { fn heap_size_of_children(&self) -> usize { self.0.heap_size_of_children() @@ -182,9 +190,7 @@ mod tests { use super::Length; use scale_factor::ScaleFactor; - #[derive(Debug, Copy, Clone)] enum Inch {} - #[derive(Debug, Copy, Clone)] enum Mm {} #[test] diff --git a/src/macros.rs b/src/macros.rs index 53e6e73..8984c4e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -20,6 +20,17 @@ macro_rules! define_matrix { _unit: PhantomData<($($phantom),+)> } + impl Clone for $name { + fn clone(&self) -> Self { + $name { + $($field: self.$field.clone(),)+ + _unit: PhantomData, + } + } + } + + impl Copy for $name {} + impl ::heapsize::HeapSizeOf for $name where T: ::heapsize::HeapSizeOf { @@ -52,5 +63,24 @@ macro_rules! define_matrix { ($(&self.$field,)+).serialize(serializer) } } + + impl ::std::cmp::Eq for $name + where T: ::std::cmp::Eq {} + + impl ::std::cmp::PartialEq for $name + where T: ::std::cmp::PartialEq + { + fn eq(&self, other: &Self) -> bool { + true $(&& self.$field == other.$field)+ + } + } + + impl ::std::hash::Hash for $name + where T: ::std::hash::Hash + { + fn hash(&self, h: &mut H) { + $(self.$field.hash(h);)+ + } + } ) } diff --git a/src/matrix2d.rs b/src/matrix2d.rs index 9e4a521..9570742 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -16,7 +16,6 @@ use std::ops::{Add, Mul, Sub}; use std::marker::PhantomData; define_matrix! { - #[derive(Clone, Copy, Eq, Hash, PartialEq)] pub struct TypedMatrix2D { pub m11: T, pub m12: T, pub m21: T, pub m22: T, diff --git a/src/matrix4d.rs b/src/matrix4d.rs index d54484e..489c362 100644 --- a/src/matrix4d.rs +++ b/src/matrix4d.rs @@ -47,12 +47,6 @@ impl TypedMatrix4D { } } -impl Copy for TypedMatrix4D {} - -impl Clone for TypedMatrix4D { - fn clone(&self) -> Self { *self } -} - impl TypedMatrix4D where T: Copy + Clone + Add + @@ -423,15 +417,6 @@ impl fmt::Debug for TypedMatrix4D { } } -impl PartialEq for TypedMatrix4D { - fn eq(&self, other: &Self) -> bool { - self.m11 == other.m11 && self.m12 == other.m12 && self.m13 == other.m13 && self.m14 == other.m14 && - self.m21 == other.m21 && self.m22 == other.m22 && self.m23 == other.m23 && self.m24 == other.m24 && - self.m31 == other.m31 && self.m32 == other.m32 && self.m33 == other.m33 && self.m34 == other.m34 && - self.m41 == other.m41 && self.m42 == other.m42 && self.m43 == other.m43 && self.m44 == other.m44 - } -} - #[cfg(test)] mod tests { use point::Point2D; diff --git a/src/point.rs b/src/point.rs index 69810f3..f2afa64 100644 --- a/src/point.rs +++ b/src/point.rs @@ -16,8 +16,6 @@ use num_traits::{Float, NumCast}; use std::fmt; use std::ops::{Add, Neg, Mul, Sub, Div}; use std::marker::PhantomData; -use std::cmp::{PartialEq, Eq}; -use std::hash::{Hash, Hasher}; define_matrix! { #[derive(RustcDecodable, RustcEncodable)] @@ -29,29 +27,6 @@ define_matrix! { pub type Point2D = TypedPoint2D; -impl Copy for TypedPoint2D {} - -impl Clone for TypedPoint2D { - fn clone(&self) -> TypedPoint2D { - TypedPoint2D::new(self.x.clone(), self.y.clone()) - } -} - -impl PartialEq> for TypedPoint2D { - fn eq(&self, other: &TypedPoint2D) -> bool { - self.x.eq(&other.x) && self.y.eq(&other.y) - } -} - -impl Eq for TypedPoint2D {} - -impl Hash for TypedPoint2D { - fn hash(&self, h: &mut H) { - self.x.hash(h); - self.y.hash(h); - } -} - impl TypedPoint2D { pub fn zero() -> TypedPoint2D { TypedPoint2D::new(Zero::zero(), Zero::zero()) @@ -223,14 +198,6 @@ define_matrix! { pub type Point3D = TypedPoint3D; -impl Hash for TypedPoint3D { - fn hash(&self, h: &mut H) { - self.x.hash(h); - self.y.hash(h); - self.z.hash(h); - } -} - impl TypedPoint3D { #[inline] pub fn zero() -> TypedPoint3D { @@ -238,22 +205,6 @@ impl TypedPoint3D { } } -impl Copy for TypedPoint3D {} - -impl Clone for TypedPoint3D { - fn clone(&self) -> TypedPoint3D { - TypedPoint3D::new(self.x.clone(), self.y.clone(), self.z.clone()) - } -} - -impl PartialEq> for TypedPoint3D { - fn eq(&self, other: &TypedPoint3D) -> bool { - self.x.eq(&other.x) && self.y.eq(&other.y) && self.z.eq(&other.z) - } -} - -impl Eq for TypedPoint3D {} - impl fmt::Debug for TypedPoint3D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "({:?},{:?},{:?})", self.x, self.y, self.z) @@ -367,34 +318,6 @@ define_matrix! { pub type Point4D = TypedPoint4D; -impl Copy for TypedPoint4D {} - -impl Clone for TypedPoint4D { - fn clone(&self) -> TypedPoint4D { - TypedPoint4D::new(self.x.clone(), - self.y.clone(), - self.z.clone(), - self.w.clone()) - } -} - -impl PartialEq> for TypedPoint4D { - fn eq(&self, other: &TypedPoint4D) -> bool { - self.x.eq(&other.x) && self.y.eq(&other.y) && self.z.eq(&other.z) && self.w.eq(&other.w) - } -} - -impl Eq for TypedPoint4D {} - -impl Hash for TypedPoint4D { - fn hash(&self, h: &mut H) { - self.x.hash(h); - self.y.hash(h); - self.z.hash(h); - self.w.hash(h); - } -} - impl TypedPoint4D { #[inline] pub fn zero() -> TypedPoint4D { @@ -543,9 +466,7 @@ mod typedpoint2d { use super::TypedPoint2D; use scale_factor::ScaleFactor; - #[derive(Debug, Copy, Clone)] pub enum Mm {} - #[derive(Debug, Copy, Clone)] pub enum Cm {} pub type Point2DMm = TypedPoint2D; diff --git a/src/scale_factor.rs b/src/scale_factor.rs index 358ea49..bd00488 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -13,6 +13,7 @@ use num::One; use heapsize::HeapSizeOf; use num_traits::NumCast; use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use std::fmt; use std::ops::{Add, Mul, Sub, Div}; use std::marker::PhantomData; @@ -35,9 +36,7 @@ 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)] +#[derive(RustcDecodable, RustcEncodable)] pub struct ScaleFactor(pub T, PhantomData<(Src, Dst)>); impl HeapSizeOf for ScaleFactor { @@ -117,9 +116,9 @@ impl ScaleFactor { // FIXME: Switch to `derive(PartialEq, Clone)` after this Rust issue is fixed: // https://github.com/mozilla/rust/issues/7671 -impl PartialEq for ScaleFactor { +impl PartialEq for ScaleFactor { fn eq(&self, other: &ScaleFactor) -> bool { - self.get().eq(&other.get()) + self.0 == other.0 } } @@ -129,15 +128,26 @@ impl Clone for ScaleFactor { } } +impl Copy for ScaleFactor {} + +impl fmt::Debug for ScaleFactor { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Display for ScaleFactor { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + #[cfg(test)] mod tests { use super::ScaleFactor; - #[derive(Debug)] enum Inch {} - #[derive(Debug)] enum Cm {} - #[derive(Debug)] enum Mm {} #[test] diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 4336054..fc42110 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -11,6 +11,7 @@ //! and margins in CSS. use num::Zero; +use std::fmt; use std::ops::Add; use std::marker::PhantomData; use length::{Length, UnknownUnit}; @@ -21,7 +22,6 @@ use heapsize::HeapSizeOf; /// A group of side offsets, which correspond to top/left/bottom/right for borders, padding, /// and margins in CSS. define_matrix! { - #[derive(Clone, Debug)] pub struct TypedSideOffsets2D { pub top: T, pub right: T, @@ -30,6 +30,13 @@ define_matrix! { } } +impl fmt::Debug for TypedSideOffsets2D { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({:?},{:?},{:?},{:?})", + self.top, self.right, self.bottom, self.left) + } +} + pub type SideOffsets2D = TypedSideOffsets2D; impl TypedSideOffsets2D { diff --git a/src/size.rs b/src/size.rs index a47efe3..d8a53be 100644 --- a/src/size.rs +++ b/src/size.rs @@ -26,22 +26,6 @@ define_matrix! { pub type Size2D = TypedSize2D; -impl Copy for TypedSize2D {} - -impl Clone for TypedSize2D { - fn clone(&self) -> TypedSize2D { - TypedSize2D::new(self.width.clone(), self.height.clone()) - } -} - -impl PartialEq> for TypedSize2D { - fn eq(&self, other: &TypedSize2D) -> bool { - self.width.eq(&other.width) && self.height.eq(&other.height) - } -} - -impl Eq for TypedSize2D {} - impl fmt::Debug for TypedSize2D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}×{:?}", self.width, self.height)