From ccf3607d17839e762384dc43fccd0cb61d04bd57 Mon Sep 17 00:00:00 2001 From: DanGould Date: Fri, 9 Feb 2024 20:13:01 -0500 Subject: [PATCH 1/2] Supply `web-time` for wasm32 web feature `std::time::SystemTime` is unavailable in wasm32-unknown-unknown. `web-time` is a drop in replacement for web environments in wasm32. --- Cargo.toml | 4 ++++ src/lib.rs | 25 +++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e36caf1..a4fdd2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 943c9ef..0e8f2bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] @@ -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::{ @@ -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() From 15db00a170c6948f7bff5c7b514409a7e5136949 Mon Sep 17 00:00:00 2001 From: DanGould Date: Sat, 10 Feb 2024 12:55:18 -0500 Subject: [PATCH 2/2] Add wasm32 target CI build --- .github/workflows/ci.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c51629a..1ea1863 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,36 @@ jobs: env: RUST_BACKTRACE: 1 + wasm_build: + name: Build wasm32 + runs-on: ubuntu-20.04 + steps: + - name: Checkout sources + uses: actions/checkout@v3 + with: + persist-credentials: false + + - name: Install stable toolchain + uses: dtolnay/rust-toolchain@stable + + - name: Add wasm target + run: rustup target add wasm32-unknown-unknown + + - name: wasm32 build (debug; default features) + run: cargo build --target wasm32-unknown-unknown --lib + env: + RUST_BACKTRACE: 1 + + - name: wasm32 build (debug; all features) + run: cargo build --target wasm32-unknown-unknown --lib --all-features + env: + RUST_BACKTRACE: 1 + + - name: wasm32 build (debug; no default features) + run: cargo build --target wasm32-unknown-unknown --lib --no-default-features + env: + RUST_BACKTRACE: 1 + msrv: runs-on: ${{ matrix.os }} strategy: