Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cap-directories = { path = "cap-directories", version = "3.4.4" }
cap-std = { path = "cap-std", version = "3.4.4" }
cap-tempfile = { path = "cap-tempfile", version = "3.4.4" }
cap-rand = { path = "cap-rand", version = "3.4.4" }
rand = "0.8.1"
rand = "0.9.2"
tempfile = "3.1.0"
camino = "1.0.5"
libc = "0.2.100"
Expand Down
3 changes: 2 additions & 1 deletion cap-rand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ edition = "2021"

[dependencies]
ambient-authority = "0.0.2"
rand = "0.8.1"
rand = "0.9.2"
rand_distr = "0.5.1"

[features]
default = []
Expand Down
45 changes: 19 additions & 26 deletions cap-rand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
#[doc(hidden)]
pub use ambient_authority::ambient_authority_known_at_compile_time;
pub use ambient_authority::{ambient_authority, AmbientAuthority};
pub use rand::{distributions, seq, CryptoRng, Error, Fill, Rng, RngCore, SeedableRng};
pub use rand::{
distr, rand_core, rand_core::OsError as Error, seq, CryptoRng, Fill, Rng, RngCore, SeedableRng,
TryCryptoRng, TryRngCore,
};

/// Convenience re-export of common members.
///
/// This corresponds to [`rand::prelude`].
pub mod prelude {
pub use crate::distributions::Distribution;
pub use crate::distr::Distribution;
#[cfg(feature = "small_rng")]
pub use crate::rngs::SmallRng;
pub use crate::rngs::{CapRng, StdRng};
Expand All @@ -52,7 +55,7 @@ pub mod prelude {
pub mod rngs {
use super::AmbientAuthority;

pub use rand::rngs::{adapter, mock, StdRng};
pub use rand::rngs::StdRng;

#[cfg(feature = "small_rng")]
pub use rand::rngs::SmallRng;
Expand Down Expand Up @@ -80,29 +83,26 @@ pub mod rngs {
}
}

impl crate::RngCore for OsRng {
#[inline]
fn next_u32(&mut self) -> u32 {
rand::rngs::OsRng.next_u32()
}
impl crate::TryRngCore for OsRng {
type Error = crate::rand_core::OsError;

#[inline]
fn next_u64(&mut self) -> u64 {
rand::rngs::OsRng.next_u64()
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
rand::rngs::OsRng.try_next_u32()
}

#[inline]
fn fill_bytes(&mut self, bytes: &mut [u8]) {
rand::rngs::OsRng.fill_bytes(bytes)
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
rand::rngs::OsRng.try_next_u64()
}

#[inline]
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), crate::Error> {
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
rand::rngs::OsRng.try_fill_bytes(bytes)
}
}

impl crate::CryptoRng for OsRng {}
impl crate::TryCryptoRng for OsRng {}

/// The type returned by `thread_rng`, essentially just a reference to a
/// PRNG in memory.
Expand Down Expand Up @@ -142,11 +142,6 @@ pub mod rngs {
fn fill_bytes(&mut self, bytes: &mut [u8]) {
self.inner.fill_bytes(bytes)
}

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

impl crate::CryptoRng for CapRng {}
Expand All @@ -164,23 +159,21 @@ pub mod rngs {
#[inline]
pub fn thread_rng(ambient_authority: AmbientAuthority) -> rngs::CapRng {
let _ = ambient_authority;
rngs::CapRng {
inner: rand::thread_rng(),
}
rngs::CapRng { inner: rand::rng() }
}

/// Retrieve the standard random number generator, seeded by the system.
///
/// This corresponds to [`rand::rngs::StdRng::from_entropy`].
/// This corresponds to [`rand::rngs::StdRng::from_os_rng`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the platform entropy
/// source.
#[inline]
pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng {
pub fn std_rng_from_os_rng(ambient_authority: AmbientAuthority) -> rngs::StdRng {
let _ = ambient_authority;
rand::rngs::StdRng::from_entropy()
rand::rngs::StdRng::from_os_rng()
}

/// Generates a random value using the thread-local random number generator.
Expand All @@ -194,7 +187,7 @@ pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng
#[inline]
pub fn random<T>(ambient_authority: AmbientAuthority) -> T
where
crate::distributions::Standard: crate::distributions::Distribution<T>,
crate::distr::StandardUniform: crate::distr::Distribution<T>,
{
let _ = ambient_authority;
rand::random()
Expand Down
4 changes: 2 additions & 2 deletions tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ fn _assert_send_sync() {
#[test]
fn binary_file() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
StdRng::from_os_rng().fill_bytes(&mut bytes);

let tmpdir = tmpdir();

Expand All @@ -1301,7 +1301,7 @@ fn binary_file() {
#[test]
fn write_then_read() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
StdRng::from_os_rng().fill_bytes(&mut bytes);

let tmpdir = tmpdir();

Expand Down
4 changes: 2 additions & 2 deletions tests/fs_utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ fn _assert_send_sync() {
#[test]
fn binary_file() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
StdRng::from_os_rng().fill_bytes(&mut bytes);

let tmpdir = tmpdir();

Expand All @@ -1298,7 +1298,7 @@ fn binary_file() {
#[test]
fn write_then_read() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
StdRng::from_os_rng().fill_bytes(&mut bytes);

let tmpdir = tmpdir();

Expand Down
2 changes: 1 addition & 1 deletion tests/rand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[test]
fn test_std_rng_from_entropy() {
let rng = cap_rand::std_rng_from_entropy(cap_rand::ambient_authority());
let rng = cap_rand::std_rng_from_os_rng(cap_rand::ambient_authority());
assert_eq!(rng.clone(), rng);
}
Loading