From 94125a5dda082fd2f3d22d8e82ed8b0274204ae9 Mon Sep 17 00:00:00 2001 From: Sebastian Verschoor Date: Sun, 10 Mar 2019 11:27:28 -0400 Subject: [PATCH 1/3] Added inplace operations for Point --- src/sdl2/rect.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) 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 + ); + } + } From 979d57d70af32af509c92643d9fdfd69dcdf4b9a Mon Sep 17 00:00:00 2001 From: Sebastian Verschoor Date: Wed, 13 Mar 2019 15:48:51 -0400 Subject: [PATCH 2/3] Updated changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 91b7ef6c9f3..432a0ef88c5 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,7 @@ when upgrading from a version of rust-sdl2 to another. ### v0.32.1 [PR #827](https://github.com/Rust-SDL2/rust-sdl2/pull/827): -Added 32-bit array pixelformats +Added inplace operations for `rect::Point`. [PR #824](https://github.com/Rust-SDL2/rust-sdl2/pull/824): Added `controller::set_rumble` and `joystick::set_rumble`, wrappers for `SDL_GameControllerRumble` and `SDL_JoystickRumble` respectively. From e5de512bd4e19f3b0093621a26053487ec3b5e52 Mon Sep 17 00:00:00 2001 From: Sebastian Verschoor Date: Wed, 13 Mar 2019 15:50:53 -0400 Subject: [PATCH 3/3] Updated changelog, (hopefully) without messing it up this time... --- changelog.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 432a0ef88c5..363a657dd3e 100644 --- a/changelog.md +++ b/changelog.md @@ -3,9 +3,12 @@ when upgrading from a version of rust-sdl2 to another. ### v0.32.1 -[PR #827](https://github.com/Rust-SDL2/rust-sdl2/pull/827): +[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 + [PR #824](https://github.com/Rust-SDL2/rust-sdl2/pull/824): Added `controller::set_rumble` and `joystick::set_rumble`, wrappers for `SDL_GameControllerRumble` and `SDL_JoystickRumble` respectively.