diff --git a/changelog.md b/changelog.md index 91b7ef6c9f3..363a657dd3e 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,9 @@ when upgrading from a version of rust-sdl2 to another. ### v0.32.1 +[PR #868](https://github.com/Rust-SDL2/rust-sdl2/pull/868): +Added inplace operations for `rect::Point`. + [PR #827](https://github.com/Rust-SDL2/rust-sdl2/pull/827): Added 32-bit array pixelformats diff --git a/src/sdl2/rect.rs b/src/sdl2/rect.rs index f336e7110c4..ce6151db5cd 100644 --- a/src/sdl2/rect.rs +++ b/src/sdl2/rect.rs @@ -4,7 +4,7 @@ use sys; use std::mem; use std::ptr; -use std::ops::{Deref, DerefMut, Add, BitAnd, BitOr, Div, Mul, Neg, Sub}; +use std::ops::{Deref, DerefMut, Add, AddAssign, BitAnd, BitOr, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use std::convert::{AsRef, AsMut}; use std::hash::{Hash, Hasher}; @@ -828,6 +828,13 @@ impl Add for Point { } } +impl AddAssign for Point { + fn add_assign(&mut self, rhs: Point) { + self.raw.x = clamp_position(self.x() + rhs.x()); + self.raw.y = clamp_position(self.y() + rhs.y()); + } +} + impl Neg for Point { type Output = Point; @@ -844,6 +851,13 @@ impl Sub for Point { } } +impl SubAssign for Point { + fn sub_assign(&mut self, rhs: Point) { + self.raw.x = clamp_position(self.x() - rhs.x()); + self.raw.y = clamp_position(self.y() - rhs.y()); + } +} + impl Mul for Point { type Output = Point; @@ -852,6 +866,13 @@ impl Mul for Point { } } +impl MulAssign for Point { + fn mul_assign(&mut self, rhs: i32) { + self.raw.x = clamped_mul(self.x(), rhs); + self.raw.y = clamped_mul(self.y(), rhs); + } +} + impl Div for Point { type Output = Point; @@ -860,6 +881,13 @@ impl Div for Point { } } +impl DivAssign for Point { + fn div_assign(&mut self, rhs: i32) { + self.raw.x /= rhs; + self.raw.y /= rhs; + } +} + #[cfg(test)] mod test { use super::{Rect, Point, max_int_value, min_int_value}; @@ -1033,6 +1061,16 @@ mod test { ); } + #[test] + fn point_add_assign() { + let mut point = Point::new(-11, 5); + point += Point::new(6, 2); + assert_eq!( + point, + Point::new(-11, 5) + Point::new(6, 2) + ); + } + #[test] fn point_sub() { assert_eq!( @@ -1041,6 +1079,16 @@ mod test { ); } + #[test] + fn point_sub_assign() { + let mut point = Point::new(-11, 5); + point -= Point::new(6, 2); + assert_eq!( + point, + Point::new(-11, 5) - Point::new(6, 2) + ); + } + #[test] fn point_mul() { assert_eq!( @@ -1049,6 +1097,16 @@ mod test { ); } + #[test] + fn point_mul_assign() { + let mut point = Point::new(-11, 5); + point *= 3; + assert_eq!( + point, + Point::new(-11, 5) * 3 + ); + } + #[test] fn point_mul_clamp() { assert_eq!( @@ -1057,6 +1115,16 @@ mod test { ); } + #[test] + fn point_mul_assign_clamp() { + let mut point = Point::new(-1000000, 5000000); + point *= -3000000; + assert_eq!( + point, + Point::new(-1000000, 5000000) * -3000000 + ); + } + #[test] fn point_div() { assert_eq!( @@ -1064,4 +1132,15 @@ mod test { Point::new(-11, 5) / 3 ); } + + #[test] + fn point_div_assign () { + let mut point = Point::new(-11, 5); + point /= 3; + assert_eq!( + point, + Point::new(-11, 5) / 3 + ); + } + }