Skip to content

Commit 2f15222

Browse files
committed
chore: bump to rand v0.9
Pretty untested, not sure the APIs are what we need either, just playing around with this for a separate project.
1 parent 54716a1 commit 2f15222

File tree

5 files changed

+25
-32
lines changed

5 files changed

+25
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cap-directories = { path = "cap-directories", version = "3.4.4" }
2626
cap-std = { path = "cap-std", version = "3.4.4" }
2727
cap-tempfile = { path = "cap-tempfile", version = "3.4.4" }
2828
cap-rand = { path = "cap-rand", version = "3.4.4" }
29-
rand = "0.8.1"
29+
rand = "0.9.2"
3030
tempfile = "3.1.0"
3131
camino = "1.0.5"
3232
libc = "0.2.100"

cap-rand/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ edition = "2021"
1414

1515
[dependencies]
1616
ambient-authority = "0.0.2"
17-
rand = "0.8.1"
17+
rand = "0.9.2"
18+
rand_distr = "0.5.1"
1819

1920
[features]
2021
default = []

cap-rand/src/lib.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
#[doc(hidden)]
3333
pub use ambient_authority::ambient_authority_known_at_compile_time;
3434
pub use ambient_authority::{ambient_authority, AmbientAuthority};
35-
pub use rand::{distributions, seq, CryptoRng, Error, Fill, Rng, RngCore, SeedableRng};
35+
pub use rand::{
36+
distr, rand_core, seq, CryptoRng, Fill, Rng, RngCore, SeedableRng, TryCryptoRng, TryRngCore,
37+
};
3638

3739
/// Convenience re-export of common members.
3840
///
3941
/// This corresponds to [`rand::prelude`].
4042
pub mod prelude {
41-
pub use crate::distributions::Distribution;
43+
pub use crate::distr::Distribution;
4244
#[cfg(feature = "small_rng")]
4345
pub use crate::rngs::SmallRng;
4446
pub use crate::rngs::{CapRng, StdRng};
@@ -52,7 +54,7 @@ pub mod prelude {
5254
pub mod rngs {
5355
use super::AmbientAuthority;
5456

55-
pub use rand::rngs::{adapter, mock, StdRng};
57+
pub use rand::rngs::StdRng;
5658

5759
#[cfg(feature = "small_rng")]
5860
pub use rand::rngs::SmallRng;
@@ -80,29 +82,26 @@ pub mod rngs {
8082
}
8183
}
8284

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

8988
#[inline]
90-
fn next_u64(&mut self) -> u64 {
91-
rand::rngs::OsRng.next_u64()
89+
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
90+
rand::rngs::OsRng::default().try_next_u32()
9291
}
9392

9493
#[inline]
95-
fn fill_bytes(&mut self, bytes: &mut [u8]) {
96-
rand::rngs::OsRng.fill_bytes(bytes)
94+
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
95+
rand::rngs::OsRng::default().try_next_u64()
9796
}
9897

9998
#[inline]
100-
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), crate::Error> {
101-
rand::rngs::OsRng.try_fill_bytes(bytes)
99+
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
100+
rand::rngs::OsRng::default().try_fill_bytes(bytes)
102101
}
103102
}
104103

105-
impl crate::CryptoRng for OsRng {}
104+
impl crate::TryCryptoRng for OsRng {}
106105

107106
/// The type returned by `thread_rng`, essentially just a reference to a
108107
/// PRNG in memory.
@@ -142,11 +141,6 @@ pub mod rngs {
142141
fn fill_bytes(&mut self, bytes: &mut [u8]) {
143142
self.inner.fill_bytes(bytes)
144143
}
145-
146-
#[inline]
147-
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), crate::Error> {
148-
self.inner.try_fill_bytes(bytes)
149-
}
150144
}
151145

152146
impl crate::CryptoRng for CapRng {}
@@ -164,23 +158,21 @@ pub mod rngs {
164158
#[inline]
165159
pub fn thread_rng(ambient_authority: AmbientAuthority) -> rngs::CapRng {
166160
let _ = ambient_authority;
167-
rngs::CapRng {
168-
inner: rand::thread_rng(),
169-
}
161+
rngs::CapRng { inner: rand::rng() }
170162
}
171163

172164
/// Retrieve the standard random number generator, seeded by the system.
173165
///
174-
/// This corresponds to [`rand::rngs::StdRng::from_entropy`].
166+
/// This corresponds to [`rand::rngs::StdRng::from_os_rng`].
175167
///
176168
/// # Ambient Authority
177169
///
178170
/// This function makes use of ambient authority to access the platform entropy
179171
/// source.
180172
#[inline]
181-
pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng {
173+
pub fn std_rng_from_os_rng(ambient_authority: AmbientAuthority) -> rngs::StdRng {
182174
let _ = ambient_authority;
183-
rand::rngs::StdRng::from_entropy()
175+
rand::rngs::StdRng::from_os_rng()
184176
}
185177

186178
/// Generates a random value using the thread-local random number generator.
@@ -194,7 +186,7 @@ pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng
194186
#[inline]
195187
pub fn random<T>(ambient_authority: AmbientAuthority) -> T
196188
where
197-
crate::distributions::Standard: crate::distributions::Distribution<T>,
189+
crate::distr::StandardUniform: crate::distr::Distribution<T>,
198190
{
199191
let _ = ambient_authority;
200192
rand::random()

tests/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ fn _assert_send_sync() {
12881288
#[test]
12891289
fn binary_file() {
12901290
let mut bytes = [0; 1024];
1291-
StdRng::from_entropy().fill_bytes(&mut bytes);
1291+
StdRng::from_os_rng().fill_bytes(&mut bytes);
12921292

12931293
let tmpdir = tmpdir();
12941294

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

13061306
let tmpdir = tmpdir();
13071307

tests/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[test]
22
fn test_std_rng_from_entropy() {
3-
let rng = cap_rand::std_rng_from_entropy(cap_rand::ambient_authority());
3+
let rng = cap_rand::std_rng_from_os_rng(cap_rand::ambient_authority());
44
assert_eq!(rng.clone(), rng);
55
}

0 commit comments

Comments
 (0)