Skip to content

Commit f2c2f89

Browse files
committed
Grab tomaka/wasm-timer#16 and implemented SystemTimeError
1 parent 25e3f4c commit f2c2f89

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/std.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2020
// DEALINGS IN THE SOFTWARE.
2121

22-
#![cfg(all(target_arch = "wasm32", target_os = "unknown"))]
23-
24-
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
25-
use std::ops::{Add, Sub, AddAssign, SubAssign};
22+
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
23+
use std::ops::{Add, AddAssign, Sub, SubAssign};
2624
use std::time::Duration;
2725

2826
#[derive(Debug, Copy, Clone)]
@@ -77,7 +75,9 @@ impl Add<Duration> for Instant {
7775

7876
fn add(self, other: Duration) -> Instant {
7977
let new_val = self.inner + other.as_millis() as f64;
80-
Instant { inner: new_val as f64 }
78+
Instant {
79+
inner: new_val as f64,
80+
}
8181
}
8282
}
8383

@@ -86,7 +86,9 @@ impl Sub<Duration> for Instant {
8686

8787
fn sub(self, other: Duration) -> Instant {
8888
let new_val = self.inner - other.as_millis() as f64;
89-
Instant { inner: new_val as f64 }
89+
Instant {
90+
inner: new_val as f64,
91+
}
9092
}
9193
}
9294

@@ -108,6 +110,14 @@ pub struct SystemTime {
108110
inner: f64,
109111
}
110112

113+
pub struct SystemTimeError(Duration);
114+
115+
impl SystemTimeError {
116+
pub fn duration(&self) -> Duration {
117+
self.0
118+
}
119+
}
120+
111121
impl PartialEq for SystemTime {
112122
fn eq(&self, other: &SystemTime) -> bool {
113123
// Note that this will most likely only compare equal if we clone an `SystemTime`,
@@ -138,16 +148,16 @@ impl SystemTime {
138148
SystemTime { inner: val }
139149
}
140150

141-
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, ()> {
151+
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
142152
let dur_ms = self.inner - earlier.inner;
143153
if dur_ms < 0.0 {
144-
return Err(())
154+
return Err(SystemTimeError(Duration::from_millis(dur_ms.abs() as u64)));
145155
}
146156
Ok(Duration::from_millis(dur_ms as u64))
147157
}
148158

149-
pub fn elapsed(&self) -> Result<Duration, ()> {
150-
self.duration_since(SystemTime::now())
159+
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
160+
SystemTime::now().duration_since(*self)
151161
}
152162

153163
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
@@ -164,7 +174,9 @@ impl Add<Duration> for SystemTime {
164174

165175
fn add(self, other: Duration) -> SystemTime {
166176
let new_val = self.inner + other.as_millis() as f64;
167-
SystemTime { inner: new_val as f64 }
177+
SystemTime {
178+
inner: new_val as f64,
179+
}
168180
}
169181
}
170182

@@ -173,7 +185,9 @@ impl Sub<Duration> for SystemTime {
173185

174186
fn sub(self, other: Duration) -> SystemTime {
175187
let new_val = self.inner - other.as_millis() as f64;
176-
SystemTime { inner: new_val as f64 }
188+
SystemTime {
189+
inner: new_val as f64,
190+
}
177191
}
178192
}
179193

0 commit comments

Comments
 (0)