Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- run: |
cargo test --features=std
cargo test --features=rng
cargo test --features=std,rng
- if: ${{ matrix.toolchain == 'nightly' }}
run: cargo test --benches

Expand All @@ -53,27 +56,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
Expand Down Expand Up @@ -123,7 +126,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)
Expand All @@ -136,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
- 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
Expand Down Expand Up @@ -216,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
run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,rng

freebsd:
name: FreeBSD VM
Expand Down Expand Up @@ -273,14 +276,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
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ repository = "https://github.com/rust-random/getrandom"
categories = ["os", "no-std"]
exclude = [".*"]

[package.metadata.docs.rs]
features = ["std", "sys_rng"]

[features]
# Implement From<getrandom::Error> for std::io::Error and
# use std to retrieve OS error descriptions
Expand All @@ -23,8 +26,12 @@ std = []
# i.e. avoid unconditionally enabling it in library crates.
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]

# Provide SysRng over rand_core
sys_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]
Expand Down Expand Up @@ -85,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 = [
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ mod util;
#[cfg(feature = "std")]
mod error_std_impls;

#[cfg(feature = "sys_rng")]
mod rng;
#[cfg(feature = "sys_rng")]
pub use rand_core;
#[cfg(feature = "sys_rng")]
pub use rng::SysRng;

pub use crate::error::{Error, RawOsError};

/// Fill `dest` with random bytes from the system's preferred random number source.
Expand Down
74 changes: 74 additions & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! 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 `SysRng`.
///
/// This struct is also available as [`rand::rngs::SysRng`] when using [rand].
///
/// # Usage example
///
/// `SysRng` implements [`TryRngCore`]:
/// ```
/// use getrandom::{rand_core::TryRngCore, SysRng};
///
/// let mut key = [0u8; 32];
/// 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::SysRng;
///
/// let mut rng = SysRng.unwrap_err();
/// let random_u64 = rng.next_u64();
/// ```
///
/// [rand]: https://crates.io/crates/rand
/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html
/// [`RngCore`]: rand_core::RngCore
#[derive(Clone, Copy, Debug, Default)]
pub struct SysRng;

impl TryRngCore for SysRng {
type Error = Error;

#[inline]
fn try_next_u32(&mut self) -> Result<u32, Error> {
crate::u32()
}

#[inline]
fn try_next_u64(&mut self) -> Result<u64, Error> {
crate::u64()
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
crate::fill(dest)
}
}

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);
}
}
17 changes: 17 additions & 0 deletions tests/rng.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading