Skip to content

Commit

Permalink
Supply instant::SystemTime for wasm32
Browse files Browse the repository at this point in the history
`std::time::SystemTime` is unavailable in wasm32-unknown-unknown.
  • Loading branch information
DanGould committed Feb 10, 2024
1 parent 386d084 commit 118e402
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ default = ["alloc"]
alloc = []
std = ["alloc"]

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.features]
web = ["web-time"]

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

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ use alloc::vec::Vec;
use core::fmt;
use core::ops::Deref;
use core::time::Duration;
#[cfg(feature = "std")]
#[cfg(all(
feature = "std",
not(all(target_family = "wasm", target_os = "unknown", feature = "web"))
))]
use std::time::SystemTime;
#[cfg(all(target_family = "wasm", target_os = "unknown", feature = "web"))]
use web_time::SystemTime;

mod server_name;
pub use server_name::{
Expand Down Expand Up @@ -478,7 +483,10 @@ pub struct UnixTime(u64);

impl UnixTime {
/// The current time, as a `UnixTime`
#[cfg(feature = "std")]
#[cfg(any(
feature = "std",
all(target_family = "wasm", target_os = "unknown", feature = "web")
))]
pub fn now() -> Self {
Self::since_unix_epoch(
SystemTime::now()
Expand Down

0 comments on commit 118e402

Please sign in to comment.