diff --git a/src/length.rs b/src/length.rs index 19927e1..bbda7fa 100644 --- a/src/length.rs +++ b/src/length.rs @@ -9,8 +9,9 @@ //! A one-dimensional length, tagged with its units. use scale_factor::ScaleFactor; +use num::Zero; -use std::num::{NumCast, cast, Zero}; +use std::num::{NumCast, cast}; /// A one-dimensional distance, with value represented by `T` and unit of measurement `Unit`. /// @@ -104,14 +105,10 @@ impl Ord for Length { fn cmp(&self, other: &Length) -> Ordering { self.get().cmp(&other.get()) } } -impl Zero for Length { +impl Zero for Length { fn zero() -> Length { Length(Zero::zero()) } - - fn is_zero(&self) -> bool { - self.get().is_zero() - } } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index 891e90f..f873bf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,9 +31,9 @@ pub mod approxeq; pub mod length; pub mod matrix; pub mod matrix2d; +pub mod num; pub mod point; pub mod rect; pub mod scale_factor; pub mod side_offsets; pub mod size; - diff --git a/src/matrix.rs b/src/matrix.rs index f82a396..9e92b15 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -8,8 +8,10 @@ // except according to those terms. use approxeq::ApproxEq; +use num::{One, Zero}; + use std::num; -use std::num::{NumCast, One, Zero}; +use std::num::NumCast; pub fn Matrix4 + Clone + ApproxEq + Mul + One + Zero>( m11: T, m12: T, m13: T, m14: T, diff --git a/src/matrix2d.rs b/src/matrix2d.rs index 7ed91d3..7013a9d 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::num::{One, Zero}; +use num::{One, Zero}; pub struct Matrix2D { m11: T, m12: T, @@ -31,7 +31,7 @@ impl + Clone + Mul + One + Zero> Matrix2D { m.m21*self.m12 + m.m22*self.m22, m.m31*self.m11 + m.m32*self.m21 + self.m31, m.m31*self.m12 + m.m32*self.m22 + self.m32) - } + } pub fn translate(&self, x: T, y: T) -> Matrix2D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); @@ -53,14 +53,13 @@ impl + Clone + Mul + One + Zero> Matrix2D { _0.clone(), _1.clone(), _0.clone(), _0.clone()); } - + pub fn to_array(&self) -> [T, ..6] { [ self.m11.clone(), self.m12.clone(), self.m21.clone(), self.m22.clone(), self.m31.clone(), self.m32.clone() ] - } - + } } diff --git a/src/num.rs b/src/num.rs new file mode 100644 index 0000000..b81dcd9 --- /dev/null +++ b/src/num.rs @@ -0,0 +1,50 @@ +// Copyright 2014 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +//! A one-dimensional length, tagged with its units. + +use std::num::Int; + + +pub trait Zero { + fn zero() -> Self; +} + +impl Zero for T { + #[inline] + fn zero() -> T { Int::zero() } +} + +impl Zero for f32 { + #[inline] + fn zero() -> f32 { 0. } +} + +impl Zero for f64 { + #[inline] + fn zero() -> f64 { 0. } +} + +pub trait One { + fn one() -> Self; +} + +impl One for T { + #[inline] + fn one() -> T { Int::one() } +} + +impl One for f32 { + #[inline] + fn one() -> f32 { 1. } +} + +impl One for f64 { + #[inline] + fn one() -> f64 { 1. } +} diff --git a/src/point.rs b/src/point.rs index 6960e60..434b8c1 100644 --- a/src/point.rs +++ b/src/point.rs @@ -9,9 +9,10 @@ use length::Length; use size::Size2D; +use num::Zero; use std::fmt; -use std::num::{NumCast, Zero}; +use std::num::NumCast; #[deriving(Clone, Decodable, Encodable, Eq, Hash, PartialEq)] pub struct Point2D { @@ -19,14 +20,10 @@ pub struct Point2D { pub y: T } -impl Zero for Point2D { - fn zero() -> Point2D { +impl Point2D { + pub fn zero() -> Point2D { Point2D { x: Zero::zero(), y: Zero::zero() } } - - fn is_zero(&self) -> bool { - self.x.is_zero() && self.y.is_zero() - } } impl fmt::Show for Point2D { @@ -35,7 +32,7 @@ impl fmt::Show for Point2D { } } -pub fn Point2D(x: T, y: T) -> Point2D { +pub fn Point2D(x: T, y: T) -> Point2D { Point2D {x: x, y: y} } diff --git a/src/rect.rs b/src/rect.rs index 348f86e..776f0e0 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -8,12 +8,13 @@ // except according to those terms. use length::Length; +use num::Zero; use point::Point2D; use size::Size2D; -use std::cmp::{PartialEq, PartialOrd}; +use std::cmp::PartialOrd; use std::fmt; -use std::num::{NumCast, Zero}; +use std::num::NumCast; #[deriving(Clone, Decodable, Encodable, PartialEq)] pub struct Rect { @@ -126,16 +127,16 @@ impl> Rect { } } -impl Rect { +impl Rect { pub fn zero() -> Rect { Rect { - origin: Zero::zero(), + origin: Point2D::zero(), size: Size2D::zero(), } } pub fn is_empty(&self) -> bool { - self.size.is_empty() + self.size == Size2D::zero() } } diff --git a/src/scale_factor.rs b/src/scale_factor.rs index a010ccf..8b9a3f9 100644 --- a/src/scale_factor.rs +++ b/src/scale_factor.rs @@ -8,7 +8,8 @@ // except according to those terms. //! A type-checked scaling factor between units. -use std::num::{NumCast, cast, One}; +use num::One; +use std::num::{NumCast, cast}; /// A scaling factor between two different units of measurement. /// diff --git a/src/side_offsets.rs b/src/side_offsets.rs index bf84dff..286fe3d 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -10,7 +10,8 @@ //! A group of side offsets, which correspond to top/left/bottom/right for borders, padding, //! and margins in CSS. -use std::num::{Num, NumCast, Zero}; +use num::Zero; +use std::num::Num; /// A group of side offsets, which correspond to top/left/bottom/right for borders, padding, /// and margins in CSS. @@ -60,8 +61,8 @@ impl Add, SideOffsets2D> for SideOffsets2D { } } -impl Zero for SideOffsets2D { - fn zero() -> SideOffsets2D { +impl SideOffsets2D { + pub fn zero() -> SideOffsets2D { SideOffsets2D { top: Zero::zero(), right: Zero::zero(), @@ -69,10 +70,6 @@ impl Zero for SideOffsets2D { left: Zero::zero(), } } - - fn is_zero(&self) -> bool { - self.top.is_zero() && self.right.is_zero() && self.bottom.is_zero() && self.left.is_zero() - } } /// A SIMD enabled version of SideOffsets2D specialized for i32. @@ -123,26 +120,26 @@ impl Add for SideOffsets2DSimdI32 { } } -impl Zero for SideOffsets2DSimdI32 { +impl SideOffsets2DSimdI32 { #[inline] - fn zero() -> SideOffsets2DSimdI32 { + pub fn zero() -> SideOffsets2DSimdI32 { SideOffsets2DSimdI32 { - top: Zero::zero(), - bottom: Zero::zero(), - right: Zero::zero(), - left: Zero::zero(), + top: 0, + bottom: 0, + right: 0, + left: 0, } } #[cfg(not(target_arch = "x86_64"))] #[inline] - fn is_zero(&self) -> bool { - self.top.is_zero() && self.right.is_zero() && self.bottom.is_zero() && self.left.is_zero() + pub fn is_zero(&self) -> bool { + self.top == 0 && self.right == 0 && self.bottom == 0 && self.left == 0 } #[cfg(target_arch = "x86_64")] #[inline] - fn is_zero(&self) -> bool { + pub fn is_zero(&self) -> bool { let is_zero: bool; unsafe { asm! { diff --git a/src/size.rs b/src/size.rs index 0025539..02f8809 100644 --- a/src/size.rs +++ b/src/size.rs @@ -8,10 +8,10 @@ // except according to those terms. use length::Length; +use num::Zero; -use std::cmp::PartialEq; use std::fmt; -use std::num::{NumCast, Zero}; +use std::num::NumCast; #[deriving(Clone, Decodable, Encodable, PartialEq)] pub struct Size2D { @@ -36,17 +36,13 @@ impl, U> Size2D { pub fn area(&self) -> U { self.width * self.height } } -impl Size2D { +impl Size2D { pub fn zero() -> Size2D { Size2D { width: Zero::zero(), height: Zero::zero(), } } - - pub fn is_empty(&self) -> bool { - self.width.is_zero() || self.height.is_zero() - } } impl, T1: Clone> Mul> for Size2D {