diff --git a/Cargo.toml b/Cargo.toml index 96bbc3fd903386fad76aaf9bc57c48b70307687c..59c20226f44ec131aaa7e71ef42b4ede1dd1fdb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "web_instant" -version = "0.2.1" +version = "0.3.0" edition = "2021" description = "Cross platform impl of Instant" authors = [ diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000000000000000000000000000000000000..d62aed71624f22a7ec26ece40fb3e1db30dffa50 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +hard_tabs = true +#group_imports = "StdExternalCrate" +use_field_init_shorthand = true +use_try_shorthand = true \ No newline at end of file diff --git a/src/instant_desktop.rs b/src/instant_desktop.rs index 3c9a723186d5aea819d8fa6e33d3c367052e7db4..2888cc1bf3ac5a2e50c003d7207e4cb85399b213 100644 --- a/src/instant_desktop.rs +++ b/src/instant_desktop.rs @@ -1,6 +1,6 @@ use std::fmt::{Debug, Formatter}; use std::ops::{Add, AddAssign, Sub, SubAssign}; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime}; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Copy, Clone, PartialEq, PartialOrd)] @@ -54,6 +54,18 @@ impl Spot { pub fn checked_sub(&self, duration: Duration) -> Option<Spot> { self.inner.checked_sub(duration).map(|inner| Spot { inner }) } + + /// Converts this `Spot` into a duration that represents the time that has elapsed between the + /// UNIX Epoch and this `Spot` + pub fn duration_since_epoch(&self) -> Duration { + let duration_since_epoch = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("Failed to get time since epoch"); + + let duration_since_instant = self.elapsed(); + + duration_since_epoch - duration_since_instant + } } impl Add<Duration> for Spot { @@ -64,7 +76,8 @@ impl Add<Duration> for Spot { /// This function may panic if the resulting point in time cannot be represented by the /// underlying data structure. See [`Spot::checked_add`] for a version without panic. fn add(self, other: Duration) -> Spot { - self.checked_add(other).expect("overflow when adding duration to instant") + self.checked_add(other) + .expect("overflow when adding duration to instant") } } @@ -78,7 +91,8 @@ impl Sub<Duration> for Spot { type Output = Spot; fn sub(self, other: Duration) -> Spot { - self.checked_sub(other).expect("overflow when subtracting duration from instant") + self.checked_sub(other) + .expect("overflow when subtracting duration from instant") } } diff --git a/src/instant_web.rs b/src/instant_web.rs index 9a6439d8fb2afc091d217162f342da7f9672d50e..88f52653f66ad83982be5a02cc00485898bf5048 100644 --- a/src/instant_web.rs +++ b/src/instant_web.rs @@ -1,6 +1,6 @@ use std::fmt::{Debug, Formatter}; use std::ops::{Add, AddAssign, Sub, SubAssign}; -use std::time::Duration; +use std::time::{Duration, Instant}; #[derive(Copy, Clone, PartialEq, PartialOrd)] pub struct Spot { @@ -27,7 +27,10 @@ impl Spot { /// Returns the amount of time elapsed from another instant to this one, /// or zero duration if that instant is later than this one. pub fn duration_since(&self, earlier: Spot) -> Duration { - self.checked_duration_since(earlier).unwrap_or_default() + match self.checked_duration_since(earlier) { + Some(duration) => duration, + None => std::process::abort(), + } } /// Returns the amount of time elapsed from another instant to this one, /// or None if that instant is later than this one. @@ -45,14 +48,19 @@ impl Spot { /// Returns the amount of time elapsed from another instant to this one, /// or zero duration if that instant is later than this one. pub fn saturating_duration_since(&self, earlier: Spot) -> Duration { - self.checked_duration_since(earlier).unwrap_or_default() + match self.checked_duration_since(earlier) { + Some(duration) => duration, + None => std::process::abort(), + } } /// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as /// `Instant` (which means it's inside the bounds of the underlying data structure), `None` /// otherwise. pub fn checked_add(&self, duration: Duration) -> Option<Spot> { let duration_millis = duration.as_secs_f64() / 1000.0; - Some(Spot { inner: self.inner + duration_millis }) + Some(Spot { + inner: self.inner + duration_millis, + }) } /// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as /// `Instant` (which means it's inside the bounds of the underlying data structure), `None` @@ -62,9 +70,17 @@ impl Spot { if duration_millis > self.inner { None } else { - Some(Spot { inner: self.inner - duration_millis }) + Some(Spot { + inner: self.inner - duration_millis, + }) } } + + /// Converts this `Spot` into a duration that represents the time that has elapsed between the + /// UNIX Epoch and this `Spot` + pub fn duration_since_epoch(&self) -> Duration { + self.duration_since(Spot { inner: 0.0 }) + } } impl Add<Duration> for Spot { @@ -75,7 +91,13 @@ impl Add<Duration> for Spot { /// This function may panic if the resulting point in time cannot be represented by the /// underlying data structure. See [`Spot::checked_add`] for a version without panic. fn add(self, other: Duration) -> Spot { - self.checked_add(other).expect("overflow when adding duration to instant") + match self.checked_add(other) { + Some(duration) => duration, + None => { + eprint!("overflow when adding duration to instant"); + std::process::abort(); + } + } } } @@ -89,7 +111,13 @@ impl Sub<Duration> for Spot { type Output = Spot; fn sub(self, other: Duration) -> Spot { - self.checked_sub(other).expect("overflow when subtracting duration from instant") + match self.checked_sub(other) { + Some(duration) => duration, + None => { + eprintln!("overflow when subtracting duration from instant"); + std::process::abort(); + } + } } } @@ -99,7 +127,6 @@ impl SubAssign<Duration> for Spot { } } - impl Sub<Spot> for Spot { type Output = Duration; diff --git a/src/lib.rs b/src/lib.rs index 8e1134221f943d8d39e83a63e7252ddff163a22a..282860d000310fc82d7b3a59c1d5aa6b1e6b28a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,23 @@ //! # Web Instant -//! -//! Provides `Spot`, a version of `Instant` that works both in environments that support `Instant` and `wasm` family targets. On desktop, the internal representation uses `std::time::Instant`. On the web, the internal representation is an `f64`, -//! and uses `js_sys::Date` methods to cover functinality where required -//! -//! All the methods on `Instant` are implemented for `Spot`, so you can just replace uses of `std::time::Instant` with `web_instant::Spot` -//! +//! +//! Provides `Spot`, a version of `Instant` that works both in environments that support `Instant` and +//! `wasm` family targets. On desktop, the internal representation uses `std::time::Instant`. On the +//! web, the internal representation is an `f64`, and uses `js_sys::Date` methods to cover +//! functionality where required +//! +//! All the methods on `Instant` are implemented for `Spot`, so you can just replace uses of +//! `std::time::Instant` with `web_instant::Spot` +//! //! ## Install -//! +//! //! `cargo add web_instant` -//! +//! //! ## Usage -//! +//! //! ```rust //! use web_instant::Spot; //! use std::time::Duration; -//! +//! //! fn my_cross_platform_timer(last_time: Spot) { //! let time_diff: Duration = Spot::now() - last_time; //! println!("It has been {} seconds", time_diff.as_secs());