Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ pre-build = [
"rm base.tar.xz",
"rm -rf /tmp/netbsd",
]

[patch.crates-io]
rand_core = { git = "https://github.com/rust-random/rand_core.git", branch = "merge_rng_traits" }
20 changes: 10 additions & 10 deletions src/sys_rng.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
use crate::Error;
use rand_core::{TryCryptoRng, TryRngCore};
use rand_core::{CryptoRng, TryRng};

/// A [`TryRngCore`] interface over the system's preferred random number source
/// A [`TryRng`] interface over the system's preferred random number 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`]:
/// `SysRng` implements [`TryRng`]:
/// ```
/// use getrandom::{rand_core::TryRngCore, SysRng};
/// use getrandom::{rand_core::TryRng, SysRng};
///
/// let mut key = [0u8; 32];
/// SysRng.try_fill_bytes(&mut key).unwrap();
/// ```
///
/// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]:
/// Using it as an [`InfallibleRng`] is possible using [`TryRng::unwrap_err`]:
/// ```
/// use getrandom::rand_core::{TryRngCore, RngCore};
/// use getrandom::rand_core::TryRng;
/// use getrandom::SysRng;
///
/// let mut rng = SysRng.unwrap_err();
/// let random_u64 = rng.next_u64();
/// let random_u64 = rng.try_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
/// [`TryRng`]: rand_core::TryRng
#[derive(Clone, Copy, Debug, Default)]
pub struct SysRng;

impl TryRngCore for SysRng {
impl TryRng for SysRng {
type Error = Error;

#[inline]
Expand All @@ -51,4 +51,4 @@ impl TryRngCore for SysRng {
}
}

impl TryCryptoRng for SysRng {}
impl CryptoRng for SysRng {}
2 changes: 1 addition & 1 deletion tests/sys_rng.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "sys_rng")]

use getrandom::SysRng;
use getrandom::rand_core::TryRngCore;
use getrandom::rand_core::TryRng;

#[test]
fn test_sys_rng() {
Expand Down