Skip to content

Commit

Permalink
Merge pull request #804 from uuid-rs/fix/wasm-no-rng
Browse files Browse the repository at this point in the history
Add a compile_error when no source of randomness is available on wasm32-unknown-unknown
  • Loading branch information
KodrAus authored Feb 14, 2025
2 parents 5421417 + bf28001 commit 7893ecc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ Add the following to your `Cargo.toml`:
version = "1.13.1"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]
```

Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@
//! `borsh`.
//! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for
//! fuzzing.
//! * `fast-rng` - uses a faster algorithm for generating random UUIDs.
//! * `fast-rng` - uses a faster algorithm for generating random UUIDs when available.
//! This feature requires more dependencies to compile, but is just as suitable for
//! UUIDs as the default algorithm.
//! * `rng-rand` - forces `rand` as the backend for randomness.
//! * `rng-getrandom` - forces `getrandom` as the backend for randomness.
//! * `bytemuck` - adds a `Pod` trait implementation to `Uuid` for byte manipulation
//!
//! # Unstable features
Expand Down Expand Up @@ -166,9 +168,9 @@
//! produce random bytes yourself and then pass them to [`Builder::from_random_bytes`]
//! without enabling the `v4` or `v7` features.
//!
//! Versions of `uuid` `1.12` or earlier relied on `getrandom` for randomness, this
//! is no longer guaranteed and configuring `getrandom`'s provider is not guaranteed
//! to make other features relying on randomness work.
//! If you're using `getrandom`, you can specify the `rng-getrandom` or `rng-rand`
//! features of `uuid` and configure `getrandom`'s provider per its docs. `uuid`
//! may upgrade its version of `getrandom` in minor releases.
//!
//! # Examples
//!
Expand Down
7 changes: 6 additions & 1 deletion src/rng.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(dead_code)] // Keeps our cfg's from becoming too convoluted in here
#![allow(dead_code, unused_imports)] // Keeps our cfg's from becoming too convoluted in here

trait Rng {
fn u128() -> u128;
Expand Down Expand Up @@ -100,8 +100,13 @@ mod imp {
Random support for `wasm32-unknown-unknown`.
*/

#![allow(dead_code, unused_imports)] // Keeps our cfg's from becoming too convoluted in here

use super::*;

#[cfg(all(not(feature = "js"), not(feature = "rng-getrandom"), not(feature = "rng-rand")))]
compile_error!("to use `uuid` on `wasm32-unknown-unknown`, specify a source of randomness using one of the `js`, `rng-getrandom`, or `rng-rand` features");

// Using `rand`
#[cfg(feature = "rng-rand")]
pub(super) struct RngImp;
Expand Down

0 comments on commit 7893ecc

Please sign in to comment.