Skip to content

Commit 1d9d4c4

Browse files
authored
Gate js-sys dependency behind 'wasm-bindgen' feature (#499)
1 parent c009038 commit 1d9d4c4

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ quickcheck = ["quickcheck-dep", "alloc"]
3737
serde-human-readable = ["serde", "formatting", "parsing"]
3838
serde-well-known = ["serde/alloc", "formatting", "parsing"] # use case for weak feature dependencies (`alloc` could just require `serde?.alloc`)
3939
std = ["alloc"]
40+
wasm-bindgen = ["js-sys"]
4041

4142
[dependencies]
4243
itoa = { version = "1.0.1", optional = true }
@@ -50,7 +51,7 @@ libc = "0.2.98"
5051
num_threads = "0.1.2"
5152

5253
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
53-
js-sys = "0.3.58"
54+
js-sys = { version = "0.3.58", optional = true }
5455

5556
[dev-dependencies]
5657
rand = { version = "0.8.4", default-features = false }

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
//!
6666
//! Enables [quickcheck](https://docs.rs/quickcheck) support for all types except [`Instant`].
6767
//!
68+
//! - `wasm-bindgen`
69+
//!
70+
//! Enables [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) support for converting
71+
//! [JavaScript dates](https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Date.html), as
72+
//! well as obtaining the UTC offset from JavaScript.
73+
//!
6874
//! One pseudo-feature flag that is only available to end users is the `unsound_local_offset` cfg.
6975
//! As the name indicates, using the feature is unsound, and [may cause unexpected segmentation
7076
//! faults](https://github.com/time-rs/time/issues/293). Unlike other flags, this is deliberately

src/offset_date_time.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ impl OffsetDateTime {
5959
pub fn now_utc() -> Self {
6060
#[cfg(all(
6161
target_arch = "wasm32",
62-
not(any(target_os = "emscripten", target_os = "wasi"))
62+
not(any(target_os = "emscripten", target_os = "wasi")),
63+
feature = "wasm-bindgen"
6364
))]
6465
{
6566
js_sys::Date::new_0().into()
6667
}
6768

6869
#[cfg(not(all(
6970
target_arch = "wasm32",
70-
not(any(target_os = "emscripten", target_os = "wasi"))
71+
not(any(target_os = "emscripten", target_os = "wasi")),
72+
feature = "wasm-bindgen"
7173
)))]
7274
SystemTime::now().into()
7375
}
@@ -1301,7 +1303,8 @@ impl From<OffsetDateTime> for SystemTime {
13011303
#[allow(clippy::fallible_impl_from)]
13021304
#[cfg(all(
13031305
target_arch = "wasm32",
1304-
not(any(target_os = "emscripten", target_os = "wasi"))
1306+
not(any(target_os = "emscripten", target_os = "wasi")),
1307+
feature = "wasm-bindgen"
13051308
))]
13061309
impl From<js_sys::Date> for OffsetDateTime {
13071310
fn from(js_date: js_sys::Date) -> Self {
@@ -1314,7 +1317,8 @@ impl From<js_sys::Date> for OffsetDateTime {
13141317

13151318
#[cfg(all(
13161319
target_arch = "wasm32",
1317-
not(any(target_os = "emscripten", target_os = "wasi"))
1320+
not(any(target_os = "emscripten", target_os = "wasi")),
1321+
feature = "wasm-bindgen"
13181322
))]
13191323
impl From<OffsetDateTime> for js_sys::Date {
13201324
fn from(datetime: OffsetDateTime) -> Self {

src/sys/local_offset_at/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#[cfg_attr(
66
all(
77
target_arch = "wasm32",
8-
not(any(target_os = "emscripten", target_os = "wasi"))
8+
not(any(target_os = "emscripten", target_os = "wasi")),
9+
feature = "wasm-bindgen"
910
),
1011
path = "wasm_js.rs"
1112
)]

0 commit comments

Comments
 (0)