Skip to content

Commit 137660d

Browse files
committed
Use verbs instead of adjectives for transformations with #[must_use].
1 parent f33d6e2 commit 137660d

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

src/point.rs

+6
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ impl<T: Round, U> TypedPoint2D<T, U> {
227227
/// This behavior is preserved for negative values (unlike the basic cast).
228228
/// For example `{ -0.1, -0.8 }.round() == { 0.0, -1.0 }`.
229229
#[inline]
230+
#[must_use]
230231
pub fn round(&self) -> Self {
231232
point2(self.x.round(), self.y.round())
232233
}
@@ -238,6 +239,7 @@ impl<T: Ceil, U> TypedPoint2D<T, U> {
238239
/// This behavior is preserved for negative values (unlike the basic cast).
239240
/// For example `{ -0.1, -0.8 }.ceil() == { 0.0, 0.0 }`.
240241
#[inline]
242+
#[must_use]
241243
pub fn ceil(&self) -> Self {
242244
point2(self.x.ceil(), self.y.ceil())
243245
}
@@ -249,6 +251,7 @@ impl<T: Floor, U> TypedPoint2D<T, U> {
249251
/// This behavior is preserved for negative values (unlike the basic cast).
250252
/// For example `{ -0.1, -0.8 }.floor() == { -1.0, -1.0 }`.
251253
#[inline]
254+
#[must_use]
252255
pub fn floor(&self) -> Self {
253256
point2(self.x.floor(), self.y.floor())
254257
}
@@ -491,6 +494,7 @@ impl<T: Round, U> TypedPoint3D<T, U> {
491494
///
492495
/// This behavior is preserved for negative values (unlike the basic cast).
493496
#[inline]
497+
#[must_use]
494498
pub fn round(&self) -> Self {
495499
point3(self.x.round(), self.y.round(), self.z.round())
496500
}
@@ -501,6 +505,7 @@ impl<T: Ceil, U> TypedPoint3D<T, U> {
501505
///
502506
/// This behavior is preserved for negative values (unlike the basic cast).
503507
#[inline]
508+
#[must_use]
504509
pub fn ceil(&self) -> Self {
505510
point3(self.x.ceil(), self.y.ceil(), self.z.ceil())
506511
}
@@ -511,6 +516,7 @@ impl<T: Floor, U> TypedPoint3D<T, U> {
511516
///
512517
/// This behavior is preserved for negative values (unlike the basic cast).
513518
#[inline]
519+
#[must_use]
514520
pub fn floor(&self) -> Self {
515521
point3(self.x.floor(), self.y.floor(), self.z.floor())
516522
}

src/rect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
157157

158158
/// Returns the same rectangle, translated by a vector.
159159
#[inline]
160+
#[must_use]
160161
pub fn translate(&self, by: &TypedVector2D<T, U>) -> TypedRect<T, U> {
161162
Self::new(self.origin + *by, self.size)
162163
}
@@ -181,6 +182,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
181182
}
182183

183184
#[inline]
185+
#[must_use]
184186
pub fn inflate(&self, width: T, height: T) -> TypedRect<T, U> {
185187
TypedRect::new(
186188
TypedPoint2D::new(self.origin.x - width, self.origin.y - height),
@@ -189,6 +191,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
189191
}
190192

191193
#[inline]
194+
#[must_use]
192195
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> TypedRect<T, U> {
193196
self.inflate(width.get(), height.get())
194197
}
@@ -209,6 +212,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
209212
}
210213

211214
#[inline]
215+
#[must_use]
212216
pub fn translate_by_size(&self, size: &TypedSize2D<T, U>) -> TypedRect<T, U> {
213217
self.translate(&size.to_vector())
214218
}
@@ -366,6 +370,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect
366370
/// avoid pixel rounding errors.
367371
/// Note that this is *not* rounding to nearest integer if the values are negative.
368372
/// They are always rounding as floor(n + 0.5).
373+
#[must_use]
369374
pub fn round(&self) -> Self {
370375
let origin = self.origin.round();
371376
let size = self.origin.add_size(&self.size).round() - origin;
@@ -374,6 +379,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect
374379

375380
/// Return a rectangle with edges rounded to integer coordinates, such that
376381
/// the original rectangle contains the resulting rectangle.
382+
#[must_use]
377383
pub fn round_in(&self) -> Self {
378384
let origin = self.origin.ceil();
379385
let size = self.origin.add_size(&self.size).floor() - origin;
@@ -382,6 +388,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect
382388

383389
/// Return a rectangle with edges rounded to integer coordinates, such that
384390
/// the original rectangle is contained in the resulting rectangle.
391+
#[must_use]
385392
pub fn round_out(&self) -> Self {
386393
let origin = self.origin.floor();
387394
let size = self.origin.add_size(&self.size).ceil() - origin;

src/transform2d.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ where T: Copy + Clone +
131131

132132
/// Returns the multiplication of the two matrices such that mat's transformation
133133
/// applies after self's transformation.
134+
#[must_use]
134135
pub fn post_mul<NewDst>(&self, mat: &TypedTransform2D<T, Dst, NewDst>) -> TypedTransform2D<T, Src, NewDst> {
135136
TypedTransform2D::row_major(
136137
self.m11 * mat.m11 + self.m12 * mat.m21,
@@ -144,6 +145,7 @@ where T: Copy + Clone +
144145

145146
/// Returns the multiplication of the two matrices such that mat's transformation
146147
/// applies before self's transformation.
148+
#[must_use]
147149
pub fn pre_mul<NewSrc>(&self, mat: &TypedTransform2D<T, NewSrc, Src>) -> TypedTransform2D<T, NewSrc, Dst> {
148150
mat.post_mul(self)
149151
}
@@ -159,12 +161,14 @@ where T: Copy + Clone +
159161
}
160162

161163
/// Applies a translation after self's transformation and returns the resulting transform.
162-
pub fn post_translated(&self, v: TypedVector2D<T, Dst>) -> TypedTransform2D<T, Src, Dst> {
164+
#[must_use]
165+
pub fn post_translate(&self, v: TypedVector2D<T, Dst>) -> TypedTransform2D<T, Src, Dst> {
163166
self.post_mul(&TypedTransform2D::create_translation(v.x, v.y))
164167
}
165168

166169
/// Applies a translation before self's transformation and returns the resulting transform.
167-
pub fn pre_translated(&self, v: TypedVector2D<T, Src>) -> TypedTransform2D<T, Src, Dst> {
170+
#[must_use]
171+
pub fn pre_translate(&self, v: TypedVector2D<T, Src>) -> TypedTransform2D<T, Src, Dst> {
168172
self.pre_mul(&TypedTransform2D::create_translation(v.x, v.y))
169173
}
170174

@@ -179,12 +183,14 @@ where T: Copy + Clone +
179183
}
180184

181185
/// Applies a scale after self's transformation and returns the resulting transform.
182-
pub fn post_scaled(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
186+
#[must_use]
187+
pub fn post_scale(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
183188
self.post_mul(&TypedTransform2D::create_scale(x, y))
184189
}
185190

186191
/// Applies a scale before self's transformation and returns the resulting transform.
187-
pub fn pre_scaled(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
192+
#[must_use]
193+
pub fn pre_scale(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
188194
TypedTransform2D::row_major(
189195
self.m11 * x, self.m12,
190196
self.m21, self.m22 * y,
@@ -205,24 +211,28 @@ where T: Copy + Clone +
205211
}
206212

207213
/// Applies a rotation after self's transformation and returns the resulting transform.
208-
pub fn post_rotated(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
214+
#[must_use]
215+
pub fn post_rotate(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
209216
self.post_mul(&TypedTransform2D::create_rotation(theta))
210217
}
211218

212219
/// Applies a rotation after self's transformation and returns the resulting transform.
213-
pub fn pre_rotated(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
220+
#[must_use]
221+
pub fn pre_rotate(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
214222
self.pre_mul(&TypedTransform2D::create_rotation(theta))
215223
}
216224

217225
/// Returns the given point transformed by this transform.
218226
#[inline]
227+
#[must_use]
219228
pub fn transform_point(&self, point: &TypedPoint2D<T, Src>) -> TypedPoint2D<T, Dst> {
220229
TypedPoint2D::new(point.x * self.m11 + point.y * self.m21 + self.m31,
221230
point.x * self.m12 + point.y * self.m22 + self.m32)
222231
}
223232

224233
/// Returns the given vector transformed by this matrix.
225234
#[inline]
235+
#[must_use]
226236
pub fn transform_vector(&self, vec: &TypedVector2D<T, Src>) -> TypedVector2D<T, Dst> {
227237
vec2(vec.x * self.m11 + vec.y * self.m21,
228238
vec.x * self.m12 + vec.y * self.m22)
@@ -231,6 +241,7 @@ where T: Copy + Clone +
231241
/// Returns a rectangle that encompasses the result of transforming the given rectangle by this
232242
/// transform.
233243
#[inline]
244+
#[must_use]
234245
pub fn transform_rect(&self, rect: &TypedRect<T, Src>) -> TypedRect<T, Dst> {
235246
TypedRect::from_points(&[
236247
self.transform_point(&rect.origin),
@@ -246,6 +257,7 @@ where T: Copy + Clone +
246257
}
247258

248259
/// Returns the inverse transform if possible.
260+
#[must_use]
249261
pub fn inverse(&self) -> Option<TypedTransform2D<T, Dst, Src>> {
250262
let det = self.determinant();
251263

@@ -325,8 +337,8 @@ mod test {
325337
#[test]
326338
pub fn test_translation() {
327339
let t1 = Mat::create_translation(1.0, 2.0);
328-
let t2 = Mat::identity().pre_translated(vec2(1.0, 2.0));
329-
let t3 = Mat::identity().post_translated(vec2(1.0, 2.0));
340+
let t2 = Mat::identity().pre_translate(vec2(1.0, 2.0));
341+
let t3 = Mat::identity().post_translate(vec2(1.0, 2.0));
330342
assert_eq!(t1, t2);
331343
assert_eq!(t1, t3);
332344

@@ -338,8 +350,8 @@ mod test {
338350
#[test]
339351
pub fn test_rotation() {
340352
let r1 = Mat::create_rotation(rad(FRAC_PI_2));
341-
let r2 = Mat::identity().pre_rotated(rad(FRAC_PI_2));
342-
let r3 = Mat::identity().post_rotated(rad(FRAC_PI_2));
353+
let r2 = Mat::identity().pre_rotate(rad(FRAC_PI_2));
354+
let r3 = Mat::identity().post_rotate(rad(FRAC_PI_2));
343355
assert_eq!(r1, r2);
344356
assert_eq!(r1, r3);
345357

@@ -351,8 +363,8 @@ mod test {
351363
#[test]
352364
pub fn test_scale() {
353365
let s1 = Mat::create_scale(2.0, 3.0);
354-
let s2 = Mat::identity().pre_scaled(2.0, 3.0);
355-
let s3 = Mat::identity().post_scaled(2.0, 3.0);
366+
let s2 = Mat::identity().pre_scale(2.0, 3.0);
367+
let s3 = Mat::identity().post_scale(2.0, 3.0);
356368
assert_eq!(s1, s2);
357369
assert_eq!(s1, s3);
358370

@@ -403,8 +415,8 @@ mod test {
403415

404416
#[test]
405417
pub fn test_pre_post() {
406-
let m1 = Transform2D::identity().post_scaled(1.0, 2.0).post_translated(vec2(1.0, 2.0));
407-
let m2 = Transform2D::identity().pre_translated(vec2(1.0, 2.0)).pre_scaled(1.0, 2.0);
418+
let m1 = Transform2D::identity().post_scale(1.0, 2.0).post_translate(vec2(1.0, 2.0));
419+
let m2 = Transform2D::identity().pre_translate(vec2(1.0, 2.0)).pre_scale(1.0, 2.0);
408420
assert!(m1.approx_eq(&m2));
409421

410422
let r = Mat::create_rotation(rad(FRAC_PI_2));
@@ -432,7 +444,7 @@ mod test {
432444
pub fn test_is_identity() {
433445
let m1 = Transform2D::identity();
434446
assert!(m1.is_identity());
435-
let m2 = m1.post_translated(vec2(0.1, 0.0));
447+
let m2 = m1.post_translate(vec2(0.1, 0.0));
436448
assert!(!m2.is_identity());
437449
}
438450

src/transform3d.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ where T: Copy + Clone +
370370
}
371371

372372
/// Multiplies all of the transform's component by a scalar and returns the result.
373+
#[must_use]
373374
pub fn mul_s(&self, x: T) -> TypedTransform3D<T, Src, Dst> {
374375
TypedTransform3D::row_major(
375376
self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x,
@@ -456,12 +457,14 @@ where T: Copy + Clone +
456457
}
457458

458459
/// Returns a transform with a translation applied before self's transformation.
459-
pub fn pre_translated(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
460+
#[must_use]
461+
pub fn pre_translate(&self, v: TypedVector3D<T, Src>) -> TypedTransform3D<T, Src, Dst> {
460462
self.pre_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z))
461463
}
462464

463465
/// Returns a transform with a translation applied after self's transformation.
464-
pub fn post_translated(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
466+
#[must_use]
467+
pub fn post_translate(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
465468
self.post_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z))
466469
}
467470

@@ -477,7 +480,8 @@ where T: Copy + Clone +
477480
}
478481

479482
/// Returns a transform with a scale applied before self's transformation.
480-
pub fn pre_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
483+
#[must_use]
484+
pub fn pre_scale(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
481485
TypedTransform3D::row_major(
482486
self.m11 * x, self.m12, self.m13, self.m14,
483487
self.m21 , self.m22 * y, self.m23, self.m24,
@@ -487,7 +491,8 @@ where T: Copy + Clone +
487491
}
488492

489493
/// Returns a transform with a scale applied after self's transformation.
490-
pub fn post_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
494+
#[must_use]
495+
pub fn post_scale(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
491496
self.post_mul(&TypedTransform3D::create_scale(x, y, z))
492497
}
493498

@@ -529,12 +534,14 @@ where T: Copy + Clone +
529534
}
530535

531536
/// Returns a transform with a rotation applied after self's transformation.
532-
pub fn post_rotated(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
537+
#[must_use]
538+
pub fn post_rotate(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
533539
self.post_mul(&TypedTransform3D::create_rotation(x, y, z, theta))
534540
}
535541

536542
/// Returns a transform with a rotation applied before self's transformation.
537-
pub fn pre_rotated(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
543+
#[must_use]
544+
pub fn pre_rotate(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
538545
self.pre_mul(&TypedTransform3D::create_rotation(x, y, z, theta))
539546
}
540547

@@ -644,8 +651,8 @@ mod tests {
644651
#[test]
645652
pub fn test_translation() {
646653
let t1 = Mf32::create_translation(1.0, 2.0, 3.0);
647-
let t2 = Mf32::identity().pre_translated(vec3(1.0, 2.0, 3.0));
648-
let t3 = Mf32::identity().post_translated(vec3(1.0, 2.0, 3.0));
654+
let t2 = Mf32::identity().pre_translate(vec3(1.0, 2.0, 3.0));
655+
let t3 = Mf32::identity().post_translate(vec3(1.0, 2.0, 3.0));
649656
assert_eq!(t1, t2);
650657
assert_eq!(t1, t3);
651658

@@ -661,8 +668,8 @@ mod tests {
661668
#[test]
662669
pub fn test_rotation() {
663670
let r1 = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
664-
let r2 = Mf32::identity().pre_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
665-
let r3 = Mf32::identity().post_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
671+
let r2 = Mf32::identity().pre_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2));
672+
let r3 = Mf32::identity().post_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2));
666673
assert_eq!(r1, r2);
667674
assert_eq!(r1, r3);
668675

@@ -678,8 +685,8 @@ mod tests {
678685
#[test]
679686
pub fn test_scale() {
680687
let s1 = Mf32::create_scale(2.0, 3.0, 4.0);
681-
let s2 = Mf32::identity().pre_scaled(2.0, 3.0, 4.0);
682-
let s3 = Mf32::identity().post_scaled(2.0, 3.0, 4.0);
688+
let s2 = Mf32::identity().pre_scale(2.0, 3.0, 4.0);
689+
let s3 = Mf32::identity().post_scale(2.0, 3.0, 4.0);
683690
assert_eq!(s1, s2);
684691
assert_eq!(s1, s3);
685692

@@ -794,8 +801,8 @@ mod tests {
794801

795802
#[test]
796803
pub fn test_pre_post() {
797-
let m1 = Transform3D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(vec3(1.0, 2.0, 3.0));
798-
let m2 = Transform3D::identity().pre_translated(vec3(1.0, 2.0, 3.0)).pre_scaled(1.0, 2.0, 3.0);
804+
let m1 = Transform3D::identity().post_scale(1.0, 2.0, 3.0).post_translate(vec3(1.0, 2.0, 3.0));
805+
let m2 = Transform3D::identity().pre_translate(vec3(1.0, 2.0, 3.0)).pre_scale(1.0, 2.0, 3.0);
799806
assert!(m1.approx_eq(&m2));
800807

801808
let r = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
@@ -840,7 +847,7 @@ mod tests {
840847
pub fn test_is_identity() {
841848
let m1 = Transform3D::identity();
842849
assert!(m1.is_identity());
843-
let m2 = m1.post_translated(vec3(0.1, 0.0, 0.0));
850+
let m2 = m1.post_translate(vec3(0.1, 0.0, 0.0));
844851
assert!(!m2.is_identity());
845852
}
846853

0 commit comments

Comments
 (0)