From 9ae950b8c5a06f7e11ece2b562d9fe575a7b6984 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Fri, 15 Dec 2017 11:36:01 -0600 Subject: [PATCH] Implement `i32 * Au`, `Au / Au`, and `Au % Au` operations --- Cargo.toml | 2 +- src/app_unit.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ce9e7aa..1bcf33e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "app_units" -version = "0.6.0" +version = "0.6.1" authors = ["The Servo Project Developers"] description = "Servo app units type (Au)" documentation = "http://doc.servo.org/app_units/" diff --git a/src/app_unit.rs b/src/app_unit.rs index 7922457..8acab1b 100644 --- a/src/app_unit.rs +++ b/src/app_unit.rs @@ -83,6 +83,21 @@ impl Sub for Au { } +impl Mul for i32 { + type Output = Au; + + #[inline] + fn mul(self, other: Au) -> Au { + if let Some(new) = other.0.checked_mul(self) { + Au(new).clamp() + } else if (self > 0) ^ (other.0 > 0) { + MIN_AU + } else { + MAX_AU + } + } +} + impl Mul for Au { type Output = Au; @@ -98,6 +113,15 @@ impl Mul for Au { } } +impl Div for Au { + type Output = i32; + + #[inline] + fn div(self, other: Au) -> i32 { + self.0 / other.0 + } +} + impl Div for Au { type Output = Au; @@ -107,6 +131,15 @@ impl Div for Au { } } +impl Rem for Au { + type Output = Au; + + #[inline] + fn rem(self, other: Au) -> Au { + Au(self.0 % other.0) + } +} + impl Rem for Au { type Output = Au; @@ -273,12 +306,21 @@ fn operations() { assert_eq!(MIN_AU - Au(1), MIN_AU); assert_eq!(Au(7) * 5, Au(35)); + assert_eq!(5 * Au(7), Au(35)); assert_eq!(MAX_AU * -1, MIN_AU); assert_eq!(MIN_AU * -1, MAX_AU); + assert_eq!(-1 * MAX_AU, MIN_AU); + assert_eq!(-1 * MIN_AU, MAX_AU); + + assert_eq!((Au(14) / 5) * 5 + Au(14) % 5, Au(14)); + assert_eq!((Au(14) / Au(5)) * Au(5) + Au(14) % Au(5), Au(14)); assert_eq!(Au(35) / 5, Au(7)); assert_eq!(Au(35) % 6, Au(5)); + assert_eq!(Au(35) / Au(5), 7); + assert_eq!(Au(35) / Au(5), 7); + assert_eq!(-Au(7), Au(-7)); }