diff --git a/Cargo.toml b/Cargo.toml index f63baf53..202a6267 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/cap-rand/Cargo.toml b/cap-rand/Cargo.toml index 6de4c597..d43d76c2 100644 --- a/cap-rand/Cargo.toml +++ b/cap-rand/Cargo.toml @@ -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 = [] diff --git a/cap-rand/src/lib.rs b/cap-rand/src/lib.rs index 7d53b6e0..89c586f1 100644 --- a/cap-rand/src/lib.rs +++ b/cap-rand/src/lib.rs @@ -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}; @@ -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; @@ -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 { + 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 { + 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. @@ -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 {} @@ -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. @@ -194,7 +187,7 @@ pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng #[inline] pub fn random(ambient_authority: AmbientAuthority) -> T where - crate::distributions::Standard: crate::distributions::Distribution, + crate::distr::StandardUniform: crate::distr::Distribution, { let _ = ambient_authority; rand::random() diff --git a/tests/fs.rs b/tests/fs.rs index 3751120e..2009d475 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -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(); @@ -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(); diff --git a/tests/fs_utf8.rs b/tests/fs_utf8.rs index e17dd988..642569e2 100644 --- a/tests/fs_utf8.rs +++ b/tests/fs_utf8.rs @@ -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(); @@ -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(); diff --git a/tests/rand.rs b/tests/rand.rs index db00423a..b779e23f 100644 --- a/tests/rand.rs +++ b/tests/rand.rs @@ -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); }