Skip to content

Commit 83061f6

Browse files
authored
Update to rand master (#53)
Temporarily, we patch this repo to use rand from git. Not ideal, but: - It lets us get this code (mostly) ready for the next rand version now - Lets us test the rand_core changes - Allows us to move rand_pcg here, if we want to (possibly not; it is used by benches in the rand repo)
1 parent d09642c commit 83061f6

32 files changed

+233
-238
lines changed

.github/workflows/benches.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ jobs:
1818
with:
1919
toolchain: nightly
2020
components: clippy, rustfmt
21+
- name: Build
22+
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
2123
- name: Rustfmt
2224
run: cargo fmt -- --check
2325
- name: Clippy
2426
run: cargo clippy --all-targets -- -D warnings
25-
- name: Build
26-
run: RUSTFLAGS=-Dwarnings cargo build --all-targets

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ exclude = [
1010
"benches",
1111
]
1212
resolver = "2"
13+
14+
[patch.crates-io.rand_core]
15+
git = "https://github.com/rust-random/rand.git"
16+
branch = "master"

benches/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ publish = false
88
[dev-dependencies]
99
criterion = "0.5.0"
1010
criterion-cycles-per-byte = "0.6"
11-
rand_core = { version = "0.6.4", features = ["getrandom"] }
11+
rand_core = { version = "=0.9.0-alpha.1", features = ["getrandom"] }
1212
rand_xoshiro = { path = "../rand_xoshiro", version = "0.6" }
1313
rand_isaac = { path = "../rand_isaac", version = "0.3" }
1414
rand_xorshift = { path = "../rand_xorshift", version = "0.3" }
@@ -18,3 +18,7 @@ rand_hc = { path = "../rand_hc", version = "0.3" }
1818
name = "mod"
1919
path = "mod.rs"
2020
harness = false
21+
22+
[patch.crates-io.rand_core]
23+
git = "https://github.com/rust-random/rand.git"
24+
branch = "master"

benches/mod.rs

+47-47
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ fn gen_bytes(c: &mut Criterion<CyclesPerByte>) {
4949
};
5050
}
5151

52-
gen_bytes!("xorshift", XorShiftRng::from_entropy());
53-
gen_bytes!("xoshiro256starstar", Xoshiro256StarStar::from_entropy());
54-
gen_bytes!("xoshiro256plus", Xoshiro256Plus::from_entropy());
55-
gen_bytes!("xoshiro256plusplus", Xoshiro256PlusPlus::from_entropy());
56-
gen_bytes!("xoshiro128starstar", Xoshiro128StarStar::from_entropy());
57-
gen_bytes!("xoshiro128plus", Xoshiro128Plus::from_entropy());
58-
gen_bytes!("xoshiro128plusplus", Xoshiro128PlusPlus::from_entropy());
59-
gen_bytes!("xoroshiro128starstar", Xoroshiro128StarStar::from_entropy());
60-
gen_bytes!("xoroshiro128plus", Xoroshiro128Plus::from_entropy());
61-
gen_bytes!("xoroshiro64starstar", Xoroshiro64StarStar::from_entropy());
62-
gen_bytes!("xoroshiro64star", Xoroshiro64Star::from_entropy());
63-
gen_bytes!("splitmix64", SplitMix64::from_entropy());
64-
gen_bytes!("hc128", Hc128Rng::from_entropy());
65-
gen_bytes!("isaac", IsaacRng::from_entropy());
66-
gen_bytes!("isaac64", Isaac64Rng::from_entropy());
52+
gen_bytes!("xorshift", XorShiftRng::from_os_rng());
53+
gen_bytes!("xoshiro256starstar", Xoshiro256StarStar::from_os_rng());
54+
gen_bytes!("xoshiro256plus", Xoshiro256Plus::from_os_rng());
55+
gen_bytes!("xoshiro256plusplus", Xoshiro256PlusPlus::from_os_rng());
56+
gen_bytes!("xoshiro128starstar", Xoshiro128StarStar::from_os_rng());
57+
gen_bytes!("xoshiro128plus", Xoshiro128Plus::from_os_rng());
58+
gen_bytes!("xoshiro128plusplus", Xoshiro128PlusPlus::from_os_rng());
59+
gen_bytes!("xoroshiro128starstar", Xoroshiro128StarStar::from_os_rng());
60+
gen_bytes!("xoroshiro128plus", Xoroshiro128Plus::from_os_rng());
61+
gen_bytes!("xoroshiro64starstar", Xoroshiro64StarStar::from_os_rng());
62+
gen_bytes!("xoroshiro64star", Xoroshiro64Star::from_os_rng());
63+
gen_bytes!("splitmix64", SplitMix64::from_os_rng());
64+
gen_bytes!("hc128", Hc128Rng::from_os_rng());
65+
gen_bytes!("isaac", IsaacRng::from_os_rng());
66+
gen_bytes!("isaac64", Isaac64Rng::from_os_rng());
6767
}
6868

6969
// Save a dependency on Rand:
@@ -105,102 +105,102 @@ fn gen_uint(c: &mut Criterion<CyclesPerByte>) {
105105
let mut g = c.benchmark_group("gen_u32");
106106
g.throughput(Throughput::Bytes(size_of::<u32>() as u64 * RAND_BENCH_N));
107107

108-
gen_uint!(g, "xorshift", u32, XorShiftRng::from_entropy());
108+
gen_uint!(g, "xorshift", u32, XorShiftRng::from_os_rng());
109109
gen_uint!(
110110
g,
111111
"xoshiro256starstar",
112112
u32,
113-
Xoshiro256StarStar::from_entropy()
113+
Xoshiro256StarStar::from_os_rng()
114114
);
115-
gen_uint!(g, "xoshiro256plus", u32, Xoshiro256Plus::from_entropy());
115+
gen_uint!(g, "xoshiro256plus", u32, Xoshiro256Plus::from_os_rng());
116116
gen_uint!(
117117
g,
118118
"xoshiro256plusplus",
119119
u32,
120-
Xoshiro256PlusPlus::from_entropy()
120+
Xoshiro256PlusPlus::from_os_rng()
121121
);
122122
gen_uint!(
123123
g,
124124
"xoshiro128starstar",
125125
u32,
126-
Xoshiro128StarStar::from_entropy()
126+
Xoshiro128StarStar::from_os_rng()
127127
);
128-
gen_uint!(g, "xoshiro128plus", u32, Xoshiro128Plus::from_entropy());
128+
gen_uint!(g, "xoshiro128plus", u32, Xoshiro128Plus::from_os_rng());
129129
gen_uint!(
130130
g,
131131
"xoshiro128plusplus",
132132
u32,
133-
Xoshiro128PlusPlus::from_entropy()
133+
Xoshiro128PlusPlus::from_os_rng()
134134
);
135135
gen_uint!(
136136
g,
137137
"xoroshiro128starstar",
138138
u32,
139-
Xoroshiro128StarStar::from_entropy()
139+
Xoroshiro128StarStar::from_os_rng()
140140
);
141-
gen_uint!(g, "xoroshiro128plus", u32, Xoroshiro128Plus::from_entropy());
141+
gen_uint!(g, "xoroshiro128plus", u32, Xoroshiro128Plus::from_os_rng());
142142
gen_uint!(
143143
g,
144144
"xoroshiro64starstar",
145145
u32,
146-
Xoroshiro64StarStar::from_entropy()
146+
Xoroshiro64StarStar::from_os_rng()
147147
);
148-
gen_uint!(g, "xoroshiro64star", u32, Xoroshiro64Star::from_entropy());
149-
gen_uint!(g, "splitmix64", u32, SplitMix64::from_entropy());
150-
gen_uint!(g, "hc128", u32, Hc128Rng::from_entropy());
151-
gen_uint!(g, "isaac", u32, IsaacRng::from_entropy());
152-
gen_uint!(g, "isaac64", u32, Isaac64Rng::from_entropy());
148+
gen_uint!(g, "xoroshiro64star", u32, Xoroshiro64Star::from_os_rng());
149+
gen_uint!(g, "splitmix64", u32, SplitMix64::from_os_rng());
150+
gen_uint!(g, "hc128", u32, Hc128Rng::from_os_rng());
151+
gen_uint!(g, "isaac", u32, IsaacRng::from_os_rng());
152+
gen_uint!(g, "isaac64", u32, Isaac64Rng::from_os_rng());
153153
}
154154

155155
{
156156
let mut g = c.benchmark_group("gen_u64");
157157
g.throughput(Throughput::Bytes(size_of::<u64>() as u64 * RAND_BENCH_N));
158158

159-
gen_uint!(g, "xorshift", u64, XorShiftRng::from_entropy());
159+
gen_uint!(g, "xorshift", u64, XorShiftRng::from_os_rng());
160160
gen_uint!(
161161
g,
162162
"xoshiro256starstar",
163163
u64,
164-
Xoshiro256StarStar::from_entropy()
164+
Xoshiro256StarStar::from_os_rng()
165165
);
166-
gen_uint!(g, "xoshiro256plus", u64, Xoshiro256Plus::from_entropy());
166+
gen_uint!(g, "xoshiro256plus", u64, Xoshiro256Plus::from_os_rng());
167167
gen_uint!(
168168
g,
169169
"xoshiro256plusplus",
170170
u64,
171-
Xoshiro256PlusPlus::from_entropy()
171+
Xoshiro256PlusPlus::from_os_rng()
172172
);
173173
gen_uint!(
174174
g,
175175
"xoshiro128starstar",
176176
u64,
177-
Xoshiro128StarStar::from_entropy()
177+
Xoshiro128StarStar::from_os_rng()
178178
);
179-
gen_uint!(g, "xoshiro128plus", u64, Xoshiro128Plus::from_entropy());
179+
gen_uint!(g, "xoshiro128plus", u64, Xoshiro128Plus::from_os_rng());
180180
gen_uint!(
181181
g,
182182
"xoshiro128plusplus",
183183
u64,
184-
Xoshiro128PlusPlus::from_entropy()
184+
Xoshiro128PlusPlus::from_os_rng()
185185
);
186186
gen_uint!(
187187
g,
188188
"xoroshiro128starstar",
189189
u64,
190-
Xoroshiro128StarStar::from_entropy()
190+
Xoroshiro128StarStar::from_os_rng()
191191
);
192-
gen_uint!(g, "xoroshiro128plus", u64, Xoroshiro128Plus::from_entropy());
192+
gen_uint!(g, "xoroshiro128plus", u64, Xoroshiro128Plus::from_os_rng());
193193
gen_uint!(
194194
g,
195195
"xoroshiro64starstar",
196196
u64,
197-
Xoroshiro64StarStar::from_entropy()
197+
Xoroshiro64StarStar::from_os_rng()
198198
);
199-
gen_uint!(g, "xoroshiro64star", u64, Xoroshiro64Star::from_entropy());
200-
gen_uint!(g, "splitmix64", u64, SplitMix64::from_entropy());
201-
gen_uint!(g, "hc128", u64, Hc128Rng::from_entropy());
202-
gen_uint!(g, "isaac", u64, IsaacRng::from_entropy());
203-
gen_uint!(g, "isaac64", u64, Isaac64Rng::from_entropy());
199+
gen_uint!(g, "xoroshiro64star", u64, Xoroshiro64Star::from_os_rng());
200+
gen_uint!(g, "splitmix64", u64, SplitMix64::from_os_rng());
201+
gen_uint!(g, "hc128", u64, Hc128Rng::from_os_rng());
202+
gen_uint!(g, "isaac", u64, IsaacRng::from_os_rng());
203+
gen_uint!(g, "isaac64", u64, Isaac64Rng::from_os_rng());
204204
}
205205
}
206206

@@ -210,8 +210,8 @@ fn init(c: &mut Criterion) {
210210
macro_rules! init_gen {
211211
($fnn:expr, $gen:ident) => {
212212
g.bench_function($fnn, |b| {
213-
let mut rng = XorShiftRng::from_entropy();
214-
b.iter(|| $gen::from_rng(black_box(&mut rng)).unwrap())
213+
let mut rng = XorShiftRng::from_os_rng();
214+
b.iter(|| $gen::from_rng(black_box(&mut rng)))
215215
});
216216
};
217217
}

rand_hc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ edition = "2021"
1616
rust-version = "1.61"
1717

1818
[dependencies]
19-
rand_core = "0.6"
19+
rand_core = "=0.9.0-alpha.1"

rand_hc/src/hc128.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
//! The HC-128 random number generator.
1616
1717
use core::fmt;
18-
use rand_core::block::{BlockRng, BlockRngCore};
19-
use rand_core::{le, CryptoRng, Error, RngCore, SeedableRng};
18+
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
19+
use rand_core::{le, CryptoRng, RngCore, SeedableRng, TryRngCore};
2020

2121
const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
2222

@@ -87,13 +87,10 @@ impl RngCore for Hc128Rng {
8787
fn fill_bytes(&mut self, dest: &mut [u8]) {
8888
self.0.fill_bytes(dest)
8989
}
90-
91-
#[inline]
92-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
93-
self.0.try_fill_bytes(dest)
94-
}
9590
}
9691

92+
rand_core::impl_try_crypto_rng_from_crypto_rng!(Hc128Rng);
93+
9794
impl SeedableRng for Hc128Rng {
9895
type Seed = <Hc128Core as SeedableRng>::Seed;
9996

@@ -103,8 +100,13 @@ impl SeedableRng for Hc128Rng {
103100
}
104101

105102
#[inline]
106-
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
107-
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
103+
fn from_rng(rng: impl RngCore) -> Self {
104+
Hc128Rng(BlockRng::<Hc128Core>::from_rng(rng))
105+
}
106+
107+
#[inline]
108+
fn try_from_rng<R: TryRngCore>(rng: R) -> Result<Self, R::Error> {
109+
BlockRng::<Hc128Core>::try_from_rng(rng).map(Hc128Rng)
108110
}
109111
}
110112

@@ -352,7 +354,7 @@ impl SeedableRng for Hc128Core {
352354
}
353355
}
354356

355-
impl CryptoRng for Hc128Core {}
357+
impl CryptoBlockRng for Hc128Core {}
356358

357359
// Custom PartialEq implementation as it can't currently be derived from an array of size 1024
358360
impl PartialEq for Hc128Core {

rand_isaac/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rust-version = "1.61"
1919
serde1 = ["serde", "rand_core/serde1"]
2020

2121
[dependencies]
22-
rand_core = { version = "0.6" }
22+
rand_core = { version = "=0.9.0-alpha.1" }
2323
serde = { version = "1.0.103", features = ["derive"], optional = true }
2424
# Not a direct dependency but required to boost the minimum version:
2525
serde_derive = { version = "1.0.103", optional = true }

rand_isaac/src/isaac.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::isaac_array::IsaacArray;
1313
use core::num::Wrapping as w;
1414
use core::{fmt, slice};
1515
use rand_core::block::{BlockRng, BlockRngCore};
16-
use rand_core::{le, Error, RngCore, SeedableRng};
16+
use rand_core::{le, RngCore, SeedableRng, TryRngCore};
1717
#[cfg(feature = "serde1")]
1818
use serde::{Deserialize, Serialize};
1919

@@ -108,13 +108,10 @@ impl RngCore for IsaacRng {
108108
fn fill_bytes(&mut self, dest: &mut [u8]) {
109109
self.0.fill_bytes(dest)
110110
}
111-
112-
#[inline]
113-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
114-
self.0.try_fill_bytes(dest)
115-
}
116111
}
117112

113+
rand_core::impl_try_rng_from_rng_core!(IsaacRng);
114+
118115
impl SeedableRng for IsaacRng {
119116
type Seed = <IsaacCore as SeedableRng>::Seed;
120117

@@ -132,8 +129,13 @@ impl SeedableRng for IsaacRng {
132129
}
133130

134131
#[inline]
135-
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
136-
BlockRng::<IsaacCore>::from_rng(rng).map(IsaacRng)
132+
fn from_rng(rng: impl RngCore) -> Self {
133+
IsaacRng(BlockRng::<IsaacCore>::from_rng(rng))
134+
}
135+
136+
#[inline]
137+
fn try_from_rng<S: TryRngCore>(rng: S) -> Result<Self, S::Error> {
138+
BlockRng::<IsaacCore>::try_from_rng(rng).map(IsaacRng)
137139
}
138140
}
139141

@@ -365,7 +367,24 @@ impl SeedableRng for IsaacCore {
365367
Self::init(key, 1)
366368
}
367369

368-
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
370+
fn from_rng(mut rng: impl RngCore) -> Self {
371+
// Custom `from_rng` implementation that fills a seed with the same size
372+
// as the entire state.
373+
let mut seed = [w(0u32); RAND_SIZE];
374+
unsafe {
375+
let ptr = seed.as_mut_ptr() as *mut u8;
376+
377+
let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE * 4);
378+
rng.fill_bytes(slice);
379+
}
380+
for i in seed.iter_mut() {
381+
*i = w(i.0.to_le());
382+
}
383+
384+
Self::init(seed, 2)
385+
}
386+
387+
fn try_from_rng<R: TryRngCore>(mut rng: R) -> Result<Self, R::Error> {
369388
// Custom `from_rng` implementation that fills a seed with the same size
370389
// as the entire state.
371390
let mut seed = [w(0u32); RAND_SIZE];
@@ -398,7 +417,7 @@ mod test {
398417
let mut rng1 = IsaacRng::from_seed(seed);
399418
assert_eq!(rng1.next_u32(), 2869442790);
400419

401-
let mut rng2 = IsaacRng::from_rng(rng1).unwrap();
420+
let mut rng2 = IsaacRng::from_rng(rng1);
402421
assert_eq!(rng2.next_u32(), 3094074039);
403422
}
404423

0 commit comments

Comments
 (0)