From 9cc3c74e5adb57bdcc44a58737ab5370b1bf968a Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 15 Jan 2015 15:55:34 -0500 Subject: [PATCH 1/3] Fix prelude removals and obsolete features. --- src/length.rs | 2 ++ src/lib.rs | 4 ++-- src/matrix.rs | 3 ++- src/matrix2d.rs | 3 ++- src/point.rs | 1 + src/rect.rs | 1 + src/scale_factor.rs | 1 + src/side_offsets.rs | 4 ++-- src/size.rs | 1 + 9 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/length.rs b/src/length.rs index 4068108..2accefd 100644 --- a/src/length.rs +++ b/src/length.rs @@ -12,6 +12,8 @@ use scale_factor::ScaleFactor; use num::Zero; use std::num::{NumCast, cast}; +use std::cmp::Ordering; +use std::ops::{Add, Sub, Mul, Div, Neg}; /// A one-dimensional distance, with value represented by `T` and unit of measurement `Unit`. /// diff --git a/src/lib.rs b/src/lib.rs index f873bf8..5e8d59b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,9 @@ #![crate_name = "geom"] #![crate_type = "rlib"] -#![feature(asm, phase, simd)] +#![feature(asm, simd)] -#[phase(plugin, link)] +#[macro_use] extern crate log; extern crate serialize; diff --git a/src/matrix.rs b/src/matrix.rs index 6ccc563..51632dd 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -12,6 +12,7 @@ use num::{One, Zero}; use std::num; use std::num::NumCast; +use std::ops::{Add, Mul, Div, Neg, Sub}; pub fn Matrix4 + Clone + ApproxEq + Mul + One + Zero>( m11: T, m12: T, m13: T, m14: T, @@ -80,7 +81,7 @@ impl + Clone + ApproxEq + Mul + One + Zero> Matrix4 { self.m41.clone(), self.m42.clone(), self.m43.clone(), self.m44.clone()) } - pub fn to_array(&self) -> [T, ..16] { + pub fn to_array(&self) -> [T; 16] { [ self.m11.clone(), self.m12.clone(), self.m13.clone(), self.m14.clone(), self.m21.clone(), self.m22.clone(), self.m23.clone(), self.m24.clone(), diff --git a/src/matrix2d.rs b/src/matrix2d.rs index a1aca3c..0edc2c8 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -8,6 +8,7 @@ // except according to those terms. use num::{One, Zero}; +use std::ops::{Add, Mul}; #[deriving(Clone, Copy)] pub struct Matrix2D { @@ -55,7 +56,7 @@ impl + Clone + Mul + One + Zero> Matrix2D { _0.clone(), _0.clone()); } - pub fn to_array(&self) -> [T, ..6] { + pub fn to_array(&self) -> [T; 6] { [ self.m11.clone(), self.m12.clone(), self.m21.clone(), self.m22.clone(), diff --git a/src/point.rs b/src/point.rs index d8bc6e0..c05e70a 100644 --- a/src/point.rs +++ b/src/point.rs @@ -13,6 +13,7 @@ use num::Zero; use std::fmt; use std::num::NumCast; +use std::ops::{Add, Neg, Mul, Sub, Div}; #[deriving(Clone, Copy, Decodable, Encodable, Eq, Hash, PartialEq)] pub struct Point2D { diff --git a/src/rect.rs b/src/rect.rs index 3990dbd..a50fbaf 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -15,6 +15,7 @@ use size::Size2D; use std::cmp::PartialOrd; use std::fmt; use std::num::NumCast; +use std::ops::{Add, Sub, Mul, Div}; #[deriving(Clone, Copy, Decodable, Encodable, PartialEq)] pub struct Rect { diff --git a/src/scale_factor.rs b/src/scale_factor.rs index f3b0b0c..6aa1bbd 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -10,6 +10,7 @@ use num::One; use std::num::{NumCast, cast}; +use std::ops::{Add, Mul, Sub, Neg, Div}; /// A scaling factor between two different units of measurement. /// diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 4914f22..1b5a92f 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -11,7 +11,7 @@ //! and margins in CSS. use num::Zero; -use std::num::Num; +use std::ops::Add; /// A group of side offsets, which correspond to top/left/bottom/right for borders, padding, /// and margins in CSS. @@ -50,7 +50,7 @@ impl SideOffsets2D where T: Add { } } -impl Add, SideOffsets2D> for SideOffsets2D { +impl Add, SideOffsets2D> for SideOffsets2D { fn add(&self, other: &SideOffsets2D) -> SideOffsets2D { SideOffsets2D { top: self.top + other.top, diff --git a/src/size.rs b/src/size.rs index 23540a5..c85a7a2 100644 --- a/src/size.rs +++ b/src/size.rs @@ -12,6 +12,7 @@ use num::Zero; use std::fmt; use std::num::NumCast; +use std::ops::{Mul, Div}; #[deriving(Clone, Copy, Decodable, Encodable, PartialEq)] pub struct Size2D { From ae31880a538245e0a5e23c04b17a1efbf1a81136 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 16 Jan 2015 00:04:18 -0500 Subject: [PATCH 2/3] Fix up operator overloading. Doesn't build yet. --- src/length.rs | 29 +++++++++++++++++------------ src/lib.rs | 2 +- src/matrix.rs | 12 ++++++------ src/matrix2d.rs | 2 +- src/point.rs | 42 +++++++++++++++++++++++++----------------- src/rect.rs | 19 ++++++++++--------- src/scale_factor.rs | 16 +++++++--------- src/side_offsets.rs | 6 +++--- src/size.rs | 18 +++++++++--------- 9 files changed, 79 insertions(+), 67 deletions(-) diff --git a/src/length.rs b/src/length.rs index 2accefd..690f151 100644 --- a/src/length.rs +++ b/src/length.rs @@ -38,46 +38,51 @@ impl Length { } // length + length -impl> Add, Length> for Length { - fn add(&self, other: &Length) -> Length { +impl> Add for Length { + type Output = Length; + fn add(self, other: Length) -> Length { Length(self.get() + other.get()) } } // length - length -impl> Sub, Length> for Length { - fn sub(&self, other: &Length) -> Length { +impl> Sub> for Length { + type Output = Length; + fn sub(self, other: Length) -> ::Output { Length(self.get() - other.get()) } } // length * scaleFactor -impl> Mul, Length> for Length { +impl> Mul> for Length { + type Output = Length; #[inline] - fn mul(&self, scale: &ScaleFactor) -> Length { + fn mul(self, scale: ScaleFactor) -> Length { Length(self.get() * scale.get()) } } // length / scaleFactor -impl> Div, Length> for Length { +impl> Div> for Length { + type Output = Length; #[inline] - fn div(&self, scale: &ScaleFactor) -> Length { + fn div(self, scale: ScaleFactor) -> Length { Length(self.get() / scale.get()) } } // -length -impl > Neg> for Length { +impl > Neg for Length { + type Output = Length; #[inline] - fn neg(&self) -> Length { + fn neg(self) -> Length { Length(-self.get()) } } -impl Length { +impl Length { /// Cast from one numeric representation to another, preserving the units. - pub fn cast(&self) -> Option> { + pub fn cast(&self) -> Option> { cast(self.get()).map(|x| Length(x)) } } diff --git a/src/lib.rs b/src/lib.rs index 5e8d59b..ff7688b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ #![crate_name = "geom"] #![crate_type = "rlib"] -#![feature(asm, simd)] +#![feature(asm, simd, old_impl_check)] #[macro_use] extern crate log; diff --git a/src/matrix.rs b/src/matrix.rs index 51632dd..2699450 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -14,7 +14,7 @@ use std::num; use std::num::NumCast; use std::ops::{Add, Mul, Div, Neg, Sub}; -pub fn Matrix4 + Clone + ApproxEq + Mul + One + Zero>( +pub fn Matrix4 + Clone + ApproxEq + Mul + One + Zero>( m11: T, m12: T, m13: T, m14: T, m21: T, m22: T, m23: T, m24: T, m31: T, m32: T, m33: T, m34: T, @@ -36,7 +36,7 @@ pub struct Matrix4 { pub m41: T, pub m42: T, pub m43: T, pub m44: T, } -impl + Clone + ApproxEq + Mul + One + Zero> Matrix4 { +impl + Clone + ApproxEq + Mul + One + Zero> Matrix4 { pub fn approx_eq(&self, other: &Matrix4) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && self.m13.approx_eq(&other.m13) && self.m14.approx_eq(&other.m14) && @@ -101,8 +101,8 @@ impl + Clone + ApproxEq + Mul + One + Zero> Matrix4 { } } -pub fn ortho + Clone + Div + ApproxEq + Mul + Neg + NumCast + One + - Sub + Zero> +pub fn ortho + Clone + Div + ApproxEq + Mul + Neg + NumCast + One + + Sub + Zero> (left: T, right: T, bottom: T, @@ -110,7 +110,7 @@ pub fn ortho + Clone + Div + ApproxEq + Mul + Neg + N near: T, far: T) -> Matrix4 { - let _2: T = num::cast(2u).unwrap(); + let _2: T = num::cast(2us).unwrap(); let _1: T = One::one(); let _0: T = Zero::zero(); @@ -124,7 +124,7 @@ pub fn ortho + Clone + Div + ApproxEq + Mul + Neg + N tx, ty, tz, _1.clone()) } -pub fn identity + Clone + ApproxEq + Mul + One + Zero>() -> Matrix4 { +pub fn identity + Clone + ApproxEq + Mul + One + Zero>() -> Matrix4 { let (_0, _1): (T, T) = (Zero::zero(), One::one()); Matrix4(_1.clone(), _0.clone(), _0.clone(), _0.clone(), _0.clone(), _1.clone(), _0.clone(), _0.clone(), diff --git a/src/matrix2d.rs b/src/matrix2d.rs index 0edc2c8..c1c9d4b 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -17,7 +17,7 @@ pub struct Matrix2D { m31: T, m32: T } -impl + Clone + Mul + One + Zero> Matrix2D { +impl + Clone + Mul + One + Zero> Matrix2D { pub fn new(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> Matrix2D { Matrix2D { m11: m11, m12: m12, diff --git a/src/point.rs b/src/point.rs index c05e70a..18e36aa 100644 --- a/src/point.rs +++ b/src/point.rs @@ -29,7 +29,7 @@ impl Point2D { impl fmt::Show for Point2D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "({},{})", self.x, self.y) + write!(f, "({:?},{:?})", self.x, self.y) } } @@ -38,47 +38,55 @@ pub fn Point2D(x: T, y: T) -> Point2D { } -impl> Add, Point2D> for Point2D { - fn add(&self, other: &Point2D) -> Point2D { +impl> Add for Point2D { + type Output = Point2D; + fn add(self, other: Point2D) -> Point2D { Point2D(self.x + other.x, self.y + other.y) } } -impl> Add, Point2D> for Point2D { - fn add(&self, other: &Size2D) -> Point2D { +impl> Add> for Point2D { + type Output = Point2D; + fn add(self, other: Size2D) -> Point2D { Point2D(self.x + other.width, self.y + other.height) } } -impl> Point2D { +impl> Point2D { pub fn add_size(&self, other: &Size2D) -> Point2D { Point2D { x: self.x + other.width, y: self.y + other.height } } } -impl> Sub, Point2D> for Point2D { - fn sub(&self, other: &Point2D) -> Point2D { +impl> Sub for Point2D { + type Output = Point2D; + fn sub(self, other: Point2D) -> Point2D { Point2D(self.x - other.x, self.y - other.y) } } -impl > Neg> for Point2D { +impl > Neg for Point2D { + type Output = Point2D; #[inline] - fn neg(&self) -> Point2D { + fn neg(self) -> Point2D { Point2D(-self.x, -self.y) } } -impl, T1: Clone> Mul> for Point2D { +#[old_impl_check] +impl, T1: Clone> Mul for Point2D { + type Output = Point2D; #[inline] - fn mul(&self, scale: &Scale) -> Point2D { + fn mul(self, scale: Scale) -> Point2D { Point2D(self.x * *scale, self.y * *scale) } } -impl, T1: Clone> Div> for Point2D { +#[old_impl_check] +impl, T1: Clone> Div for Point2D { + type Output = Point2D; #[inline] - fn div(&self, scale: &Scale) -> Point2D { + fn div(self, scale: Scale) -> Point2D { Point2D(self.x / *scale, self.y / *scale) } } @@ -103,9 +111,9 @@ impl Point2D> { } } -impl Point2D> { +impl Point2D> { /// Cast from one numeric representation to another, preserving the units. - pub fn cast(&self) -> Option>> { + pub fn cast(&self) -> Option>> { match (self.x.cast(), self.y.cast()) { (Some(x), Some(y)) => Some(Point2D(x, y)), _ => None @@ -119,7 +127,7 @@ impl Point2D> { self.cast().unwrap() } - pub fn as_uint(&self) -> Point2D> { + pub fn as_uint(&self) -> Point2D> { self.cast().unwrap() } } diff --git a/src/rect.rs b/src/rect.rs index a50fbaf..d1cd52a 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -36,7 +36,7 @@ pub fn Rect(origin: Point2D, size: Size2D) -> Rect { } } -impl + Sub> Rect { +impl + Sub> Rect { #[inline] pub fn intersects(&self, other: &Rect) -> bool { self.origin.x < other.origin.x + other.size.width && @@ -123,7 +123,8 @@ impl + Sub> Rect { } } -impl> Rect { +#[old_impl_check] +impl> Rect { #[inline] pub fn scale(&self, x: Scale, y: Scale) -> Rect { Rect { @@ -155,16 +156,16 @@ pub fn max(x: T, y: T) -> T { if x >= y { x } else { y } } -impl, T1: Clone> Mul> for Rect { +impl> Mul for Rect { #[inline] - fn mul(&self, scale: &Scale) -> Rect { + fn mul(&self, scale: &Scale) -> Rect { Rect(self.origin * *scale, self.size * *scale) } } -impl, T1: Clone> Div> for Rect { +impl> Div for Rect { #[inline] - fn div(&self, scale: &Scale) -> Rect { + fn div(&self, scale: &Scale) -> Rect { Rect(self.origin / *scale, self.size / *scale) } } @@ -184,9 +185,9 @@ impl Rect> { } } -impl Rect> { +impl Rect> { /// Cast from one numeric representation to another, preserving the units. - pub fn cast(&self) -> Option>> { + pub fn cast(&self) -> Option>> { match (self.origin.cast(), self.size.cast()) { (Some(origin), Some(size)) => Some(Rect(origin, size)), _ => None @@ -200,7 +201,7 @@ impl Rect> { self.cast().unwrap() } - pub fn as_uint(&self) -> Rect> { + pub fn as_uint(&self) -> Rect> { self.cast().unwrap() } } diff --git a/src/scale_factor.rs b/src/scale_factor.rs index 6aa1bbd..604327d 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -42,7 +42,7 @@ impl ScaleFactor { } } -impl> ScaleFactor { +impl> ScaleFactor { /// The inverse ScaleFactor (1.0 / self). pub fn inv(&self) -> ScaleFactor { let one: T = One::one(); @@ -51,8 +51,8 @@ impl> ScaleFactor { } // scale0 * scale1 -impl> -Mul, ScaleFactor> for ScaleFactor { +impl> +Mul> for ScaleFactor { #[inline] fn mul(&self, other: &ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() * other.get()) @@ -60,8 +60,7 @@ Mul, ScaleFactor> for ScaleFactor { } // scale0 + scale1 -impl> -Add, ScaleFactor> for ScaleFactor { +impl> Add for ScaleFactor { #[inline] fn add(&self, other: &ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() + other.get()) @@ -69,17 +68,16 @@ Add, ScaleFactor> for ScaleFactor> -Sub, ScaleFactor> for ScaleFactor { +impl> Sub for ScaleFactor { #[inline] fn sub(&self, other: &ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() - other.get()) } } -impl ScaleFactor { +impl ScaleFactor { /// Cast from one numeric representation to another, preserving the units. - pub fn cast(&self) -> Option> { + pub fn cast(&self) -> Option> { cast(self.get()).map(|x| ScaleFactor(x)) } } diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 1b5a92f..cde6447 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -40,7 +40,7 @@ impl SideOffsets2D { } } -impl SideOffsets2D where T: Add { +impl SideOffsets2D where T: Add { pub fn horizontal(&self) -> T { self.left + self.right } @@ -50,7 +50,7 @@ impl SideOffsets2D where T: Add { } } -impl Add, SideOffsets2D> for SideOffsets2D { +impl Add for SideOffsets2D { fn add(&self, other: &SideOffsets2D) -> SideOffsets2D { SideOffsets2D { top: self.top + other.top, @@ -113,7 +113,7 @@ impl SideOffsets2DSimdI32 { } } -impl Add for SideOffsets2DSimdI32 { +impl Add for SideOffsets2DSimdI32 { #[inline] fn add(&self, other: &SideOffsets2DSimdI32) -> SideOffsets2DSimdI32 { *self + *other // Use SIMD addition diff --git a/src/size.rs b/src/size.rs index c85a7a2..c6d07bb 100644 --- a/src/size.rs +++ b/src/size.rs @@ -33,8 +33,8 @@ pub fn Size2D(width: T, height: T) -> Size2D { } } -impl, U> Size2D { - pub fn area(&self) -> U { self.width * self.height } +impl> Size2D { + pub fn area(&self) -> U { self.width * self.height } } impl Size2D { @@ -46,16 +46,16 @@ impl Size2D { } } -impl, T1: Clone> Mul> for Size2D { +impl> Mul for Size2D { #[inline] - fn mul(&self, scale: &Scale) -> Size2D { + fn mul(&self, scale: &Scale) -> Size2D { Size2D(self.width * *scale, self.height * *scale) } } -impl, T1: Clone> Div> for Size2D { +impl> Div for Size2D { #[inline] - fn div(&self, scale: &Scale) -> Size2D { + fn div(&self, scale: &Scale) -> Size2D { Size2D(self.width / *scale, self.height / *scale) } } @@ -80,9 +80,9 @@ impl Size2D> { } } -impl Size2D> { +impl Size2D> { /// Cast from one numeric representation to another, preserving the units. - pub fn cast(&self) -> Option>> { + pub fn cast(&self) -> Option>> { match (self.width.cast(), self.height.cast()) { (Some(w), Some(h)) => Some(Size2D(w, h)), _ => None @@ -96,7 +96,7 @@ impl Size2D> { self.cast().unwrap() } - pub fn as_uint(&self) -> Size2D> { + pub fn as_uint(&self) -> Size2D> { self.cast().unwrap() } } From a4a4a03aa024412bf3f4e093c0198b433c6ad63f Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 16 Jan 2015 00:38:57 -0500 Subject: [PATCH 3/3] More operator overloading and warning fixes. --- src/length.rs | 8 ++++---- src/lib.rs | 2 +- src/matrix.rs | 6 +++--- src/matrix2d.rs | 4 ++-- src/point.rs | 14 ++++++-------- src/rect.rs | 22 ++++++++++++---------- src/scale_factor.rs | 29 ++++++++++++++++------------- src/side_offsets.rs | 20 +++++++++++--------- src/size.rs | 22 ++++++++++++---------- 9 files changed, 67 insertions(+), 60 deletions(-) diff --git a/src/length.rs b/src/length.rs index 690f151..726fd3a 100644 --- a/src/length.rs +++ b/src/length.rs @@ -26,7 +26,7 @@ use std::ops::{Add, Sub, Mul, Div, Neg}; /// /// 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. -#[deriving(Copy, Decodable, Encodable, Show)] +#[derive(Copy, RustcDecodable, RustcEncodable, Show)] pub struct Length(pub T); impl Length { @@ -87,7 +87,7 @@ impl Length { } } -// FIXME: Switch to `deriving(Clone, PartialEq, PartialOrd, Zero)` after this Rust issue is fixed: +// FIXME: Switch to `derive(Clone, PartialEq, PartialOrd, Zero)` after this Rust issue is fixed: // https://github.com/mozilla/rust/issues/7671 impl Clone for Length { @@ -124,9 +124,9 @@ mod tests { use scale_factor::ScaleFactor; use std::num::Zero; - #[deriving(Show)] + #[derive(Show)] enum Inch {} - #[deriving(Show)] + #[derive(Show)] enum Mm {} #[test] diff --git a/src/lib.rs b/src/lib.rs index ff7688b..f4a76ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ #[macro_use] extern crate log; -extern crate serialize; +extern crate "serialize" as rustc_serialize; extern crate rand; extern crate test; diff --git a/src/matrix.rs b/src/matrix.rs index 2699450..71323c7 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -28,7 +28,7 @@ pub fn Matrix4 + Clone + ApproxEq + Mul + One } } -#[deriving(Show, Copy)] +#[derive(Show, Copy)] pub struct Matrix4 { pub m11: T, pub m12: T, pub m13: T, pub m14: T, pub m21: T, pub m22: T, pub m23: T, pub m24: T, @@ -36,7 +36,7 @@ pub struct Matrix4 { pub m41: T, pub m42: T, pub m43: T, pub m44: T, } -impl + Clone + ApproxEq + Mul + One + Zero> Matrix4 { +impl + Copy + Clone + ApproxEq + Mul + One + Zero> Matrix4 { pub fn approx_eq(&self, other: &Matrix4) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && self.m13.approx_eq(&other.m13) && self.m14.approx_eq(&other.m14) && @@ -101,7 +101,7 @@ impl + Clone + ApproxEq + Mul + One + Zero> M } } -pub fn ortho + Clone + Div + ApproxEq + Mul + Neg + NumCast + One + +pub fn ortho + Copy + Clone + Div + ApproxEq + Mul + Neg + NumCast + One + Sub + Zero> (left: T, right: T, diff --git a/src/matrix2d.rs b/src/matrix2d.rs index c1c9d4b..5f54d9f 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -10,14 +10,14 @@ use num::{One, Zero}; use std::ops::{Add, Mul}; -#[deriving(Clone, Copy)] +#[derive(Clone, Copy)] pub struct Matrix2D { m11: T, m12: T, m21: T, m22: T, m31: T, m32: T } -impl + Clone + Mul + One + Zero> Matrix2D { +impl + Copy + Clone + Mul + One + Zero> Matrix2D { pub fn new(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> Matrix2D { Matrix2D { m11: m11, m12: m12, diff --git a/src/point.rs b/src/point.rs index 18e36aa..62b1213 100644 --- a/src/point.rs +++ b/src/point.rs @@ -15,7 +15,7 @@ use std::fmt; use std::num::NumCast; use std::ops::{Add, Neg, Mul, Sub, Div}; -#[deriving(Clone, Copy, Decodable, Encodable, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] pub struct Point2D { pub x: T, pub y: T @@ -52,7 +52,7 @@ impl> Add> for Point2D { } } -impl> Point2D { +impl> Point2D { pub fn add_size(&self, other: &Size2D) -> Point2D { Point2D { x: self.x + other.width, y: self.y + other.height } } @@ -73,21 +73,19 @@ impl > Neg for Point2D { } } -#[old_impl_check] -impl, T1: Clone> Mul for Point2D { +impl, T1: Clone> Mul for Point2D { type Output = Point2D; #[inline] fn mul(self, scale: Scale) -> Point2D { - Point2D(self.x * *scale, self.y * *scale) + Point2D(self.x * scale, self.y * scale) } } -#[old_impl_check] -impl, T1: Clone> Div for Point2D { +impl, T1: Clone> Div for Point2D { type Output = Point2D; #[inline] fn div(self, scale: Scale) -> Point2D { - Point2D(self.x / *scale, self.y / *scale) + Point2D(self.x / scale, self.y / scale) } } diff --git a/src/rect.rs b/src/rect.rs index d1cd52a..e4ea9bc 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -17,7 +17,7 @@ use std::fmt; use std::num::NumCast; use std::ops::{Add, Sub, Mul, Div}; -#[deriving(Clone, Copy, Decodable, Encodable, PartialEq)] +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq)] pub struct Rect { pub origin: Point2D, pub size: Size2D, @@ -25,7 +25,7 @@ pub struct Rect { impl fmt::Show for Rect { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Rect({} at {})", self.size, self.origin) + write!(f, "Rect({:?} at {:?})", self.size, self.origin) } } @@ -36,7 +36,7 @@ pub fn Rect(origin: Point2D, size: Size2D) -> Rect { } } -impl + Sub> Rect { +impl + Sub> Rect { #[inline] pub fn intersects(&self, other: &Rect) -> bool { self.origin.x < other.origin.x + other.size.width && @@ -124,7 +124,7 @@ impl + Sub> Rect { } #[old_impl_check] -impl> Rect { +impl> Rect { #[inline] pub fn scale(&self, x: Scale, y: Scale) -> Rect { Rect { @@ -156,17 +156,19 @@ pub fn max(x: T, y: T) -> T { if x >= y { x } else { y } } -impl> Mul for Rect { +impl, T1: Clone> Mul for Rect { + type Output = Rect; #[inline] - fn mul(&self, scale: &Scale) -> Rect { - Rect(self.origin * *scale, self.size * *scale) + fn mul(self, scale: Scale) -> Rect { + Rect(self.origin * scale, self.size * scale) } } -impl> Div for Rect { +impl, T1: Clone> Div for Rect { + type Output = Rect; #[inline] - fn div(&self, scale: &Scale) -> Rect { - Rect(self.origin / *scale, self.size / *scale) + fn div(self, scale: Scale) -> Rect { + Rect(self.origin / scale, self.size / scale) } } diff --git a/src/scale_factor.rs b/src/scale_factor.rs index 604327d..73845a2 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -10,7 +10,7 @@ use num::One; use std::num::{NumCast, cast}; -use std::ops::{Add, Mul, Sub, Neg, Div}; +use std::ops::{Add, Mul, Sub, Div}; /// A scaling factor between two different units of measurement. /// @@ -31,7 +31,7 @@ use std::ops::{Add, Mul, Sub, Neg, Div}; /// let one_foot: Length = Length(12.0); /// let one_foot_in_mm: Length = one_foot * mm_per_inch; /// ``` -#[deriving(Copy, Decodable, Encodable, Show)] +#[derive(Copy, RustcDecodable, RustcEncodable, Show)] pub struct ScaleFactor(pub T); impl ScaleFactor { @@ -42,7 +42,7 @@ impl ScaleFactor { } } -impl> ScaleFactor { +impl> ScaleFactor { /// The inverse ScaleFactor (1.0 / self). pub fn inv(&self) -> ScaleFactor { let one: T = One::one(); @@ -51,26 +51,29 @@ impl> ScaleFactor { } // scale0 * scale1 -impl> +impl> Mul> for ScaleFactor { + type Output = ScaleFactor; #[inline] - fn mul(&self, other: &ScaleFactor) -> ScaleFactor { + fn mul(self, other: ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() * other.get()) } } // scale0 + scale1 -impl> Add for ScaleFactor { +impl> Add for ScaleFactor { + type Output = ScaleFactor; #[inline] - fn add(&self, other: &ScaleFactor) -> ScaleFactor { + fn add(self, other: ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() + other.get()) } } // scale0 - scale1 -impl> Sub for ScaleFactor { +impl> Sub for ScaleFactor { + type Output = ScaleFactor; #[inline] - fn sub(&self, other: &ScaleFactor) -> ScaleFactor { + fn sub(self, other: ScaleFactor) -> ScaleFactor { ScaleFactor(self.get() - other.get()) } } @@ -82,7 +85,7 @@ impl ScaleFactor { } } -// FIXME: Switch to `deriving(PartialEq, Clone)` after this Rust issue is fixed: +// FIXME: Switch to `derive(PartialEq, Clone)` after this Rust issue is fixed: // https://github.com/mozilla/rust/issues/7671 impl PartialEq for ScaleFactor { @@ -101,11 +104,11 @@ impl Clone for ScaleFactor { mod tests { use super::ScaleFactor; - #[deriving(Show)] + #[derive(Show)] enum Inch {} - #[deriving(Show)] + #[derive(Show)] enum Cm {} - #[deriving(Show)] + #[derive(Show)] enum Mm {} #[test] diff --git a/src/side_offsets.rs b/src/side_offsets.rs index cde6447..c359356 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -15,7 +15,7 @@ use std::ops::Add; /// A group of side offsets, which correspond to top/left/bottom/right for borders, padding, /// and margins in CSS. -#[deriving(Clone, Copy, PartialEq)] +#[derive(Clone, Copy, PartialEq)] pub struct SideOffsets2D { pub top: T, pub right: T, @@ -40,7 +40,7 @@ impl SideOffsets2D { } } -impl SideOffsets2D where T: Add { +impl SideOffsets2D where T: Add + Copy { pub fn horizontal(&self) -> T { self.left + self.right } @@ -50,8 +50,9 @@ impl SideOffsets2D where T: Add { } } -impl Add for SideOffsets2D { - fn add(&self, other: &SideOffsets2D) -> SideOffsets2D { +impl> Add for SideOffsets2D { + type Output = SideOffsets2D; + fn add(self, other: SideOffsets2D) -> SideOffsets2D { SideOffsets2D { top: self.top + other.top, right: self.right + other.right, @@ -73,7 +74,7 @@ impl SideOffsets2D { } /// A SIMD enabled version of SideOffsets2D specialized for i32. -#[deriving(Clone, Copy, PartialEq, Rand)] +#[derive(Clone, Copy, PartialEq, Rand)] #[simd] pub struct SideOffsets2DSimdI32 { pub top: i32, @@ -113,12 +114,13 @@ impl SideOffsets2DSimdI32 { } } -impl Add for SideOffsets2DSimdI32 { +/*impl Add for SideOffsets2DSimdI32 { + type Output = SideOffsets2DSimdI32; #[inline] - fn add(&self, other: &SideOffsets2DSimdI32) -> SideOffsets2DSimdI32 { - *self + *other // Use SIMD addition + fn add(self, other: SideOffsets2DSimdI32) -> SideOffsets2DSimdI32 { + self + other // Use SIMD addition } -} +}*/ impl SideOffsets2DSimdI32 { #[inline] diff --git a/src/size.rs b/src/size.rs index c6d07bb..9377cfc 100644 --- a/src/size.rs +++ b/src/size.rs @@ -14,7 +14,7 @@ use std::fmt; use std::num::NumCast; use std::ops::{Mul, Div}; -#[deriving(Clone, Copy, Decodable, Encodable, PartialEq)] +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq)] pub struct Size2D { pub width: T, pub height: T @@ -22,7 +22,7 @@ pub struct Size2D { impl fmt::Show for Size2D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}×{}", self.width, self.height) + write!(f, "{:?}×{:?}", self.width, self.height) } } @@ -33,8 +33,8 @@ pub fn Size2D(width: T, height: T) -> Size2D { } } -impl> Size2D { - pub fn area(&self) -> U { self.width * self.height } +impl, U> Size2D { + pub fn area(&self) -> U { self.width * self.height } } impl Size2D { @@ -46,17 +46,19 @@ impl Size2D { } } -impl> Mul for Size2D { +impl, T1: Clone> Mul for Size2D { + type Output = Size2D; #[inline] - fn mul(&self, scale: &Scale) -> Size2D { - Size2D(self.width * *scale, self.height * *scale) + fn mul(self, scale: Scale) -> Size2D { + Size2D(self.width * scale, self.height * scale) } } -impl> Div for Size2D { +impl, T1: Clone> Div for Size2D { + type Output = Size2D; #[inline] - fn div(&self, scale: &Scale) -> Size2D { - Size2D(self.width / *scale, self.height / *scale) + fn div(self, scale: Scale) -> Size2D { + Size2D(self.width / scale, self.height / scale) } }