Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/time/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::js::TIME_ORIGIN;
doc = "[`std::time::Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html"
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Instant(Duration);
pub struct Instant(pub(crate) Duration);

impl Instant {
/// See [`std::time::Instant::now()`].
Expand Down
44 changes: 43 additions & 1 deletion src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#![allow(clippy::absolute_paths)]

use core::time::Duration;
use std::time::SystemTime as StdSystemTime;

use crate::SystemTime;
use crate::{Instant, SystemTime};

#[cfg(all(
target_arch = "wasm32",
Expand Down Expand Up @@ -79,3 +80,44 @@ impl SystemTimeExt for SystemTime {
.expect("found `SystemTime` earlier than unix epoch")
}
}

/// Web-specific extension to [`web_time::Instant`](crate::Instant).
pub trait InstantExt {
/// Create a [`web_time::Instant`](crate::Instant) from a [`Duration`]
/// representing the monotonic time elapsed since an arbitrary, fixed
/// origin.
///
/// # Note
///
/// The resulting [`Instant`](crate::Instant) is only meaningful relative to
/// other [`Instant`](crate::Instant)s that share the same origin (whether
/// obtained via [`Instant::now()`](crate::Instant::now) or this method).
/// Mixing timestamps from different origins will yield nonsensical
/// [`Duration`]s.
///
/// [`Performance.now()`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
/// [`Performance.timeOrigin`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin
#[must_use]
fn from_duration(duration: Duration) -> Self;

/// Returns the [`Duration`] backing this [`Instant`](crate::Instant), i.e.
/// the monotonic time elapsed since the origin it was created from.
///
/// # Note
///
/// Like [`InstantExt::from_duration`], the returned [`Duration`] is only
/// meaningful relative to other [`Instant`](crate::Instant)s sharing the
/// same origin.
#[must_use]
fn as_duration(&self) -> Duration;
}

impl InstantExt for Instant {
fn from_duration(duration: Duration) -> Self {
Self(duration)
}

fn as_duration(&self) -> Duration {
self.0
}
}
Loading