Skip to content

Commit b31328b

Browse files
authored
Merge pull request #206 from cakebaker/bump_rand
Bump `rand` & adapt tests to its API changes
2 parents 3be5f0e + 1ddd589 commit b31328b

3 files changed

Lines changed: 62 additions & 42 deletions

File tree

Cargo.lock

Lines changed: 48 additions & 29 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
@@ -47,7 +47,7 @@ linux-raw-sys = { version = "0.7.0", features = ["ioctl"] }
4747
nix = { version = "0.29", default-features = false }
4848
phf = "0.11.2"
4949
phf_codegen = "0.11.2"
50-
rand = { version = "0.8", features = ["small_rng"] }
50+
rand = { version = "0.9.0", features = ["small_rng"] }
5151
regex = "1.10.2"
5252
serde = { version = "1.0", features = ["derive"] }
5353
serde_json = "1.0.122"

tests/common/random.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// This file is part of the uutils coreutils package.
1+
// This file is part of the uutils util-linux package.
22
//
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)