Skip to content

Commit

Permalink
Use web-time for wasm32-unknown-unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay committed Aug 1, 2024
1 parent 1077b0b commit 85bd0db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
5 changes: 4 additions & 1 deletion tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ signal = [
]
sync = []
test-util = ["rt", "sync", "time"]
time = []
time = ["web-time"]

[dependencies]
tokio-macros = { version = "~2.4.0", path = "../tokio-macros", optional = true }
Expand All @@ -98,6 +98,9 @@ parking_lot = { version = "0.12.0", optional = true }
[target.'cfg(not(target_family = "wasm"))'.dependencies]
socket2 = { version = "0.5.5", optional = true, features = [ "all" ] }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
web-time = { version = "1", optional = true }

# Currently unstable. The API exposed by these features may be broken at any time.
# Requires `--cfg tokio_unstable` to enable.
[target.'cfg(tokio_unstable)'.dependencies]
Expand Down
18 changes: 18 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,21 @@ macro_rules! cfg_is_wasm_not_wasi {
)*
}
}

macro_rules! cfg_wasm_unknown {
($($item:item)*) => {
$(
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
$item
)*
}
}

macro_rules! cfg_not_wasm_unknown {
($($item:item)*) => {
$(
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
$item
)*
}
}
12 changes: 10 additions & 2 deletions tokio/src/time/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ cfg_not_test_util! {
#[derive(Debug, Clone)]
pub(crate) struct Clock {}

pub(crate) fn now() -> Instant {
Instant::from_std(std::time::Instant::now())
cfg_not_wasm_unknown! {
pub(crate) fn now() -> Instant {
Instant::from_std(std::time::Instant::now())
}
}

cfg_wasm_unknown! {
pub(crate) fn now() -> Instant {
Instant::from_std(web_time::Instant::now())
}
}

impl Clock {
Expand Down
31 changes: 21 additions & 10 deletions tokio/src/time/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ use std::fmt;
use std::ops;
use std::time::Duration;

cfg_wasm_unknown! {
type InstantInner = web_time::Instant;
}

cfg_not_wasm_unknown! {
type InstantInner = std::time::Instant;
}

/// A measurement of a monotonically nondecreasing clock.
/// Opaque and useful only with `Duration`.
///
Expand Down Expand Up @@ -32,7 +40,7 @@ use std::time::Duration;
/// take advantage of `time::pause()` and `time::advance()`.
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Instant {
std: std::time::Instant,
std: InstantInner,
}

impl Instant {
Expand All @@ -50,7 +58,7 @@ impl Instant {
}

/// Create a `tokio::time::Instant` from a `std::time::Instant`.
pub fn from_std(std: std::time::Instant) -> Instant {
pub fn from_std(std: InstantInner) -> Instant {
Instant { std }
}

Expand All @@ -63,7 +71,7 @@ impl Instant {
}

/// Convert the value into a `std::time::Instant`.
pub fn into_std(self) -> std::time::Instant {
pub fn into_std(self) -> InstantInner {
self.std
}

Expand Down Expand Up @@ -150,14 +158,14 @@ impl Instant {
}
}

impl From<std::time::Instant> for Instant {
fn from(time: std::time::Instant) -> Instant {
impl From<InstantInner> for Instant {
fn from(time: InstantInner) -> Instant {
Instant::from_std(time)
}
}

impl From<Instant> for std::time::Instant {
fn from(time: Instant) -> std::time::Instant {
impl From<Instant> for InstantInner {
fn from(time: Instant) -> InstantInner {
time.into_std()
}
}
Expand Down Expand Up @@ -188,7 +196,7 @@ impl ops::Sub<Duration> for Instant {
type Output = Instant;

fn sub(self, rhs: Duration) -> Instant {
Instant::from_std(std::time::Instant::sub(self.std, rhs))
Instant::from_std(InstantInner::sub(self.std, rhs))
}
}

Expand All @@ -206,10 +214,13 @@ impl fmt::Debug for Instant {

#[cfg(not(feature = "test-util"))]
mod variant {
use super::Instant;
use super::{
Instant,
InstantInner,
};

pub(super) fn now() -> Instant {
Instant::from_std(std::time::Instant::now())
Instant::from_std(InstantInner::now())
}
}

Expand Down

0 comments on commit 85bd0db

Please sign in to comment.