Skip to content

Commit

Permalink
Merge pull request #868 from sebastianv89/master
Browse files Browse the repository at this point in the history
Added inplace operations for Point
  • Loading branch information
Cobrand authored Mar 14, 2019
2 parents 33c5710 + e5de512 commit 0ebf344
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
81 changes: 80 additions & 1 deletion src/sdl2/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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;

Expand All @@ -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<i32> for Point {
type Output = Point;

Expand All @@ -852,6 +866,13 @@ impl Mul<i32> for Point {
}
}

impl MulAssign<i32> 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<i32> for Point {
type Output = Point;

Expand All @@ -860,6 +881,13 @@ impl Div<i32> for Point {
}
}

impl DivAssign<i32> 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};
Expand Down Expand Up @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -1057,11 +1115,32 @@ 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!(
Point::new(-3, 1),
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
);
}

}

0 comments on commit 0ebf344

Please sign in to comment.