Skip to content

Commit 932aeae

Browse files
authored
Merge pull request #315 from cakebaker/bump_rand
Bump `rand` & adapt tests to its API changes
2 parents 8310783 + 6697d2d commit 932aeae

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

Cargo.lock

Lines changed: 44 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ nix = { version = "0.29", default-features = false, features = ["process"] }
5353
phf = "0.11.2"
5454
phf_codegen = "0.11.2"
5555
prettytable-rs = "0.10.0"
56-
rand = { version = "0.8.5", features = ["small_rng"] }
56+
rand = { version = "0.9.0", features = ["small_rng"] }
5757
regex = "1.10.4"
5858
sysinfo = "0.33.0"
5959
tempfile = "3.10.1"

tests/common/random.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use rand::distributions::{Distribution, Uniform};
7-
use rand::{thread_rng, Rng};
6+
use rand::distr::{Distribution, Uniform};
7+
use rand::{rng, Rng};
88

99
/// Samples alphanumeric characters `[A-Za-z0-9]` including newline `\n`
1010
///
@@ -38,7 +38,7 @@ impl AlphanumericNewline {
3838
where
3939
R: Rng + ?Sized,
4040
{
41-
let idx = rng.gen_range(0..Self::CHARSET.len());
41+
let idx = rng.random_range(0..Self::CHARSET.len());
4242
Self::CHARSET[idx]
4343
}
4444
}
@@ -80,7 +80,7 @@ impl RandomString {
8080
where
8181
D: Distribution<u8>,
8282
{
83-
thread_rng()
83+
rng()
8484
.sample_iter(dist)
8585
.take(length)
8686
.map(|b| b as char)
@@ -132,15 +132,15 @@ impl RandomString {
132132
return if num_delimiter > 0 {
133133
String::from(delimiter as char)
134134
} else {
135-
String::from(thread_rng().sample(&dist) as char)
135+
String::from(rng().sample(&dist) as char)
136136
};
137137
}
138138

139139
let samples = length - 1;
140-
let mut result: Vec<u8> = thread_rng().sample_iter(&dist).take(samples).collect();
140+
let mut result: Vec<u8> = rng().sample_iter(&dist).take(samples).collect();
141141

142142
if num_delimiter == 0 {
143-
result.push(thread_rng().sample(&dist));
143+
result.push(rng().sample(&dist));
144144
return String::from_utf8(result).unwrap();
145145
}
146146

@@ -150,9 +150,10 @@ impl RandomString {
150150
num_delimiter
151151
};
152152

153-
let between = Uniform::new(0, samples);
153+
// safe to unwrap because samples is at least 1, thus high > low
154+
let between = Uniform::new(0, samples).unwrap();
154155
for _ in 0..num_delimiter {
155-
let mut pos = between.sample(&mut thread_rng());
156+
let mut pos = between.sample(&mut rng());
156157
let turn = pos;
157158
while result[pos] == delimiter {
158159
pos += 1;
@@ -169,7 +170,7 @@ impl RandomString {
169170
if end_with_delimiter {
170171
result.push(delimiter);
171172
} else {
172-
result.push(thread_rng().sample(&dist));
173+
result.push(rng().sample(&dist));
173174
}
174175

175176
String::from_utf8(result).unwrap()
@@ -179,7 +180,7 @@ impl RandomString {
179180
#[cfg(test)]
180181
mod tests {
181182
use super::*;
182-
use rand::distributions::Alphanumeric;
183+
use rand::distr::Alphanumeric;
183184

184185
#[test]
185186
fn test_random_string_generate() {

0 commit comments

Comments
 (0)