Skip to content

Commit

Permalink
Merge pull request #794 from uuid-rs/feat/getrandom-wasm32
Browse files Browse the repository at this point in the history
Support forcing `getrandom` on `wasm32-unknown-unknown` without JavaScript
  • Loading branch information
KodrAus authored Feb 5, 2025
2 parents 75ff98c + 9e7300c commit 6494c4b
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 65 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ jobs:
- name: Fast RNG
run: wasm-pack test --node -- --features "js v4 fast-rng"

- name: rng-getrandom
env:
RUSTFLAGS: '--cfg getrandom_backend="wasm_js"'
working-directory: ./tests/wasm32-getrandom-test
run: wasm-pack test --node

- name: rng-rand
env:
RUSTFLAGS: '--cfg getrandom_backend="wasm_js"'
working-directory: ./tests/wasm32-getrandom-test
run: wasm-pack test --node -- --features "rand"

wasi:
name: Tests / WebAssembly (WASI)
runs-on: ubuntu-latest
Expand Down
18 changes: 15 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ v8 = []
js = ["dep:wasm-bindgen"]

rng = ["dep:getrandom"]
rng-getrandom = ["rng", "dep:getrandom", "uuid-rng-internal-lib", "uuid-rng-internal-lib/getrandom"]
rng-rand = ["rng", "dep:rand", "uuid-rng-internal-lib", "uuid-rng-internal-lib/rand"]

fast-rng = ["rng", "dep:rand"]

sha1 = ["dep:sha1_smol"]
Expand Down Expand Up @@ -126,13 +129,20 @@ default-features = false
# Private
# (Formally public)
[target.'cfg(not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))'.dependencies.getrandom]
optional = true
version = "0.3"
optional = true

[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-rng-internal-lib]
# Work-around lack of support for both `dep:x` and `x/` in MSRV
package = "uuid-rng-internal"
version = "1.12.1"
path = "rng"
optional = true

# Private
[target.'cfg(not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))'.dependencies.rand]
optional = true
version = "0.9"
optional = true

# Private
[dependencies.md-5]
Expand Down Expand Up @@ -175,7 +185,7 @@ version = "1.0"
[dev-dependencies.serde_test]
version = "1.0.56"

[target.'cfg(target = "wasm32-unknown-unknown")'.dev-dependencies.wasm-bindgen]
[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dev-dependencies.wasm-bindgen]
version = "0.2"

[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dev-dependencies.wasm-bindgen-test]
Expand All @@ -190,6 +200,8 @@ version = "1"
[workspace]
members = [
"macros",
"rng",
"examples",
"tests/smoke-test",
"tests/wasm32-getrandom-test",
]
26 changes: 26 additions & 0 deletions rng/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "uuid-rng-internal"
version = "1.12.1"
edition = "2018"
authors = [
"uuid-rs contributors"
]
categories = [
"data-structures",
"no-std",
"parser-implementations",
"wasm"
]
description = "Private implementation details of the uuid crate."
documentation = "https://docs.rs/uuid"
repository = "https://github.com/uuid-rs/uuid"
license = "Apache-2.0 OR MIT"

# Forces a dependency on `getrandom`
[dependencies.getrandom]
version = "0.3"
optional = true

[dependencies.rand]
version = "0.9"
optional = true
20 changes: 20 additions & 0 deletions rng/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Implementation details for the `uuid` crate.
//!
//! This crate is not meant to be used directly. It
//! allows `wasm32-unknown-unknown` users who aren't
//! in a JS-enabled runtime to configure a source of
//! randomness via `getrandom`:
//!
//! ```toml
//! [dependencies.uuid]
//! features = ["v4", "rng-getrandom"]
//! ```
#[doc(hidden)]
pub mod __private {
#[cfg(feature = "getrandom")]
pub use getrandom;

#[cfg(feature = "rand")]
pub use rand;
}
4 changes: 3 additions & 1 deletion src/external/serde_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ impl<'de> Deserialize<'de> for NonNilUuid {
{
let uuid = Uuid::deserialize(deserializer)?;

NonNilUuid::try_from(uuid).map_err(|_| de::Error::invalid_value(de::Unexpected::Other("nil UUID"), &"a non-nil UUID"))
NonNilUuid::try_from(uuid).map_err(|_| {
de::Error::invalid_value(de::Unexpected::Other("nil UUID"), &"a non-nil UUID")
})
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,13 @@ pub enum Variant {
// NOTE: Also check `NonNilUuid` when ading new derives here
#[cfg_attr(
all(uuid_unstable, feature = "zerocopy"),
derive(zerocopy::IntoBytes, zerocopy::FromBytes, zerocopy::KnownLayout, zerocopy::Immutable, zerocopy::Unaligned)
derive(
zerocopy::IntoBytes,
zerocopy::FromBytes,
zerocopy::KnownLayout,
zerocopy::Immutable,
zerocopy::Unaligned
)
)]
#[cfg_attr(
feature = "borsh",
Expand Down
Loading

0 comments on commit 6494c4b

Please sign in to comment.