From 8da3baf457f97d743012f03fd5ea13856db25fa2 Mon Sep 17 00:00:00 2001 From: Eric Sheppard Date: Thu, 28 Jul 2022 20:45:36 +1000 Subject: [PATCH] dont use stub.rs when wasmbind feature is enabled no longer require wasmbind feature to get js-sys on wasm32-unknown-unknown target --- CHANGELOG.md | 1 + Cargo.toml | 6 +++--- README.md | 1 - src/datetime/mod.rs | 6 +++--- src/lib.rs | 1 - src/offset/local/mod.rs | 28 +++++++++++++++++++++------- src/offset/local/stub.rs | 4 ++-- src/offset/utc.rs | 9 ++++++--- 8 files changed, 36 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 768011d77d..51fd48e902 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Versions with only mechanical changes will be omitted from the following list. * Add the `and_local_timezone` method to `NaiveDateTime` * Fix the behavior of `Duration::abs()` for negative durations with non-zero nanos * Add compatibility with rfc2822 comments (#733) +* Make `js-sys` and `wasm-bindgen` enabled by default when target is `wasm32-unknown-unknown` for ease of API discovery ## 0.4.19 diff --git a/Cargo.toml b/Cargo.toml index 64d3d06e0e..44b7034a5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ libc = [] std = [] clock = ["std", "winapi"] oldtime = ["time"] -wasmbind = ["wasm-bindgen", "js-sys"] +wasmbind = [] # TODO: empty feature to avoid breaking change in 0.4.20, can be removed later unstable-locales = ["pure-rust-locales", "alloc"] __internal_bench = ["criterion"] __doctest = [] @@ -42,8 +42,8 @@ criterion = { version = "0.3", optional = true } rkyv = {version = "0.7", optional = true} [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] -wasm-bindgen = { version = "0.2", optional = true } -js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API +wasm-bindgen = { version = "0.2" } +js-sys = { version = "0.3" } # contains FFI bindings for the JS Date API [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi"], optional = true } diff --git a/README.md b/README.md index 59d4eb7139..fa7e289de5 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,6 @@ UNIX-like operating systems and the Windows API (`winapi`) for Windows. Optional features: -- `wasmbind`: Enable integration with [wasm-bindgen][] and its `js-sys` project - [`serde`][]: Enable serialization/deserialization via serde. - `unstable-locales`: Enable localization. This adds various methods with a `_localized` suffix. The implementation and API may change or even be diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index f814b23180..28cb16d23a 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -994,21 +994,21 @@ impl From> for SystemTime { } } -#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] +#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] impl From for DateTime { fn from(date: js_sys::Date) -> DateTime { DateTime::::from(&date) } } -#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] +#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] impl From<&js_sys::Date> for DateTime { fn from(date: &js_sys::Date) -> DateTime { Utc.timestamp_millis(date.get_time() as i64) } } -#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] +#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] impl From> for js_sys::Date { /// Converts a `DateTime` to a JS `Date`. The resulting value may be lossy, /// any values that have a millisecond timestamp value greater/less than ±8,640,000,000,000,000 diff --git a/src/lib.rs b/src/lib.rs index fb71d10c61..d3f3d673c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,6 @@ //! //! Optional features: //! -//! - `wasmbind`: Enable integration with [wasm-bindgen][] and its `js-sys` project //! - [`serde`][]: Enable serialization/deserialization via serde. //! - `unstable-locales`: Enable localization. This adds various methods with a //! `_localized` suffix. The implementation and API may change or even be diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index cae9278bb4..5f13172301 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -11,7 +11,12 @@ use super::{LocalResult, TimeZone}; use crate::naive::{NaiveDate, NaiveDateTime}; use crate::{Date, DateTime}; -#[cfg(all(not(unix), not(windows)))] +// we don't want `stub.rs` when using the wasmbind feature as we use js-sys to get the date instead +#[cfg(all( + not(unix), + not(windows), + not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))) +))] #[path = "stub.rs"] mod inner; @@ -51,13 +56,16 @@ impl Local { } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] + #[cfg(not(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] pub fn now() -> DateTime { inner::now() } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] pub fn now() -> DateTime { use super::Utc; let now: DateTime = super::Utc::now(); @@ -101,7 +109,7 @@ impl TimeZone for Local { midnight.map(|datetime| Date::from_utc(*local, *datetime.offset())) } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { let mut local = local.clone(); // Get the offset from the js runtime @@ -110,7 +118,10 @@ impl TimeZone for Local { LocalResult::Single(DateTime::from_utc(local, offset)) } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] + #[cfg(not(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { inner::naive_to_local(local, true) } @@ -120,14 +131,17 @@ impl TimeZone for Local { Date::from_utc(*utc, *midnight.offset()) } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { // Get the offset from the js runtime let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60); DateTime::from_utc(*utc, offset) } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] + #[cfg(not(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { // this is OK to unwrap as getting local time from a UTC // timestamp is never ambiguous diff --git a/src/offset/local/stub.rs b/src/offset/local/stub.rs index d4d76c5a9b..57e491d5c6 100644 --- a/src/offset/local/stub.rs +++ b/src/offset/local/stub.rs @@ -18,7 +18,7 @@ pub(super) fn now() -> DateTime { } /// Converts a local `NaiveDateTime` to the `time::Timespec`. -#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] +#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult> { let tm = Tm { tm_sec: d.second() as i32, @@ -54,7 +54,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult DateTime { if tm.tm_sec >= 60 { tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000; diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 730c03e95f..457006116d 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -6,7 +6,7 @@ use core::fmt; #[cfg(all( feature = "clock", - not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")) + not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))) ))] use std::time::{SystemTime, UNIX_EPOCH}; @@ -47,7 +47,10 @@ impl Utc { } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] + #[cfg(not(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] pub fn now() -> DateTime { let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch"); @@ -56,7 +59,7 @@ impl Utc { } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] pub fn now() -> DateTime { let now = js_sys::Date::new_0(); DateTime::::from(now)