Skip to content

Commit 6cbbcb4

Browse files
authored
Add SysRng behind new feature sys_rng (#751)
1 parent cd81c0b commit 6cbbcb4

File tree

7 files changed

+112
-16
lines changed

7 files changed

+112
-16
lines changed

.github/workflows/tests.yml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ jobs:
3737
- uses: Swatinem/rust-cache@v2
3838
- run: cargo test
3939
# Make sure enabling the std feature doesn't break anything
40-
- run: cargo test --features=std
40+
- run: |
41+
cargo test --features=std
42+
cargo test --features=sys_rng
43+
cargo test --features=std,sys_rng
4144
- if: ${{ matrix.toolchain == 'nightly' }}
4245
run: cargo test --benches
4346

@@ -53,27 +56,27 @@ jobs:
5356
with:
5457
targets: ${{ matrix.target }}
5558
- uses: Swatinem/rust-cache@v2
56-
- run: cargo test --target=${{ matrix.target }} --features=std
59+
- run: cargo test --target=${{ matrix.target }} --features=std,sys_rng
5760
- env:
5861
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
5962
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
60-
run: cargo test --target=${{ matrix.target }} --features=std
63+
run: cargo test --target=${{ matrix.target }} --features=std,sys_rng
6164
- env:
6265
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw"
6366
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="linux_raw"
64-
run: cargo test --target=${{ matrix.target }} --features=std
67+
run: cargo test --target=${{ matrix.target }} --features=std,sys_rng
6568
- env:
6669
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
6770
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
68-
run: cargo test --features=std
71+
run: cargo test --features=std,sys_rng
6972
- env:
7073
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback
7174
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback
72-
run: cargo test --features=std
75+
run: cargo test --features=std,sys_rng
7376
- env:
7477
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
7578
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
76-
run: cargo test --features=std
79+
run: cargo test --features=std,sys_rng
7780

7881
ios:
7982
name: iOS Simulator
@@ -123,7 +126,7 @@ jobs:
123126
with:
124127
toolchain: ${{ matrix.toolchain }}
125128
- uses: Swatinem/rust-cache@v2
126-
- run: cargo test --features=std
129+
- run: cargo test --features=std,sys_rng
127130

128131
windows7:
129132
name: Windows 7 (on Windows 10)
@@ -136,8 +139,8 @@ jobs:
136139
toolchain: nightly-2025-09-28
137140
components: rust-src
138141
- uses: Swatinem/rust-cache@v2
139-
- run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std
140-
- run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std
142+
- run: cargo test --target=x86_64-win7-windows-msvc -Z build-std --features=std,sys_rng
143+
- run: cargo test --target=i686-win7-windows-msvc -Z build-std --features=std,sys_rng
141144

142145
sanitizer-linux:
143146
name: Sanitizer Linux
@@ -216,7 +219,7 @@ jobs:
216219
wget -O - $URL | tar -xz -C ~/.cargo/bin
217220
cross --version
218221
- name: Test
219-
run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std
222+
run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,sys_rng
220223

221224
freebsd:
222225
name: FreeBSD VM
@@ -273,14 +276,14 @@ jobs:
273276
description: Web,
274277
version: stable,
275278
flags: '-Dwarnings --cfg getrandom_backend="wasm_js"',
276-
args: '--features=std,wasm_js',
279+
args: '--features=std,sys_rng,wasm_js',
277280
}
278281
- {
279282
description: Web with Atomics,
280283
version: nightly,
281284
components: rust-src,
282285
flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory',
283-
args: '--features=std,wasm_js -Zbuild-std=panic_abort,std',
286+
args: '--features=std,sys_rng,wasm_js -Zbuild-std=panic_abort,std',
284287
}
285288
steps:
286289
- uses: actions/checkout@v5

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- `RawOsError` type alias [#739]
11+
- `SysRng` behind new feature `sys_rng` [#751]
1112

1213
### Changed
1314
- Use Edition 2024 and MSRV 1.85 [#749]
1415

1516
[#739]: https://github.com/rust-random/getrandom/pull/739
1617
[#749]: https://github.com/rust-random/getrandom/pull/749
18+
[#751]: https://github.com/rust-random/getrandom/pull/751
1719

1820
## [0.3.4] - 2025-10-14
1921

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ repository = "https://github.com/rust-random/getrandom"
1111
categories = ["os", "no-std"]
1212
exclude = [".*"]
1313

14+
[package.metadata.docs.rs]
15+
features = ["std", "sys_rng"]
16+
1417
[features]
1518
# Implement From<getrandom::Error> for std::io::Error and
1619
# use std to retrieve OS error descriptions
@@ -23,8 +26,12 @@ std = []
2326
# i.e. avoid unconditionally enabling it in library crates.
2427
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]
2528

29+
# Provide SysRng over rand_core
30+
sys_rng = ["dep:rand_core"]
31+
2632
[dependencies]
2733
cfg-if = "1"
34+
rand_core = { version = "0.10.0-rc-2", optional = true }
2835

2936
# getrandom / linux_android_with_fallback
3037
[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]
@@ -85,9 +92,6 @@ check-cfg = [
8592
'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this.
8693
]
8794

88-
[package.metadata.docs.rs]
89-
features = ["std"]
90-
9195
# workaround for https://github.com/cross-rs/cross/issues/1345
9296
[package.metadata.cross.target.x86_64-unknown-netbsd]
9397
pre-build = [

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ mod util;
3939
#[cfg(feature = "std")]
4040
mod error_std_impls;
4141

42+
/// `rand_core` adapter
43+
#[cfg(feature = "sys_rng")]
44+
mod sys_rng;
45+
46+
#[cfg(feature = "sys_rng")]
47+
pub use rand_core;
48+
#[cfg(feature = "sys_rng")]
49+
pub use sys_rng::SysRng;
50+
4251
pub use crate::error::{Error, RawOsError};
4352

4453
/// Fill `dest` with random bytes from the system's preferred random number source.

src/sys_rng.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::Error;
2+
use rand_core::{TryCryptoRng, TryRngCore};
3+
4+
/// A [`TryRngCore`] interface over the system's preferred random number source
5+
///
6+
/// This is a zero-sized struct. It can be freely constructed with just `SysRng`.
7+
///
8+
/// This struct is also available as [`rand::rngs::SysRng`] when using [rand].
9+
///
10+
/// # Usage example
11+
///
12+
/// `SysRng` implements [`TryRngCore`]:
13+
/// ```
14+
/// use getrandom::{rand_core::TryRngCore, SysRng};
15+
///
16+
/// let mut key = [0u8; 32];
17+
/// SysRng.try_fill_bytes(&mut key).unwrap();
18+
/// ```
19+
///
20+
/// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]:
21+
/// ```
22+
/// use getrandom::rand_core::{TryRngCore, RngCore};
23+
/// use getrandom::SysRng;
24+
///
25+
/// let mut rng = SysRng.unwrap_err();
26+
/// let random_u64 = rng.next_u64();
27+
/// ```
28+
///
29+
/// [rand]: https://crates.io/crates/rand
30+
/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html
31+
/// [`RngCore`]: rand_core::RngCore
32+
#[derive(Clone, Copy, Debug, Default)]
33+
pub struct SysRng;
34+
35+
impl TryRngCore for SysRng {
36+
type Error = Error;
37+
38+
#[inline]
39+
fn try_next_u32(&mut self) -> Result<u32, Error> {
40+
crate::u32()
41+
}
42+
43+
#[inline]
44+
fn try_next_u64(&mut self) -> Result<u64, Error> {
45+
crate::u64()
46+
}
47+
48+
#[inline]
49+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
50+
crate::fill(dest)
51+
}
52+
}
53+
54+
impl TryCryptoRng for SysRng {}

tests/sys_rng.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![cfg(feature = "sys_rng")]
2+
3+
use getrandom::SysRng;
4+
use getrandom::rand_core::TryRngCore;
5+
6+
#[test]
7+
fn test_sys_rng() {
8+
let x = SysRng.try_next_u64().unwrap();
9+
let y = SysRng.try_next_u64().unwrap();
10+
assert!(x != 0);
11+
assert!(x != y);
12+
}
13+
14+
#[test]
15+
fn test_construction() {
16+
assert!(SysRng.try_next_u64().unwrap() != 0);
17+
}

0 commit comments

Comments
 (0)