Skip to content

Commit 7f46dc9

Browse files
author
bors-servo
authored
Auto merge of #40 - mbrubeck:ops, r=emilio
Implement `i32 * Au`, `Au / Au`, and `Au % Au` operations None
2 parents 2a3ba23 + 9ae950b commit 7f46dc9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "app_units"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
authors = ["The Servo Project Developers"]
55
description = "Servo app units type (Au)"
66
documentation = "http://doc.servo.org/app_units/"

src/app_unit.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ impl Sub for Au {
8383

8484
}
8585

86+
impl Mul<Au> for i32 {
87+
type Output = Au;
88+
89+
#[inline]
90+
fn mul(self, other: Au) -> Au {
91+
if let Some(new) = other.0.checked_mul(self) {
92+
Au(new).clamp()
93+
} else if (self > 0) ^ (other.0 > 0) {
94+
MIN_AU
95+
} else {
96+
MAX_AU
97+
}
98+
}
99+
}
100+
86101
impl Mul<i32> for Au {
87102
type Output = Au;
88103

@@ -98,6 +113,15 @@ impl Mul<i32> for Au {
98113
}
99114
}
100115

116+
impl Div for Au {
117+
type Output = i32;
118+
119+
#[inline]
120+
fn div(self, other: Au) -> i32 {
121+
self.0 / other.0
122+
}
123+
}
124+
101125
impl Div<i32> for Au {
102126
type Output = Au;
103127

@@ -107,6 +131,15 @@ impl Div<i32> for Au {
107131
}
108132
}
109133

134+
impl Rem for Au {
135+
type Output = Au;
136+
137+
#[inline]
138+
fn rem(self, other: Au) -> Au {
139+
Au(self.0 % other.0)
140+
}
141+
}
142+
110143
impl Rem<i32> for Au {
111144
type Output = Au;
112145

@@ -273,12 +306,21 @@ fn operations() {
273306
assert_eq!(MIN_AU - Au(1), MIN_AU);
274307

275308
assert_eq!(Au(7) * 5, Au(35));
309+
assert_eq!(5 * Au(7), Au(35));
276310
assert_eq!(MAX_AU * -1, MIN_AU);
277311
assert_eq!(MIN_AU * -1, MAX_AU);
312+
assert_eq!(-1 * MAX_AU, MIN_AU);
313+
assert_eq!(-1 * MIN_AU, MAX_AU);
314+
315+
assert_eq!((Au(14) / 5) * 5 + Au(14) % 5, Au(14));
316+
assert_eq!((Au(14) / Au(5)) * Au(5) + Au(14) % Au(5), Au(14));
278317

279318
assert_eq!(Au(35) / 5, Au(7));
280319
assert_eq!(Au(35) % 6, Au(5));
281320

321+
assert_eq!(Au(35) / Au(5), 7);
322+
assert_eq!(Au(35) / Au(5), 7);
323+
282324
assert_eq!(-Au(7), Au(-7));
283325
}
284326

0 commit comments

Comments
 (0)