From 137660d9c4350ab8a07355f41e9008af4f88f4e1 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Sat, 27 May 2017 13:00:03 +0200 Subject: [PATCH] Use verbs instead of adjectives for transformations with #[must_use]. --- src/point.rs | 6 ++++++ src/rect.rs | 7 +++++++ src/transform2d.rs | 42 +++++++++++++++++++++++++++--------------- src/transform3d.rs | 37 ++++++++++++++++++++++--------------- src/vector.rs | 6 ++++++ 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/point.rs b/src/point.rs index 919644c..28f47f7 100644 --- a/src/point.rs +++ b/src/point.rs @@ -227,6 +227,7 @@ impl TypedPoint2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.round() == { 0.0, -1.0 }`. #[inline] + #[must_use] pub fn round(&self) -> Self { point2(self.x.round(), self.y.round()) } @@ -238,6 +239,7 @@ impl TypedPoint2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.ceil() == { 0.0, 0.0 }`. #[inline] + #[must_use] pub fn ceil(&self) -> Self { point2(self.x.ceil(), self.y.ceil()) } @@ -249,6 +251,7 @@ impl TypedPoint2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.floor() == { -1.0, -1.0 }`. #[inline] + #[must_use] pub fn floor(&self) -> Self { point2(self.x.floor(), self.y.floor()) } @@ -491,6 +494,7 @@ impl TypedPoint3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn round(&self) -> Self { point3(self.x.round(), self.y.round(), self.z.round()) } @@ -501,6 +505,7 @@ impl TypedPoint3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn ceil(&self) -> Self { point3(self.x.ceil(), self.y.ceil(), self.z.ceil()) } @@ -511,6 +516,7 @@ impl TypedPoint3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn floor(&self) -> Self { point3(self.x.floor(), self.y.floor(), self.z.floor()) } diff --git a/src/rect.rs b/src/rect.rs index 9b51221..defd4e7 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -157,6 +157,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub) -> TypedRect { Self::new(self.origin + *by, self.size) } @@ -181,6 +182,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub TypedRect { TypedRect::new( TypedPoint2D::new(self.origin.x - width, self.origin.y - height), @@ -189,6 +191,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub, height: Length) -> TypedRect { self.inflate(width.get(), height.get()) } @@ -209,6 +212,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub) -> TypedRect { self.translate(&size.to_vector()) } @@ -366,6 +370,7 @@ impl + Sub, U> TypedRect /// avoid pixel rounding errors. /// Note that this is *not* rounding to nearest integer if the values are negative. /// They are always rounding as floor(n + 0.5). + #[must_use] pub fn round(&self) -> Self { let origin = self.origin.round(); let size = self.origin.add_size(&self.size).round() - origin; @@ -374,6 +379,7 @@ impl + Sub, U> TypedRect /// Return a rectangle with edges rounded to integer coordinates, such that /// the original rectangle contains the resulting rectangle. + #[must_use] pub fn round_in(&self) -> Self { let origin = self.origin.ceil(); let size = self.origin.add_size(&self.size).floor() - origin; @@ -382,6 +388,7 @@ impl + Sub, U> TypedRect /// Return a rectangle with edges rounded to integer coordinates, such that /// the original rectangle is contained in the resulting rectangle. + #[must_use] pub fn round_out(&self) -> Self { let origin = self.origin.floor(); let size = self.origin.add_size(&self.size).ceil() - origin; diff --git a/src/transform2d.rs b/src/transform2d.rs index d581219..9852fa8 100644 --- a/src/transform2d.rs +++ b/src/transform2d.rs @@ -131,6 +131,7 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies after self's transformation. + #[must_use] pub fn post_mul(&self, mat: &TypedTransform2D) -> TypedTransform2D { TypedTransform2D::row_major( self.m11 * mat.m11 + self.m12 * mat.m21, @@ -144,6 +145,7 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies before self's transformation. + #[must_use] pub fn pre_mul(&self, mat: &TypedTransform2D) -> TypedTransform2D { mat.post_mul(self) } @@ -159,12 +161,14 @@ where T: Copy + Clone + } /// Applies a translation after self's transformation and returns the resulting transform. - pub fn post_translated(&self, v: TypedVector2D) -> TypedTransform2D { + #[must_use] + pub fn post_translate(&self, v: TypedVector2D) -> TypedTransform2D { self.post_mul(&TypedTransform2D::create_translation(v.x, v.y)) } /// Applies a translation before self's transformation and returns the resulting transform. - pub fn pre_translated(&self, v: TypedVector2D) -> TypedTransform2D { + #[must_use] + pub fn pre_translate(&self, v: TypedVector2D) -> TypedTransform2D { self.pre_mul(&TypedTransform2D::create_translation(v.x, v.y)) } @@ -179,12 +183,14 @@ where T: Copy + Clone + } /// Applies a scale after self's transformation and returns the resulting transform. - pub fn post_scaled(&self, x: T, y: T) -> TypedTransform2D { + #[must_use] + pub fn post_scale(&self, x: T, y: T) -> TypedTransform2D { self.post_mul(&TypedTransform2D::create_scale(x, y)) } /// Applies a scale before self's transformation and returns the resulting transform. - pub fn pre_scaled(&self, x: T, y: T) -> TypedTransform2D { + #[must_use] + pub fn pre_scale(&self, x: T, y: T) -> TypedTransform2D { TypedTransform2D::row_major( self.m11 * x, self.m12, self.m21, self.m22 * y, @@ -205,17 +211,20 @@ where T: Copy + Clone + } /// Applies a rotation after self's transformation and returns the resulting transform. - pub fn post_rotated(&self, theta: Radians) -> TypedTransform2D { + #[must_use] + pub fn post_rotate(&self, theta: Radians) -> TypedTransform2D { self.post_mul(&TypedTransform2D::create_rotation(theta)) } /// Applies a rotation after self's transformation and returns the resulting transform. - pub fn pre_rotated(&self, theta: Radians) -> TypedTransform2D { + #[must_use] + pub fn pre_rotate(&self, theta: Radians) -> TypedTransform2D { self.pre_mul(&TypedTransform2D::create_rotation(theta)) } /// Returns the given point transformed by this transform. #[inline] + #[must_use] pub fn transform_point(&self, point: &TypedPoint2D) -> TypedPoint2D { TypedPoint2D::new(point.x * self.m11 + point.y * self.m21 + self.m31, point.x * self.m12 + point.y * self.m22 + self.m32) @@ -223,6 +232,7 @@ where T: Copy + Clone + /// Returns the given vector transformed by this matrix. #[inline] + #[must_use] pub fn transform_vector(&self, vec: &TypedVector2D) -> TypedVector2D { vec2(vec.x * self.m11 + vec.y * self.m21, vec.x * self.m12 + vec.y * self.m22) @@ -231,6 +241,7 @@ where T: Copy + Clone + /// Returns a rectangle that encompasses the result of transforming the given rectangle by this /// transform. #[inline] + #[must_use] pub fn transform_rect(&self, rect: &TypedRect) -> TypedRect { TypedRect::from_points(&[ self.transform_point(&rect.origin), @@ -246,6 +257,7 @@ where T: Copy + Clone + } /// Returns the inverse transform if possible. + #[must_use] pub fn inverse(&self) -> Option> { let det = self.determinant(); @@ -325,8 +337,8 @@ mod test { #[test] pub fn test_translation() { let t1 = Mat::create_translation(1.0, 2.0); - let t2 = Mat::identity().pre_translated(vec2(1.0, 2.0)); - let t3 = Mat::identity().post_translated(vec2(1.0, 2.0)); + let t2 = Mat::identity().pre_translate(vec2(1.0, 2.0)); + let t3 = Mat::identity().post_translate(vec2(1.0, 2.0)); assert_eq!(t1, t2); assert_eq!(t1, t3); @@ -338,8 +350,8 @@ mod test { #[test] pub fn test_rotation() { let r1 = Mat::create_rotation(rad(FRAC_PI_2)); - let r2 = Mat::identity().pre_rotated(rad(FRAC_PI_2)); - let r3 = Mat::identity().post_rotated(rad(FRAC_PI_2)); + let r2 = Mat::identity().pre_rotate(rad(FRAC_PI_2)); + let r3 = Mat::identity().post_rotate(rad(FRAC_PI_2)); assert_eq!(r1, r2); assert_eq!(r1, r3); @@ -351,8 +363,8 @@ mod test { #[test] pub fn test_scale() { let s1 = Mat::create_scale(2.0, 3.0); - let s2 = Mat::identity().pre_scaled(2.0, 3.0); - let s3 = Mat::identity().post_scaled(2.0, 3.0); + let s2 = Mat::identity().pre_scale(2.0, 3.0); + let s3 = Mat::identity().post_scale(2.0, 3.0); assert_eq!(s1, s2); assert_eq!(s1, s3); @@ -403,8 +415,8 @@ mod test { #[test] pub fn test_pre_post() { - let m1 = Transform2D::identity().post_scaled(1.0, 2.0).post_translated(vec2(1.0, 2.0)); - let m2 = Transform2D::identity().pre_translated(vec2(1.0, 2.0)).pre_scaled(1.0, 2.0); + let m1 = Transform2D::identity().post_scale(1.0, 2.0).post_translate(vec2(1.0, 2.0)); + let m2 = Transform2D::identity().pre_translate(vec2(1.0, 2.0)).pre_scale(1.0, 2.0); assert!(m1.approx_eq(&m2)); let r = Mat::create_rotation(rad(FRAC_PI_2)); @@ -432,7 +444,7 @@ mod test { pub fn test_is_identity() { let m1 = Transform2D::identity(); assert!(m1.is_identity()); - let m2 = m1.post_translated(vec2(0.1, 0.0)); + let m2 = m1.post_translate(vec2(0.1, 0.0)); assert!(!m2.is_identity()); } diff --git a/src/transform3d.rs b/src/transform3d.rs index 056478c..d4613b9 100644 --- a/src/transform3d.rs +++ b/src/transform3d.rs @@ -370,6 +370,7 @@ where T: Copy + Clone + } /// Multiplies all of the transform's component by a scalar and returns the result. + #[must_use] pub fn mul_s(&self, x: T) -> TypedTransform3D { TypedTransform3D::row_major( self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x, @@ -456,12 +457,14 @@ where T: Copy + Clone + } /// Returns a transform with a translation applied before self's transformation. - pub fn pre_translated(&self, v: TypedVector3D) -> TypedTransform3D { + #[must_use] + pub fn pre_translate(&self, v: TypedVector3D) -> TypedTransform3D { self.pre_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z)) } /// Returns a transform with a translation applied after self's transformation. - pub fn post_translated(&self, v: TypedVector3D) -> TypedTransform3D { + #[must_use] + pub fn post_translate(&self, v: TypedVector3D) -> TypedTransform3D { self.post_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z)) } @@ -477,7 +480,8 @@ where T: Copy + Clone + } /// Returns a transform with a scale applied before self's transformation. - pub fn pre_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D { + #[must_use] + pub fn pre_scale(&self, x: T, y: T, z: T) -> TypedTransform3D { TypedTransform3D::row_major( self.m11 * x, self.m12, self.m13, self.m14, self.m21 , self.m22 * y, self.m23, self.m24, @@ -487,7 +491,8 @@ where T: Copy + Clone + } /// Returns a transform with a scale applied after self's transformation. - pub fn post_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D { + #[must_use] + pub fn post_scale(&self, x: T, y: T, z: T) -> TypedTransform3D { self.post_mul(&TypedTransform3D::create_scale(x, y, z)) } @@ -529,12 +534,14 @@ where T: Copy + Clone + } /// Returns a transform with a rotation applied after self's transformation. - pub fn post_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { + #[must_use] + pub fn post_rotate(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { self.post_mul(&TypedTransform3D::create_rotation(x, y, z, theta)) } /// Returns a transform with a rotation applied before self's transformation. - pub fn pre_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { + #[must_use] + pub fn pre_rotate(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { self.pre_mul(&TypedTransform3D::create_rotation(x, y, z, theta)) } @@ -644,8 +651,8 @@ mod tests { #[test] pub fn test_translation() { let t1 = Mf32::create_translation(1.0, 2.0, 3.0); - let t2 = Mf32::identity().pre_translated(vec3(1.0, 2.0, 3.0)); - let t3 = Mf32::identity().post_translated(vec3(1.0, 2.0, 3.0)); + let t2 = Mf32::identity().pre_translate(vec3(1.0, 2.0, 3.0)); + let t3 = Mf32::identity().post_translate(vec3(1.0, 2.0, 3.0)); assert_eq!(t1, t2); assert_eq!(t1, t3); @@ -661,8 +668,8 @@ mod tests { #[test] pub fn test_rotation() { let r1 = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2)); - let r2 = Mf32::identity().pre_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2)); - let r3 = Mf32::identity().post_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2)); + let r2 = Mf32::identity().pre_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2)); + let r3 = Mf32::identity().post_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2)); assert_eq!(r1, r2); assert_eq!(r1, r3); @@ -678,8 +685,8 @@ mod tests { #[test] pub fn test_scale() { let s1 = Mf32::create_scale(2.0, 3.0, 4.0); - let s2 = Mf32::identity().pre_scaled(2.0, 3.0, 4.0); - let s3 = Mf32::identity().post_scaled(2.0, 3.0, 4.0); + let s2 = Mf32::identity().pre_scale(2.0, 3.0, 4.0); + let s3 = Mf32::identity().post_scale(2.0, 3.0, 4.0); assert_eq!(s1, s2); assert_eq!(s1, s3); @@ -794,8 +801,8 @@ mod tests { #[test] pub fn test_pre_post() { - let m1 = Transform3D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(vec3(1.0, 2.0, 3.0)); - let m2 = Transform3D::identity().pre_translated(vec3(1.0, 2.0, 3.0)).pre_scaled(1.0, 2.0, 3.0); + let m1 = Transform3D::identity().post_scale(1.0, 2.0, 3.0).post_translate(vec3(1.0, 2.0, 3.0)); + let m2 = Transform3D::identity().pre_translate(vec3(1.0, 2.0, 3.0)).pre_scale(1.0, 2.0, 3.0); assert!(m1.approx_eq(&m2)); let r = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2)); @@ -840,7 +847,7 @@ mod tests { pub fn test_is_identity() { let m1 = Transform3D::identity(); assert!(m1.is_identity()); - let m2 = m1.post_translated(vec3(0.1, 0.0, 0.0)); + let m2 = m1.post_translate(vec3(0.1, 0.0, 0.0)); assert!(!m2.is_identity()); } diff --git a/src/vector.rs b/src/vector.rs index 7f33a2a..fa7ef7b 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -248,6 +248,7 @@ impl TypedVector2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.round() == { 0.0, -1.0 }`. #[inline] + #[must_use] pub fn round(&self) -> Self { vec2(self.x.round(), self.y.round()) } @@ -259,6 +260,7 @@ impl TypedVector2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.ceil() == { 0.0, 0.0 }`. #[inline] + #[must_use] pub fn ceil(&self) -> Self { vec2(self.x.ceil(), self.y.ceil()) } @@ -270,6 +272,7 @@ impl TypedVector2D { /// This behavior is preserved for negative values (unlike the basic cast). /// For example `{ -0.1, -0.8 }.floor() == { -1.0, -1.0 }`. #[inline] + #[must_use] pub fn floor(&self) -> Self { vec2(self.x.floor(), self.y.floor()) } @@ -568,6 +571,7 @@ impl TypedVector3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn round(&self) -> Self { vec3(self.x.round(), self.y.round(), self.z.round()) } @@ -578,6 +582,7 @@ impl TypedVector3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn ceil(&self) -> Self { vec3(self.x.ceil(), self.y.ceil(), self.z.ceil()) } @@ -588,6 +593,7 @@ impl TypedVector3D { /// /// This behavior is preserved for negative values (unlike the basic cast). #[inline] + #[must_use] pub fn floor(&self) -> Self { vec3(self.x.floor(), self.y.floor(), self.z.floor()) }