Added inplace operations for Point

This commit is contained in:
Sebastian Verschoor
2019-03-10 11:27:28 -04:00
parent 31e879adb4
commit 94125a5dda
+80 -1
View File
@@ -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,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
);
}
}