From d3c2bddf33f594caa8e87647a0ed4fae895b785d Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 08:56:17 +1000 Subject: [PATCH 1/8] support forcing rand or getrandom on wasm32-unknown-unknown --- .github/workflows/ci.yml | 10 ++ Cargo.toml | 13 +- getrandom/Cargo.toml | 21 +++ getrandom/src/lib.rs | 16 ++ src/external/serde_support.rs | 4 +- src/lib.rs | 8 +- src/rng.rs | 232 ++++++++++++++++++------- tests/wasm32-getrandom-test/Cargo.toml | 21 +++ tests/wasm32-getrandom-test/src/lib.rs | 14 ++ 9 files changed, 276 insertions(+), 63 deletions(-) create mode 100644 getrandom/Cargo.toml create mode 100644 getrandom/src/lib.rs create mode 100644 tests/wasm32-getrandom-test/Cargo.toml create mode 100644 tests/wasm32-getrandom-test/src/lib.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 428d376e..17b6e4de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -133,6 +133,16 @@ 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_dir: ./tests/wasm32-getrandom-test + run: wasm-pack test --node + + - name: rng-rand + env: RUSTFLAGS='--cfg getrandom_backend="wasm_js"' + working_dir: ./tests/wasm32-getrandom-test + run: wasm-pack test --node -- --features "rand" + wasi: name: Tests / WebAssembly (WASI) runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index d499722b..7195416f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,9 @@ v8 = [] js = ["dep:wasm-bindgen"] rng = ["dep:getrandom"] +rng-getrandom = ["rng", "dep:uuid-getrandom-internal"] +rng-rand = ["rng-getrandom", "dep:rand"] + fast-rng = ["rng", "dep:rand"] sha1 = ["dep:sha1_smol"] @@ -126,13 +129,17 @@ default-features = false # Private # (Formally public) [target.'cfg(not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))'.dependencies.getrandom] +version = "0.3" optional = true +[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-getrandom-internal] version = "0.3" +path = "getrandom" +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] @@ -175,7 +182,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] @@ -190,6 +197,8 @@ version = "1" [workspace] members = [ "macros", + "getrandom", "examples", "tests/smoke-test", + "tests/wasm32-getrandom-test", ] diff --git a/getrandom/Cargo.toml b/getrandom/Cargo.toml new file mode 100644 index 00000000..c2be7603 --- /dev/null +++ b/getrandom/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "uuid-getrandom-internal" +version = "0.3.0" +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" diff --git a/getrandom/src/lib.rs b/getrandom/src/lib.rs new file mode 100644 index 00000000..01727bb2 --- /dev/null +++ b/getrandom/src/lib.rs @@ -0,0 +1,16 @@ +//! 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 { + pub use getrandom::*; +} diff --git a/src/external/serde_support.rs b/src/external/serde_support.rs index 954a3496..bd8b330e 100644 --- a/src/external/serde_support.rs +++ b/src/external/serde_support.rs @@ -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") + }) } } diff --git a/src/lib.rs b/src/lib.rs index 77c17fc3..f0553ebe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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", diff --git a/src/rng.rs b/src/rng.rs index 378eec01..5b5403b2 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -1,9 +1,67 @@ -#[cfg(not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))] +trait Rng { + #[allow(dead_code)] + fn u128() -> u128; + + #[allow(dead_code)] + fn u64() -> u64; + + #[allow(dead_code)] + fn u16() -> u16; +} + +#[cfg(any(feature = "v4", feature = "v7"))] +pub(crate) fn u128() -> u128 { + imp::RngImp::u128() +} + +#[cfg(feature = "v7")] +pub(crate) fn u64() -> u64 { + imp::RngImp::u64() +} + +#[cfg(any(feature = "v1", feature = "v6"))] +pub(crate) fn u16() -> u16 { + imp::RngImp::u16() +} + +#[cfg(not(all( + target_arch = "wasm32", + target_vendor = "unknown", + target_os = "unknown" +)))] mod imp { - #[cfg(any(feature = "v4", feature = "v7"))] - pub(crate) fn u128() -> u128 { - #[cfg(not(feature = "fast-rng"))] - { + /* + Random support for non `wasm32-unknown-unknown` platforms. + */ + + use super::*; + + // Using `rand` + #[cfg(any(feature = "rng-rand", feature = "fast-rng"))] + pub(super) struct RngImp; + + #[cfg(any(feature = "rng-rand", feature = "fast-rng"))] + impl Rng for RngImp { + fn u128() -> u128 { + rand::random() + } + + fn u64() -> u64 { + rand::random() + } + + fn u16() -> u16 { + rand::random() + } + } + + // Using `getrandom` + #[cfg(all(not(feature = "fast-rng"), not(feature = "rng-rand")))] + pub(super) struct RngImp; + + #[cfg(all(not(feature = "fast-rng"), not(feature = "rng-rand")))] + impl Rng for RngImp { + fn u128() -> u128 { let mut bytes = [0u8; 16]; getrandom::fill(&mut bytes).unwrap_or_else(|err| { @@ -14,16 +72,18 @@ mod imp { u128::from_ne_bytes(bytes) } - #[cfg(feature = "fast-rng")] - { - rand::random() + fn u64() -> u64 { + let mut bytes = [0u8; 8]; + + getrandom::fill(&mut bytes).unwrap_or_else(|err| { + // NB: getrandom::Error has no source; this is adequate display + panic!("could not retrieve random bytes for uuid: {}", err) + }); + + u64::from_ne_bytes(bytes) } - } - #[cfg(any(feature = "v1", feature = "v6"))] - pub(crate) fn u16() -> u16 { - #[cfg(not(feature = "fast-rng"))] - { + fn u16() -> u16 { let mut bytes = [0u8; 2]; getrandom::fill(&mut bytes).unwrap_or_else(|err| { @@ -33,20 +93,61 @@ mod imp { u16::from_ne_bytes(bytes) } + } +} - #[cfg(feature = "fast-rng")] - { +#[cfg(all( + target_arch = "wasm32", + target_vendor = "unknown", + target_os = "unknown" +))] +mod imp { + /* + Random support for `wasm32-unknown-unknown`. + */ + + use super::*; + + // Using `rand` + #[cfg(feature = "rng-rand")] + pub(super) struct RngImp; + + #[cfg(feature = "rng-rand")] + impl Rng for RngImp { + fn u128() -> u128 { + rand::random() + } + + fn u64() -> u64 { + rand::random() + } + + fn u16() -> u16 { rand::random() } } - #[cfg(feature = "v7")] - pub(crate) fn u64() -> u64 { - #[cfg(not(feature = "fast-rng"))] - { + // Using `getrandom` + #[cfg(all(feature = "rng-getrandom", not(feature = "rng-rand")))] + pub(super) struct RngImp; + + #[cfg(all(feature = "rng-getrandom", not(feature = "rng-rand")))] + impl Rng for RngImp { + fn u128() -> u128 { + let mut bytes = [0u8; 16]; + + uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { + // NB: getrandom::Error has no source; this is adequate display + panic!("could not retrieve random bytes for uuid: {}", err) + }); + + u128::from_ne_bytes(bytes) + } + + fn u64() -> u64 { let mut bytes = [0u8; 8]; - getrandom::fill(&mut bytes).unwrap_or_else(|err| { + uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); @@ -54,56 +155,74 @@ mod imp { u64::from_ne_bytes(bytes) } - #[cfg(feature = "fast-rng")] - { - rand::random() + fn u16() -> u16 { + let mut bytes = [0u8; 2]; + + uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { + // NB: getrandom::Error has no source; this is adequate display + panic!("could not retrieve random bytes for uuid: {}", err) + }); + + u16::from_ne_bytes(bytes) } } -} -#[cfg(all(feature = "js", target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))] -mod imp { - /* - This module preserves the stabilized behavior of `uuid` that requires the - `js` feature to enable rng on `wasm32-unknown-unknown`, which it inherited - from `getrandom` `0.2` - */ + // Using WebCrypto via `wasm-bindgen` + #[cfg(all( + feature = "js", + not(feature = "rng-rand"), + not(feature = "rng-getrandom") + ))] + pub(super) struct RngImp; + + #[cfg(all( + feature = "js", + not(feature = "rng-rand"), + not(feature = "rng-getrandom") + ))] + impl Rng for RngImp { + fn u128() -> u128 { + let mut bytes = [0u8; 16]; - #[cfg(any(feature = "v4", feature = "v7"))] - pub(crate) fn u128() -> u128 { - let mut bytes = [0u8; 16]; + if !webcrypto::fill(&mut bytes) { + panic!("could not retrieve random bytes for uuid") + } - if !getrandom::fill(&mut bytes) { - panic!("could not retrieve random bytes for uuid") + u128::from_ne_bytes(bytes) } - u128::from_ne_bytes(bytes) - } + fn u64() -> u64 { + let mut bytes = [0u8; 8]; - #[cfg(any(feature = "v1", feature = "v6"))] - pub(crate) fn u16() -> u16 { - let mut bytes = [0u8; 2]; + if !webcrypto::fill(&mut bytes) { + panic!("could not retrieve random bytes for uuid") + } - if !getrandom::fill(&mut bytes) { - panic!("could not retrieve random bytes for uuid") + u64::from_ne_bytes(bytes) } - u16::from_ne_bytes(bytes) - } + fn u16() -> u16 { + let mut bytes = [0u8; 2]; - #[cfg(feature = "v7")] - pub(crate) fn u64() -> u64 { - let mut bytes = [0u8; 8]; + if !webcrypto::fill(&mut bytes) { + panic!("could not retrieve random bytes for uuid") + } - if !getrandom::fill(&mut bytes) { - panic!("could not retrieve random bytes for uuid") + u16::from_ne_bytes(bytes) } - - u64::from_ne_bytes(bytes) } - mod getrandom { + #[cfg(all( + feature = "js", + not(feature = "rng-rand"), + not(feature = "rng-getrandom") + ))] + mod webcrypto { /* + This module preserves the stabilized behavior of `uuid` that requires the + `js` feature to enable rng on `wasm32-unknown-unknown`, which it inherited + from `getrandom` `0.2`. + Vendored from `getrandom`: https://github.com/rust-random/getrandom/blob/ce3b017fdee0233c6ecd61e68b96a84bf6f911bf/src/backends/wasm_js.rs Copyright (c) 2018-2024 The rust-random Project Developers @@ -180,7 +299,7 @@ mod imp { sub_buf.copy_to_uninit(chunk); } - + true } @@ -196,8 +315,3 @@ mod imp { } } } - -#[cfg(all(not(feature = "js"), target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))] -compile_error!("the `js` feature is required for the `wasm32-unknown-unknown` target"); - -pub(crate) use self::imp::*; diff --git a/tests/wasm32-getrandom-test/Cargo.toml b/tests/wasm32-getrandom-test/Cargo.toml new file mode 100644 index 00000000..54ef3bfe --- /dev/null +++ b/tests/wasm32-getrandom-test/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "uuid-wasm32-getrandom-test" +version = "0.1.0" +edition = "2024" + +[features] +rand = ["uuid/rng-rand"] + +[dependencies.uuid] +path = "../../" +features = ["v4", "rng-getrandom"] + +[dependencies.getrandom] +version = "0.3" +features = ["wasm_js"] + +[dependencies.wasm-bindgen] +version = "0.2" + +[dependencies.wasm-bindgen-test] +version = "0.3" diff --git a/tests/wasm32-getrandom-test/src/lib.rs b/tests/wasm32-getrandom-test/src/lib.rs new file mode 100644 index 00000000..83db260c --- /dev/null +++ b/tests/wasm32-getrandom-test/src/lib.rs @@ -0,0 +1,14 @@ +#![cfg(test)] + +use uuid::{Uuid, Variant, Version}; + +use wasm_bindgen_test::*; + +#[test] +#[wasm_bindgen_test] +fn test_new() { + let uuid = Uuid::new_v4(); + + assert_eq!(uuid.get_version(), Some(Version::Random)); + assert_eq!(uuid.get_variant(), Variant::RFC4122); +} From 406fb23317a25e34a8e5689a892a99dc12413571 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 08:58:23 +1000 Subject: [PATCH 2/8] tweak uuid-getrandom-internal version --- Cargo.toml | 3 ++- getrandom/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7195416f..2412fee9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -131,8 +131,9 @@ default-features = false [target.'cfg(not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))'.dependencies.getrandom] version = "0.3" optional = true + [target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-getrandom-internal] -version = "0.3" +version = "1.12.1" path = "getrandom" optional = true diff --git a/getrandom/Cargo.toml b/getrandom/Cargo.toml index c2be7603..41c89796 100644 --- a/getrandom/Cargo.toml +++ b/getrandom/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uuid-getrandom-internal" -version = "0.3.0" +version = "1.12.1" edition = "2018" authors = [ "uuid-rs contributors" From 1ed9390699f94078b743702e4ac1f21fd9b6c067 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 09:06:10 +1000 Subject: [PATCH 3/8] fix up feature selection --- Cargo.toml | 10 +++++----- {getrandom => rng}/Cargo.toml | 7 ++++++- {getrandom => rng}/src/lib.rs | 6 +++++- src/rng.rs | 12 ++++++------ 4 files changed, 22 insertions(+), 13 deletions(-) rename {getrandom => rng}/Cargo.toml (82%) rename {getrandom => rng}/src/lib.rs (77%) diff --git a/Cargo.toml b/Cargo.toml index 2412fee9..cda368cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,8 +71,8 @@ v8 = [] js = ["dep:wasm-bindgen"] rng = ["dep:getrandom"] -rng-getrandom = ["rng", "dep:uuid-getrandom-internal"] -rng-rand = ["rng-getrandom", "dep:rand"] +rng-getrandom = ["rng", "dep:getrandom", "dep:uuid-rng-internal", "uuid-rng-internal/getrandom"] +rng-rand = ["rng", "dep:rand", "dep:uuid-rng-internal", "uuid-rng-internal/rand"] fast-rng = ["rng", "dep:rand"] @@ -132,9 +132,9 @@ default-features = false version = "0.3" optional = true -[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-getrandom-internal] +[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-rng-internal] version = "1.12.1" -path = "getrandom" +path = "rng" optional = true # Private @@ -198,7 +198,7 @@ version = "1" [workspace] members = [ "macros", - "getrandom", + "rng", "examples", "tests/smoke-test", "tests/wasm32-getrandom-test", diff --git a/getrandom/Cargo.toml b/rng/Cargo.toml similarity index 82% rename from getrandom/Cargo.toml rename to rng/Cargo.toml index 41c89796..ec3b384e 100644 --- a/getrandom/Cargo.toml +++ b/rng/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "uuid-getrandom-internal" +name = "uuid-rng-internal" version = "1.12.1" edition = "2018" authors = [ @@ -19,3 +19,8 @@ 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 diff --git a/getrandom/src/lib.rs b/rng/src/lib.rs similarity index 77% rename from getrandom/src/lib.rs rename to rng/src/lib.rs index 01727bb2..7b0b22e1 100644 --- a/getrandom/src/lib.rs +++ b/rng/src/lib.rs @@ -12,5 +12,9 @@ #[doc(hidden)] pub mod __private { - pub use getrandom::*; + #[cfg(feature = "getrandom")] + pub use getrandom; + + #[cfg(feature = "rand")] + pub use rand; } diff --git a/src/rng.rs b/src/rng.rs index 5b5403b2..6236446a 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -115,15 +115,15 @@ mod imp { #[cfg(feature = "rng-rand")] impl Rng for RngImp { fn u128() -> u128 { - rand::random() + uuid_rng_internal::__private::rand::random() } fn u64() -> u64 { - rand::random() + uuid_rng_internal::__private::rand::random() } fn u16() -> u16 { - rand::random() + uuid_rng_internal::__private::rand::random() } } @@ -136,7 +136,7 @@ mod imp { fn u128() -> u128 { let mut bytes = [0u8; 16]; - uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); @@ -147,7 +147,7 @@ mod imp { fn u64() -> u64 { let mut bytes = [0u8; 8]; - uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); @@ -158,7 +158,7 @@ mod imp { fn u16() -> u16 { let mut bytes = [0u8; 2]; - uuid_getrandom_internal::__private::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); From 83295ba5eb2f57610b90021f4b7e076025d63905 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 09:24:13 +1000 Subject: [PATCH 4/8] fix up env vars --- .github/workflows/ci.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 17b6e4de..f95db64b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -134,12 +134,14 @@ jobs: run: wasm-pack test --node -- --features "js v4 fast-rng" - name: rng-getrandom - env: RUSTFLAGS='--cfg getrandom_backend="wasm_js"' + env: + RUSTFLAGS: '--cfg getrandom_backend="wasm_js"' working_dir: ./tests/wasm32-getrandom-test run: wasm-pack test --node - name: rng-rand - env: RUSTFLAGS='--cfg getrandom_backend="wasm_js"' + env: + RUSTFLAGS: '--cfg getrandom_backend="wasm_js"' working_dir: ./tests/wasm32-getrandom-test run: wasm-pack test --node -- --features "rand" From 9c117f451212d8363b88d0c72f922313cd23cf84 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 09:31:30 +1000 Subject: [PATCH 5/8] fix up more yaml --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f95db64b..f3ff5428 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -136,13 +136,13 @@ jobs: - name: rng-getrandom env: RUSTFLAGS: '--cfg getrandom_backend="wasm_js"' - working_dir: ./tests/wasm32-getrandom-test + working-directory: ./tests/wasm32-getrandom-test run: wasm-pack test --node - name: rng-rand env: RUSTFLAGS: '--cfg getrandom_backend="wasm_js"' - working_dir: ./tests/wasm32-getrandom-test + working-directory: ./tests/wasm32-getrandom-test run: wasm-pack test --node -- --features "rand" wasi: From 2c662f156aa151fc7b351ba26cbeb0ed3e74a0fd Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 09:33:58 +1000 Subject: [PATCH 6/8] downgrade test edition --- tests/wasm32-getrandom-test/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wasm32-getrandom-test/Cargo.toml b/tests/wasm32-getrandom-test/Cargo.toml index 54ef3bfe..354c74b7 100644 --- a/tests/wasm32-getrandom-test/Cargo.toml +++ b/tests/wasm32-getrandom-test/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "uuid-wasm32-getrandom-test" version = "0.1.0" -edition = "2024" +edition = "2018" [features] rand = ["uuid/rng-rand"] From 6c155b477ac6c7c6397760896987bb347b08709f Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 10:41:14 +1000 Subject: [PATCH 7/8] work around lack of dep:x and x/y feature support in 1.63 --- Cargo.toml | 8 +++++--- src/rng.rs | 12 ++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cda368cf..20a4909c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,8 +71,8 @@ v8 = [] js = ["dep:wasm-bindgen"] rng = ["dep:getrandom"] -rng-getrandom = ["rng", "dep:getrandom", "dep:uuid-rng-internal", "uuid-rng-internal/getrandom"] -rng-rand = ["rng", "dep:rand", "dep:uuid-rng-internal", "uuid-rng-internal/rand"] +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"] @@ -132,7 +132,9 @@ default-features = false version = "0.3" optional = true -[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"))'.dependencies.uuid-rng-internal] +[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 diff --git a/src/rng.rs b/src/rng.rs index 6236446a..b0470212 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -115,15 +115,15 @@ mod imp { #[cfg(feature = "rng-rand")] impl Rng for RngImp { fn u128() -> u128 { - uuid_rng_internal::__private::rand::random() + uuid_rng_internal_lib::__private::rand::random() } fn u64() -> u64 { - uuid_rng_internal::__private::rand::random() + uuid_rng_internal_lib::__private::rand::random() } fn u16() -> u16 { - uuid_rng_internal::__private::rand::random() + uuid_rng_internal_lib::__private::rand::random() } } @@ -136,7 +136,7 @@ mod imp { fn u128() -> u128 { let mut bytes = [0u8; 16]; - uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal_lib::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); @@ -147,7 +147,7 @@ mod imp { fn u64() -> u64 { let mut bytes = [0u8; 8]; - uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal_lib::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); @@ -158,7 +158,7 @@ mod imp { fn u16() -> u16 { let mut bytes = [0u8; 2]; - uuid_rng_internal::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { + uuid_rng_internal_lib::__private::getrandom::fill(&mut bytes).unwrap_or_else(|err| { // NB: getrandom::Error has no source; this is adequate display panic!("could not retrieve random bytes for uuid: {}", err) }); From 9e7300ca4aa4d6b56a1599f3ebd9d7e22f324c4d Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 5 Feb 2025 11:05:39 +1000 Subject: [PATCH 8/8] fix some warnings on various feature enablement --- src/rng.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/rng.rs b/src/rng.rs index b0470212..774ce2b7 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -1,25 +1,19 @@ +#![allow(dead_code)] // Keeps our cfg's from becoming too convoluted in here + trait Rng { - #[allow(dead_code)] fn u128() -> u128; - - #[allow(dead_code)] fn u64() -> u64; - - #[allow(dead_code)] fn u16() -> u16; } -#[cfg(any(feature = "v4", feature = "v7"))] pub(crate) fn u128() -> u128 { imp::RngImp::u128() } -#[cfg(feature = "v7")] pub(crate) fn u64() -> u64 { imp::RngImp::u64() } -#[cfg(any(feature = "v1", feature = "v6"))] pub(crate) fn u16() -> u16 { imp::RngImp::u16() } @@ -212,11 +206,7 @@ mod imp { } } - #[cfg(all( - feature = "js", - not(feature = "rng-rand"), - not(feature = "rng-getrandom") - ))] + #[cfg(feature = "js")] mod webcrypto { /* This module preserves the stabilized behavior of `uuid` that requires the