Skip to content

Commit

Permalink
Gate js-sys dependency behind 'wasm-bindgen' feature (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2i committed Aug 5, 2022
1 parent ea2d104 commit c605aac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -37,6 +37,7 @@ quickcheck = ["quickcheck-dep", "alloc"]
serde-human-readable = ["serde", "formatting", "parsing"]
serde-well-known = ["serde/alloc", "formatting", "parsing"] # use case for weak feature dependencies (`alloc` could just require `serde?.alloc`)
std = ["alloc"]
wasm-bindgen = ["js-sys"]

[dependencies]
itoa = { version = "1.0.1", optional = true }
Expand All @@ -50,7 +51,7 @@ libc = "0.2.98"
num_threads = "0.1.2"

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

[dev-dependencies]
rand = { version = "0.8.4", default-features = false }
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -65,6 +65,12 @@
//!
//! Enables [quickcheck](https://docs.rs/quickcheck) support for all types except [`Instant`].
//!
//! - `wasm-bindgen`
//!
//! Enables [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) support for converting
//! [JavaScript dates](https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Date.html), as
//! well as obtaining the UTC offset from JavaScript.
//!
//! One pseudo-feature flag that is only available to end users is the `unsound_local_offset` cfg.
//! As the name indicates, using the feature is unsound, and [may cause unexpected segmentation
//! faults](https://github.com/time-rs/time/issues/293). Unlike other flags, this is deliberately
Expand Down
9 changes: 6 additions & 3 deletions src/offset_date_time.rs
Expand Up @@ -59,7 +59,8 @@ impl OffsetDateTime {
pub fn now_utc() -> Self {
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
{
js_sys::Date::new_0().into()
Expand Down Expand Up @@ -1301,7 +1302,8 @@ impl From<OffsetDateTime> for SystemTime {
#[allow(clippy::fallible_impl_from)]
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
impl From<js_sys::Date> for OffsetDateTime {
fn from(js_date: js_sys::Date) -> Self {
Expand All @@ -1314,7 +1316,8 @@ impl From<js_sys::Date> for OffsetDateTime {

#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
impl From<OffsetDateTime> for js_sys::Date {
fn from(datetime: OffsetDateTime) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion src/sys/local_offset_at/mod.rs
Expand Up @@ -5,7 +5,8 @@
#[cfg_attr(
all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
),
path = "wasm_js.rs"
)]
Expand Down

0 comments on commit c605aac

Please sign in to comment.