From d4a480ae42304341ae3af8259a44d00a7d645a9b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 10 Nov 2025 15:56:16 +0000 Subject: [PATCH 01/19] Add rand_core as an optional dependency --- Cargo.lock | 7 +++++++ Cargo.toml | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 983abcef..3a0907cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,7 @@ dependencies = [ "js-sys", "libc", "r-efi", + "rand_core", "wasip2", "wasm-bindgen", "wasm-bindgen-test", @@ -105,6 +106,12 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand_core" +version = "0.10.0-rc-2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "104a23e4e8b77312a823b6b5613edbac78397e2f34320bc7ac4277013ec4478e" + [[package]] name = "same-file" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 0190fbd9..fc0f1294 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,12 @@ std = [] # i.e. avoid unconditionally enabling it in library crates. wasm_js = ["dep:wasm-bindgen", "dep:js-sys"] +# Provide OsRng over rand_core +rng = ["dep:rand_core"] + [dependencies] cfg-if = "1" +rand_core = { version = "0.10.0-rc-2", optional = true } # getrandom / linux_android_with_fallback [target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(all(target_os = "linux", target_env = ""), getrandom_backend = "custom", getrandom_backend = "linux_raw", getrandom_backend = "rdrand", getrandom_backend = "rndr"))))'.dependencies] From d2f6a1d477e74a14e453a721b6575026b8fbc112 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 10 Nov 2025 16:01:52 +0000 Subject: [PATCH 02/19] Add OsRng --- src/lib.rs | 7 +++++++ src/rng.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/rng.rs diff --git a/src/lib.rs b/src/lib.rs index befea654..3867c2d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,13 @@ mod util; #[cfg(feature = "std")] mod error_std_impls; +#[cfg(feature = "rng")] +mod rng; +#[cfg(feature = "rng")] +pub extern crate rand_core; +#[cfg(feature = "rng")] +pub use rng::OsRng; + pub use crate::error::{Error, RawOsError}; /// Fill `dest` with random bytes from the system's preferred random number source. diff --git a/src/rng.rs b/src/rng.rs new file mode 100644 index 00000000..8e0e1848 --- /dev/null +++ b/src/rng.rs @@ -0,0 +1,60 @@ +//! rand_core adapter + +use crate::Error; +use rand_core::{TryCryptoRng, TryRngCore}; + +/// An RNG over the operating-system's random data source +/// +/// This is a zero-sized struct. It can be freely constructed with just `OsRng`. +/// +/// This struct is also available as [`rand::rngs::OsRng`] when using [rand]. +/// +/// # Usage example +/// ``` +/// use getrandom::{rand_core::{TryRngCore, RngCore}, OsRng}; +/// +/// let mut key = [0u8; 32]; +/// OsRng.try_fill_bytes(&mut key).unwrap(); +/// +/// let mut rng = OsRng.unwrap_err(); +/// let random_u64 = rng.next_u64(); +/// ``` +/// +/// [rand]: https://crates.io/crates/rand +/// [`rand::rngs::OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html +#[derive(Clone, Copy, Debug, Default)] +pub struct OsRng; + +impl TryRngCore for OsRng { + type Error = Error; + + #[inline] + fn try_next_u32(&mut self) -> Result { + crate::u32() + } + + #[inline] + fn try_next_u64(&mut self) -> Result { + crate::u64() + } + + #[inline] + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + crate::fill(dest) + } +} + +impl TryCryptoRng for OsRng {} + +#[test] +fn test_os_rng() { + let x = OsRng.try_next_u64().unwrap(); + let y = OsRng.try_next_u64().unwrap(); + assert!(x != 0); + assert!(x != y); +} + +#[test] +fn test_construction() { + assert!(OsRng.try_next_u64().unwrap() != 0); +} From bdbe38d0cfd60edd1350f73db0913c4911f37a8c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 10 Nov 2025 16:03:35 +0000 Subject: [PATCH 03/19] Test rng feature in CI --- .github/workflows/tests.yml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index abb18135..15010d21 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: - uses: Swatinem/rust-cache@v2 - run: cargo test # Make sure enabling the std feature doesn't break anything - - run: cargo test --features=std + - run: cargo test --features=std,rng - if: ${{ matrix.toolchain == 'nightly' }} run: cargo test --benches @@ -53,27 +53,27 @@ jobs: with: targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@v2 - - run: cargo test --target=${{ matrix.target }} --features=std + - run: cargo test --target=${{ matrix.target }} --features=std,rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo test --target=${{ matrix.target }} --features=std + run: cargo test --target=${{ matrix.target }} --features=std,rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw" - run: cargo test --target=${{ matrix.target }} --features=std + run: cargo test --target=${{ matrix.target }} --features=std,rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback - run: cargo test --features=std + run: cargo test --features=std,rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback - run: cargo test --features=std + run: cargo test --features=std,rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo test --features=std + run: cargo test --features=std,rng ios: name: iOS Simulator @@ -123,7 +123,7 @@ jobs: with: toolchain: ${{ matrix.toolchain }} - uses: Swatinem/rust-cache@v2 - - run: cargo test --features=std + - run: cargo test --features=std,rng windows7: name: Windows 7 (on Windows 10) @@ -136,8 +136,8 @@ jobs: toolchain: nightly-2025-09-28 components: rust-src - uses: Swatinem/rust-cache@v2 - - run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std - - run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std + - run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std,rng + - run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std,rng sanitizer-linux: name: Sanitizer Linux @@ -216,7 +216,7 @@ jobs: wget -O - $URL | tar -xz -C ~/.cargo/bin cross --version - name: Test - run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std + run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,rng freebsd: name: FreeBSD VM @@ -273,14 +273,14 @@ jobs: description: Web, version: stable, flags: '-Dwarnings --cfg getrandom_backend="wasm_js"', - args: '--features=std,wasm_js', + args: '--features=std,rng,wasm_js', } - { description: Web with Atomics, version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std,wasm_js -Zbuild-std=panic_abort,std', + args: '--features=std,rng,wasm_js -Zbuild-std=panic_abort,std', } steps: - uses: actions/checkout@v5 From 26cb7e9f0f27b471f24a4395d74b2a875098fe3c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 19 Nov 2025 09:58:51 +0000 Subject: [PATCH 04/19] Test std / rng / std,rng feature combinations --- .github/workflows/tests.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 15010d21..b24123a5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,10 @@ jobs: - uses: Swatinem/rust-cache@v2 - run: cargo test # Make sure enabling the std feature doesn't break anything - - run: cargo test --features=std,rng + - run: | + cargo test --features=std + cargo test --features=rng + cargo test --features=std,rng - if: ${{ matrix.toolchain == 'nightly' }} run: cargo test --benches From 254be581d08b318854fd5b8caf78448e0dbb0928 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 19 Nov 2025 10:00:42 +0000 Subject: [PATCH 05/19] Use rng feature in docs builds --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc0f1294..26eba103 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ repository = "https://github.com/rust-random/getrandom" categories = ["os", "no-std"] exclude = [".*"] +[package.metadata.docs.rs] +features = ["std", "rng"] + [features] # Implement From for std::io::Error and # use std to retrieve OS error descriptions @@ -89,9 +92,6 @@ check-cfg = [ 'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this. ] -[package.metadata.docs.rs] -features = ["std"] - # workaround for https://github.com/cross-rs/cross/issues/1345 [package.metadata.cross.target.x86_64-unknown-netbsd] pre-build = [ From ff68f2a3d737f6b721070646f953c9e280783a18 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 19 Nov 2025 10:07:24 +0000 Subject: [PATCH 06/19] Use test module --- src/rng.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/rng.rs b/src/rng.rs index 8e0e1848..d9d63d2a 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -46,15 +46,20 @@ impl TryRngCore for OsRng { impl TryCryptoRng for OsRng {} -#[test] -fn test_os_rng() { - let x = OsRng.try_next_u64().unwrap(); - let y = OsRng.try_next_u64().unwrap(); - assert!(x != 0); - assert!(x != y); -} +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_os_rng() { + let x = OsRng.try_next_u64().unwrap(); + let y = OsRng.try_next_u64().unwrap(); + assert!(x != 0); + assert!(x != y); + } -#[test] -fn test_construction() { - assert!(OsRng.try_next_u64().unwrap() != 0); + #[test] + fn test_construction() { + assert!(OsRng.try_next_u64().unwrap() != 0); + } } From 8bdcacfd7c2cfcb29217e9a751ade60d438a5263 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 19 Nov 2025 10:19:29 +0000 Subject: [PATCH 07/19] pub use rand_core --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3867c2d9..648b5ccc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ mod error_std_impls; #[cfg(feature = "rng")] mod rng; #[cfg(feature = "rng")] -pub extern crate rand_core; +pub use rand_core; #[cfg(feature = "rng")] pub use rng::OsRng; From 1e873b4bd9ff35ebd495f55f296e04e28bd53673 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 19 Nov 2025 10:19:44 +0000 Subject: [PATCH 08/19] Document what OsRng implements --- src/rng.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/rng.rs b/src/rng.rs index d9d63d2a..a47e1d7e 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -10,11 +10,19 @@ use rand_core::{TryCryptoRng, TryRngCore}; /// This struct is also available as [`rand::rngs::OsRng`] when using [rand]. /// /// # Usage example +/// +/// `OsRng` implements [`TryRngCore`]: /// ``` -/// use getrandom::{rand_core::{TryRngCore, RngCore}, OsRng}; +/// use getrandom::{rand_core::TryRngCore, OsRng}; /// /// let mut key = [0u8; 32]; /// OsRng.try_fill_bytes(&mut key).unwrap(); +/// ``` +/// +/// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]: +/// ``` +/// use getrandom::rand_core::{TryRngCore, RngCore}; +/// use getrandom::OsRng; /// /// let mut rng = OsRng.unwrap_err(); /// let random_u64 = rng.next_u64(); @@ -22,6 +30,7 @@ use rand_core::{TryCryptoRng, TryRngCore}; /// /// [rand]: https://crates.io/crates/rand /// [`rand::rngs::OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html +/// [`RngCore`]: rand_core::RngCore #[derive(Clone, Copy, Debug, Default)] pub struct OsRng; From b7ed994b67029af6151633c7f3317838bf749a2c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:12:46 +0000 Subject: [PATCH 09/19] Rename OsRng -> SysRng --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/rng.rs | 28 ++++++++++++++-------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 26eba103..ae71ba9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ std = [] # i.e. avoid unconditionally enabling it in library crates. wasm_js = ["dep:wasm-bindgen", "dep:js-sys"] -# Provide OsRng over rand_core +# Provide SysRng over rand_core rng = ["dep:rand_core"] [dependencies] diff --git a/src/lib.rs b/src/lib.rs index 648b5ccc..6e31fada 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ mod rng; #[cfg(feature = "rng")] pub use rand_core; #[cfg(feature = "rng")] -pub use rng::OsRng; +pub use rng::SysRng; pub use crate::error::{Error, RawOsError}; diff --git a/src/rng.rs b/src/rng.rs index a47e1d7e..bf210e3d 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -5,36 +5,36 @@ use rand_core::{TryCryptoRng, TryRngCore}; /// An RNG over the operating-system's random data source /// -/// This is a zero-sized struct. It can be freely constructed with just `OsRng`. +/// This is a zero-sized struct. It can be freely constructed with just `SysRng`. /// -/// This struct is also available as [`rand::rngs::OsRng`] when using [rand]. +/// This struct is also available as [`rand::rngs::SysRng`] when using [rand]. /// /// # Usage example /// -/// `OsRng` implements [`TryRngCore`]: +/// `SysRng` implements [`TryRngCore`]: /// ``` -/// use getrandom::{rand_core::TryRngCore, OsRng}; +/// use getrandom::{rand_core::TryRngCore, SysRng}; /// /// let mut key = [0u8; 32]; -/// OsRng.try_fill_bytes(&mut key).unwrap(); +/// SysRng.try_fill_bytes(&mut key).unwrap(); /// ``` /// /// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]: /// ``` /// use getrandom::rand_core::{TryRngCore, RngCore}; -/// use getrandom::OsRng; +/// use getrandom::SysRng; /// -/// let mut rng = OsRng.unwrap_err(); +/// let mut rng = SysRng.unwrap_err(); /// let random_u64 = rng.next_u64(); /// ``` /// /// [rand]: https://crates.io/crates/rand -/// [`rand::rngs::OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html +/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html /// [`RngCore`]: rand_core::RngCore #[derive(Clone, Copy, Debug, Default)] -pub struct OsRng; +pub struct SysRng; -impl TryRngCore for OsRng { +impl TryRngCore for SysRng { type Error = Error; #[inline] @@ -53,7 +53,7 @@ impl TryRngCore for OsRng { } } -impl TryCryptoRng for OsRng {} +impl TryCryptoRng for SysRng {} #[cfg(test)] mod test { @@ -61,14 +61,14 @@ mod test { #[test] fn test_os_rng() { - let x = OsRng.try_next_u64().unwrap(); - let y = OsRng.try_next_u64().unwrap(); + let x = SysRng.try_next_u64().unwrap(); + let y = SysRng.try_next_u64().unwrap(); assert!(x != 0); assert!(x != y); } #[test] fn test_construction() { - assert!(OsRng.try_next_u64().unwrap() != 0); + assert!(SysRng.try_next_u64().unwrap() != 0); } } From c87ed8094bec34ec5c15175b7bba709a61b70d0b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:14:12 +0000 Subject: [PATCH 10/19] Rename feature rng -> sys_rng --- Cargo.toml | 4 ++-- src/lib.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ae71ba9f..3037a445 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ categories = ["os", "no-std"] exclude = [".*"] [package.metadata.docs.rs] -features = ["std", "rng"] +features = ["std", "sys_rng"] [features] # Implement From for std::io::Error and @@ -27,7 +27,7 @@ std = [] wasm_js = ["dep:wasm-bindgen", "dep:js-sys"] # Provide SysRng over rand_core -rng = ["dep:rand_core"] +sys_rng = ["dep:rand_core"] [dependencies] cfg-if = "1" diff --git a/src/lib.rs b/src/lib.rs index 6e31fada..dd4275e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,11 +39,11 @@ mod util; #[cfg(feature = "std")] mod error_std_impls; -#[cfg(feature = "rng")] +#[cfg(feature = "sys_rng")] mod rng; -#[cfg(feature = "rng")] +#[cfg(feature = "sys_rng")] pub use rand_core; -#[cfg(feature = "rng")] +#[cfg(feature = "sys_rng")] pub use rng::SysRng; pub use crate::error::{Error, RawOsError}; From c304fa321768dba7630fe7cdfc17c3fcbf0885cf Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:15:55 +0000 Subject: [PATCH 11/19] Move SysRng tests to tests/ folder --- tests/rng.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/rng.rs diff --git a/tests/rng.rs b/tests/rng.rs new file mode 100644 index 00000000..d1cfeb74 --- /dev/null +++ b/tests/rng.rs @@ -0,0 +1,17 @@ +#![cfg(feature = "sys_rng")] + +use getrandom::SysRng; +use getrandom::rand_core::TryRngCore; + +#[test] +fn test_os_rng() { + let x = SysRng.try_next_u64().unwrap(); + let y = SysRng.try_next_u64().unwrap(); + assert!(x != 0); + assert!(x != y); +} + +#[test] +fn test_construction() { + assert!(SysRng.try_next_u64().unwrap() != 0); +} From fac8d4373112db0d5d9faffd927a977aa649a5a3 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:42:52 +0000 Subject: [PATCH 12/19] Remove redundant test --- src/rng.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/rng.rs b/src/rng.rs index bf210e3d..1cc6a678 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -54,21 +54,3 @@ impl TryRngCore for SysRng { } impl TryCryptoRng for SysRng {} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_os_rng() { - let x = SysRng.try_next_u64().unwrap(); - let y = SysRng.try_next_u64().unwrap(); - assert!(x != 0); - assert!(x != y); - } - - #[test] - fn test_construction() { - assert!(SysRng.try_next_u64().unwrap() != 0); - } -} From 9982171c6ef6843893499924eff50747fe68accb Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:44:58 +0000 Subject: [PATCH 13/19] Rename more things to sys_rng --- .github/workflows/tests.yml | 28 ++++++++++++++-------------- tests/rng.rs | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b24123a5..cb3766a9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,8 +39,8 @@ jobs: # Make sure enabling the std feature doesn't break anything - run: | cargo test --features=std - cargo test --features=rng - cargo test --features=std,rng + cargo test --features=sys_rng + cargo test --features=std,sys_rng - if: ${{ matrix.toolchain == 'nightly' }} run: cargo test --benches @@ -56,27 +56,27 @@ jobs: with: targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@v2 - - run: cargo test --target=${{ matrix.target }} --features=std,rng + - run: cargo test --target=${{ matrix.target }} --features=std,sys_rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo test --target=${{ matrix.target }} --features=std,rng + run: cargo test --target=${{ matrix.target }} --features=std,sys_rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw" - run: cargo test --target=${{ matrix.target }} --features=std,rng + run: cargo test --target=${{ matrix.target }} --features=std,sys_rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback - run: cargo test --features=std,rng + run: cargo test --features=std,sys_rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback - run: cargo test --features=std,rng + run: cargo test --features=std,sys_rng - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo test --features=std,rng + run: cargo test --features=std,sys_rng ios: name: iOS Simulator @@ -126,7 +126,7 @@ jobs: with: toolchain: ${{ matrix.toolchain }} - uses: Swatinem/rust-cache@v2 - - run: cargo test --features=std,rng + - run: cargo test --features=std,sys_rng windows7: name: Windows 7 (on Windows 10) @@ -139,8 +139,8 @@ jobs: toolchain: nightly-2025-09-28 components: rust-src - uses: Swatinem/rust-cache@v2 - - run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std,rng - - run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std,rng + - run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std,sys_rng + - run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std,sys_rng sanitizer-linux: name: Sanitizer Linux @@ -219,7 +219,7 @@ jobs: wget -O - $URL | tar -xz -C ~/.cargo/bin cross --version - name: Test - run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,rng + run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,sys_rng freebsd: name: FreeBSD VM @@ -276,14 +276,14 @@ jobs: description: Web, version: stable, flags: '-Dwarnings --cfg getrandom_backend="wasm_js"', - args: '--features=std,rng,wasm_js', + args: '--features=std,sys_rng,wasm_js', } - { description: Web with Atomics, version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std,rng,wasm_js -Zbuild-std=panic_abort,std', + args: '--features=std,sys_rng,wasm_js -Zbuild-std=panic_abort,std', } steps: - uses: actions/checkout@v5 diff --git a/tests/rng.rs b/tests/rng.rs index d1cfeb74..541912d2 100644 --- a/tests/rng.rs +++ b/tests/rng.rs @@ -4,7 +4,7 @@ use getrandom::SysRng; use getrandom::rand_core::TryRngCore; #[test] -fn test_os_rng() { +fn test_sys_rng() { let x = SysRng.try_next_u64().unwrap(); let y = SysRng.try_next_u64().unwrap(); assert!(x != 0); From 0ce588269ea39784bc500df616a895150b802469 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 13:49:25 +0000 Subject: [PATCH 14/19] Rename modules to sys_rng --- src/lib.rs | 4 ++-- src/{rng.rs => sys_rng.rs} | 0 tests/{rng.rs => sys_rng.rs} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{rng.rs => sys_rng.rs} (100%) rename tests/{rng.rs => sys_rng.rs} (100%) diff --git a/src/lib.rs b/src/lib.rs index dd4275e6..4c49c68d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,11 +40,11 @@ mod util; mod error_std_impls; #[cfg(feature = "sys_rng")] -mod rng; +mod sys_rng; #[cfg(feature = "sys_rng")] pub use rand_core; #[cfg(feature = "sys_rng")] -pub use rng::SysRng; +pub use sys_rng::SysRng; pub use crate::error::{Error, RawOsError}; diff --git a/src/rng.rs b/src/sys_rng.rs similarity index 100% rename from src/rng.rs rename to src/sys_rng.rs diff --git a/tests/rng.rs b/tests/sys_rng.rs similarity index 100% rename from tests/rng.rs rename to tests/sys_rng.rs From fac50458ba9464251b94113454eb10151a3fdea5 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 21 Nov 2025 15:34:22 +0000 Subject: [PATCH 15/19] Move sys_rng module descriptor --- src/lib.rs | 2 ++ src/sys_rng.rs | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c49c68d..ed69ad23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,10 @@ mod util; #[cfg(feature = "std")] mod error_std_impls; +/// `rand_core` adapter #[cfg(feature = "sys_rng")] mod sys_rng; + #[cfg(feature = "sys_rng")] pub use rand_core; #[cfg(feature = "sys_rng")] diff --git a/src/sys_rng.rs b/src/sys_rng.rs index 1cc6a678..ccfc702d 100644 --- a/src/sys_rng.rs +++ b/src/sys_rng.rs @@ -1,5 +1,3 @@ -//! rand_core adapter - use crate::Error; use rand_core::{TryCryptoRng, TryRngCore}; From 684a4bb2f0063302c16e2f0cb2fd78f0e15aca94 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 24 Nov 2025 07:16:15 +0000 Subject: [PATCH 16/19] Add type UnwrappedSysRng --- src/lib.rs | 2 +- src/sys_rng.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ed69ad23..27197443 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ mod sys_rng; #[cfg(feature = "sys_rng")] pub use rand_core; #[cfg(feature = "sys_rng")] -pub use sys_rng::SysRng; +pub use sys_rng::{SysRng, UnwrappedSysRng}; pub use crate::error::{Error, RawOsError}; diff --git a/src/sys_rng.rs b/src/sys_rng.rs index ccfc702d..fe647b9c 100644 --- a/src/sys_rng.rs +++ b/src/sys_rng.rs @@ -1,7 +1,7 @@ use crate::Error; use rand_core::{TryCryptoRng, TryRngCore}; -/// An RNG over the operating-system's random data source +/// A [`TryRngCore`] interface over the system's preferred random number source /// /// This is a zero-sized struct. It can be freely constructed with just `SysRng`. /// @@ -52,3 +52,10 @@ impl TryRngCore for SysRng { } impl TryCryptoRng for SysRng {} + +/// An [`RngCore`](rand_core::RngCore) interface over the system's preferred random number source +/// +/// # Panics +/// +/// This wrapper will panic on error (see [crate docs](crate#error-handling)). +pub type UnwrappedSysRng = rand_core::UnwrapErr; From 830a6737129572a00be6a657d7bbb7b7c718c0ff Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 24 Nov 2025 07:45:18 +0000 Subject: [PATCH 17/19] CHANGELOG entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cce685f8..d29812c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `RawOsError` type alias [#739] +- `SysRng` and `UnwrappedSysRng` behind new feature `sys_rng` [#751] ### Changed - Use Edition 2024 and MSRV 1.85 [#749] [#739]: https://github.com/rust-random/getrandom/pull/739 [#749]: https://github.com/rust-random/getrandom/pull/749 +[#751]: https://github.com/rust-random/getrandom/pull/751 ## [0.3.4] - 2025-10-14 From 8c1a7c5061a0c6e0cf8f142466f9b6546b009ded Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 24 Nov 2025 07:47:02 +0000 Subject: [PATCH 18/19] Test UnwrappedSysRng --- tests/sys_rng.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/sys_rng.rs b/tests/sys_rng.rs index 541912d2..214c7c8c 100644 --- a/tests/sys_rng.rs +++ b/tests/sys_rng.rs @@ -1,7 +1,7 @@ #![cfg(feature = "sys_rng")] -use getrandom::SysRng; -use getrandom::rand_core::TryRngCore; +use getrandom::rand_core::{RngCore, TryRngCore}; +use getrandom::{SysRng, UnwrappedSysRng}; #[test] fn test_sys_rng() { @@ -15,3 +15,10 @@ fn test_sys_rng() { fn test_construction() { assert!(SysRng.try_next_u64().unwrap() != 0); } + +#[test] +fn test_unwrapped_sys_rng() { + let mut buf = [0u8; 128]; + UnwrappedSysRng::default().fill_bytes(&mut buf); + assert!(buf != [0; 128]); +} From 8689ae56dd84ac484ed2ee8403d47ad17afc1e4f Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 24 Nov 2025 10:51:17 +0000 Subject: [PATCH 19/19] Remove UnwrappedSysRng --- CHANGELOG.md | 2 +- src/lib.rs | 2 +- src/sys_rng.rs | 7 ------- tests/sys_rng.rs | 11 ++--------- 4 files changed, 4 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d29812c8..28162ef4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `RawOsError` type alias [#739] -- `SysRng` and `UnwrappedSysRng` behind new feature `sys_rng` [#751] +- `SysRng` behind new feature `sys_rng` [#751] ### Changed - Use Edition 2024 and MSRV 1.85 [#749] diff --git a/src/lib.rs b/src/lib.rs index 27197443..ed69ad23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ mod sys_rng; #[cfg(feature = "sys_rng")] pub use rand_core; #[cfg(feature = "sys_rng")] -pub use sys_rng::{SysRng, UnwrappedSysRng}; +pub use sys_rng::SysRng; pub use crate::error::{Error, RawOsError}; diff --git a/src/sys_rng.rs b/src/sys_rng.rs index fe647b9c..d7c019e1 100644 --- a/src/sys_rng.rs +++ b/src/sys_rng.rs @@ -52,10 +52,3 @@ impl TryRngCore for SysRng { } impl TryCryptoRng for SysRng {} - -/// An [`RngCore`](rand_core::RngCore) interface over the system's preferred random number source -/// -/// # Panics -/// -/// This wrapper will panic on error (see [crate docs](crate#error-handling)). -pub type UnwrappedSysRng = rand_core::UnwrapErr; diff --git a/tests/sys_rng.rs b/tests/sys_rng.rs index 214c7c8c..541912d2 100644 --- a/tests/sys_rng.rs +++ b/tests/sys_rng.rs @@ -1,7 +1,7 @@ #![cfg(feature = "sys_rng")] -use getrandom::rand_core::{RngCore, TryRngCore}; -use getrandom::{SysRng, UnwrappedSysRng}; +use getrandom::SysRng; +use getrandom::rand_core::TryRngCore; #[test] fn test_sys_rng() { @@ -15,10 +15,3 @@ fn test_sys_rng() { fn test_construction() { assert!(SysRng.try_next_u64().unwrap() != 0); } - -#[test] -fn test_unwrapped_sys_rng() { - let mut buf = [0u8; 128]; - UnwrappedSysRng::default().fill_bytes(&mut buf); - assert!(buf != [0; 128]); -}