diff --git a/src/length.rs b/src/length.rs index 4068108..726fd3a 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`. /// @@ -24,7 +26,7 @@ use std::num::{NumCast, cast}; /// /// 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 { @@ -36,51 +38,56 @@ 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)) } } -// 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 { @@ -117,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 f873bf8..f4a76ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,11 @@ #![crate_name = "geom"] #![crate_type = "rlib"] -#![feature(asm, phase, simd)] +#![feature(asm, simd, old_impl_check)] -#[phase(plugin, link)] +#[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 6ccc563..71323c7 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -12,8 +12,9 @@ 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>( +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, @@ -27,7 +28,7 @@ pub fn Matrix4 + Clone + ApproxEq + Mul + One + Zero>( } } -#[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, @@ -35,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) && @@ -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(), @@ -100,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 + Copy + Clone + Div + ApproxEq + Mul + Neg + NumCast + One + + Sub + Zero> (left: T, right: T, bottom: T, @@ -109,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(); @@ -123,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 a1aca3c..5f54d9f 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -8,15 +8,16 @@ // except according to those terms. 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, @@ -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..62b1213 100644 --- a/src/point.rs +++ b/src/point.rs @@ -13,8 +13,9 @@ 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)] +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] pub struct Point2D { pub x: T, pub y: T @@ -28,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) } } @@ -37,48 +38,54 @@ 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 { +impl, T1: Clone> Mul for Point2D { + type Output = Point2D; #[inline] - fn mul(&self, scale: &Scale) -> Point2D { - Point2D(self.x * *scale, self.y * *scale) + fn mul(self, scale: Scale) -> Point2D { + Point2D(self.x * scale, self.y * scale) } } -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) + fn div(self, scale: Scale) -> Point2D { + Point2D(self.x / scale, self.y / scale) } } @@ -102,9 +109,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 @@ -118,7 +125,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 3990dbd..e4ea9bc 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -15,8 +15,9 @@ 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)] +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq)] pub struct Rect { pub origin: Point2D, pub size: Size2D, @@ -24,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) } } @@ -35,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 && @@ -122,7 +123,8 @@ impl + Sub> Rect { } } -impl> Rect { +#[old_impl_check] +impl> Rect { #[inline] pub fn scale(&self, x: Scale, y: Scale) -> Rect { Rect { @@ -154,17 +156,19 @@ pub fn max(x: T, y: T) -> T { if x >= y { x } else { y } } -impl, T1: Clone> 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, T1: Clone> 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) } } @@ -183,9 +187,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 @@ -199,7 +203,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 f3b0b0c..73845a2 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, Div}; /// A scaling factor between two different units of measurement. /// @@ -30,7 +31,7 @@ use std::num::{NumCast, cast}; /// 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 { @@ -41,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(); @@ -50,40 +51,41 @@ impl> ScaleFactor { } // scale0 * scale1 -impl> -Mul, ScaleFactor> for ScaleFactor { +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, ScaleFactor> 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, ScaleFactor> 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()) } } -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)) } } -// 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 { @@ -102,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 4914f22..c359356 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -11,11 +11,11 @@ //! 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. -#[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, SideOffsets2D> 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 23540a5..9377cfc 100644 --- a/src/size.rs +++ b/src/size.rs @@ -12,8 +12,9 @@ use num::Zero; 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 @@ -21,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) } } @@ -32,7 +33,7 @@ pub fn Size2D(width: T, height: T) -> Size2D { } } -impl, U> Size2D { +impl, U> Size2D { pub fn area(&self) -> U { self.width * self.height } } @@ -45,17 +46,19 @@ impl Size2D { } } -impl, T1: Clone> 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, T1: Clone> 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) } } @@ -79,9 +82,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 @@ -95,7 +98,7 @@ impl Size2D> { self.cast().unwrap() } - pub fn as_uint(&self) -> Size2D> { + pub fn as_uint(&self) -> Size2D> { self.cast().unwrap() } }