Skip to content

Commit

Permalink
Supply web-time for wasm32 web feature
Browse files Browse the repository at this point in the history
`std::time::SystemTime` is unavailable in wasm32-unknown-unknown.
`web-time` is a drop in replacement for web environments in wasm32.
  • Loading branch information
DanGould committed Feb 27, 2024
1 parent ffc074a commit ccf3607
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ categories = ["network-programming", "data-structures", "cryptography"]
default = ["alloc"]
alloc = []
std = ["alloc"]
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
Expand Down
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
//! [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
//! [`PrivateKeyDer::clone_key()`]: https://docs.rs/rustls-pki-types/latest/rustls_pki_types/enum.PrivateKeyDer.html#method.clone_key
//!
//! ## Target `wasm32-unknown-unknown` with the `web` feature
//!
//! [`std::time::SystemTime`](https://doc.rust-lang.org/std/time/struct.SystemTime.html)
//! is unavailable in `wasm32-unknown-unknown` targets, so calls to
//! [`UnixTime::now()`](https://docs.rs/rustls-pki-types/latest/rustls_pki_types/struct.UnixTime.html#method.now),
//! otherwise enabled by the [`std`](https://docs.rs/crate/rustls-pki-types/latest/features#std) feature,
//! require building instead with the [`web`](https://docs.rs/crate/rustls-pki-types/latest/features#web)
//! feature. It gets time by calling [`Date.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now)
//! in the browser.
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(unreachable_pub, clippy::use_self)]
Expand All @@ -52,8 +62,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"))
))]
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 @@ -512,7 +527,13 @@ pub struct UnixTime(u64);

impl UnixTime {
/// The current time, as a `UnixTime`
#[cfg(feature = "std")]
#[cfg(any(
all(
feature = "std",
not(all(target_family = "wasm", target_os = "unknown"))
),
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 ccf3607

Please sign in to comment.