Skip to content

[breaking change] Add SystemTimeError #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ impl SystemTime {
}
}

pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, ()> {
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
let dur_ms = self.0 - earlier.0;
if dur_ms < 0.0 {
return Err(());
return Err(SystemTimeError(Duration::from_millis(-dur_ms as u64)));
}
Ok(Duration::from_millis(dur_ms as u64))
}

pub fn elapsed(&self) -> Result<Duration, ()> {
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
self.duration_since(SystemTime::now())
}

Expand Down Expand Up @@ -238,3 +238,25 @@ impl SubAssign<Duration> for SystemTime {
*self = *self - rhs;
}
}

#[derive(Clone, Debug)]
pub struct SystemTimeError(Duration);

impl SystemTimeError {
pub fn duration(&self) -> Duration {
self.0
}
}

impl std::fmt::Display for SystemTimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "second time provided was later than self")
}
}

impl std::error::Error for SystemTimeError {
#[allow(deprecated)]
fn description(&self) -> &str {
"other time was not earlier than self"
}
}