Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added inplace operations for Point #868

Merged
merged 3 commits into from
Mar 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added inplace operations for Point
  • Loading branch information
Sebastian Verschoor committed Mar 10, 2019
commit 94125a5dda082fd2f3d22d8e82ed8b0274204ae9
81 changes: 80 additions & 1 deletion src/sdl2/rect.rs
Original file line number Diff line number Diff line change
@@ -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<i32> for Point {
type Output = Point;

@@ -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;

@@ -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};
@@ -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,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
);
}

}