Skip to content

Commit

Permalink
dont use stub.rs when wasmbind feature is enabled
Browse files Browse the repository at this point in the history
no longer require wasmbind feature to get js-sys on wasm32-unknown-unknown target

fix comment and remove wasmbind feature from tests
  • Loading branch information
esheppa committed Jul 28, 2022
1 parent e081a5b commit d20c344
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ci/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ test_wasm() {
}

test_wasm_simple() {
if ! runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind ; then
if ! runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node ; then
# sometimes on github the initial build takes 8-10 minutes, and we
# check that the time makes sense inside the test by approximately
# comparing it to the env var,
#
# so re-run the test in case it took too long
runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind
runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node
fi
}

Expand Down
6 changes: 3 additions & 3 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,21 +994,21 @@ impl<Tz: TimeZone> From<DateTime<Tz>> 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<js_sys::Date> for DateTime<Utc> {
fn from(date: js_sys::Date) -> DateTime<Utc> {
DateTime::<Utc>::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<Utc> {
fn from(date: &js_sys::Date) -> DateTime<Utc> {
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<DateTime<Utc>> for js_sys::Date {
/// Converts a `DateTime<Utc>` 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
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 22 additions & 7 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ 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 the target_os is not wasi or emscripten
// 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;

Expand Down Expand Up @@ -51,13 +57,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<Local> {
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<Local> {
use super::Utc;
let now: DateTime<Utc> = super::Utc::now();
Expand Down Expand Up @@ -101,7 +110,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<DateTime<Local>> {
let mut local = local.clone();
// Get the offset from the js runtime
Expand All @@ -110,7 +119,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<DateTime<Local>> {
inner::naive_to_local(local, true)
}
Expand All @@ -120,14 +132,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<Local> {
// 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<Local> {
// this is OK to unwrap as getting local time from a UTC
// timestamp is never ambiguous
Expand Down
4 changes: 2 additions & 2 deletions src/offset/local/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(super) fn now() -> DateTime<Local> {
}

/// 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<DateTime<Local>> {
let tm = Tm {
tm_sec: d.second() as i32,
Expand Down Expand Up @@ -54,7 +54,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date

/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
/// This assumes that `time` is working correctly, i.e. any error is fatal.
#[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 tm_to_datetime(mut tm: Tm) -> DateTime<Local> {
if tm.tm_sec >= 60 {
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
Expand Down
9 changes: 6 additions & 3 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Utc> {
let now =
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
Expand All @@ -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<Utc> {
let now = js_sys::Date::new_0();
DateTime::<Utc>::from(now)
Expand Down

0 comments on commit d20c344

Please sign in to comment.