Skip to content

Commit

Permalink
Implement i32 * Au, Au / Au, and Au % Au operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Dec 15, 2017
1 parent 2a3ba23 commit 9ae950b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
42 changes: 42 additions & 0 deletions src/app_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ impl Sub for Au {

}

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

Expand All @@ -98,6 +113,15 @@ impl Mul<i32> for Au {
}
}

impl Div for Au {
type Output = i32;

#[inline]
fn div(self, other: Au) -> i32 {
self.0 / other.0
}
}

impl Div<i32> for Au {
type Output = Au;

Expand All @@ -107,6 +131,15 @@ impl Div<i32> for Au {
}
}

impl Rem for Au {
type Output = Au;

#[inline]
fn rem(self, other: Au) -> Au {
Au(self.0 % other.0)
}
}

impl Rem<i32> for Au {
type Output = Au;

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

Expand Down

0 comments on commit 9ae950b

Please sign in to comment.