Skip to content

Commit c803144

Browse files
committed
fix(tool): update rand library
1 parent cbee020 commit c803144

File tree

9 files changed

+400
-32
lines changed

9 files changed

+400
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ itertools = "*"
2525
json = "*"
2626
log = "*"
2727
lzw = "*"
28-
rand = "^0.4"
28+
rand = "^0.6"
2929
test-logger = "*"
3030
vec_map = "*"
3131
webidl = "*"

crates/binjs_generic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ binjs_meta = { path = "../binjs_meta", version = "*" }
1818
binjs_shared = { path = "../binjs_shared", version = "*" }
1919
json = "^0.11"
2020
log = "*"
21-
rand = "^0.4"
21+
rand = "^0.6"
2222

2323
[dev-dependencies]
2424
clap = "^2.30"

crates/binjs_generic/src/pick.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use util::pick;
22

33
use binjs_meta::spec::*;
4+
use std::iter;
45

56
use json;
67
use json::JsonValue as JSON;
78
use rand;
9+
use rand::distributions::Alphanumeric;
810

911
pub trait Pick {
1012
fn random<T: rand::Rng>(&self, syntax: &Spec, rng: &mut T, depth_limit: isize) -> JSON;
@@ -69,11 +71,11 @@ impl Pick for TypeSpec {
6971
{
7072
const MAX_STRING_LEN : usize = 10;
7173
let len = rng.gen_range(0, MAX_STRING_LEN);
72-
let string : String = rng.gen_ascii_chars().take(len).collect();
74+
let string : String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(len).collect();
7375
json::from(string)
7476
}
7577
TypeSpec::Number => {
76-
json::from(rng.next_f64())
78+
json::from(rng.next_u64())
7779
}
7880
TypeSpec::Void =>
7981
JSON::Null,

crates/binjs_io/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ flate2 = "^1.0"
1313
itertools = "*"
1414
lzw = "*"
1515
log = "*"
16-
rand = "^0.4"
16+
rand = "^0.6"
1717
range-encoding = "0.1.9"
1818
serde = "^1.0"
1919
serde_derive = "^1.0"

crates/binjs_io/src/bytes/compress.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use bytes::serialize::*;
44
use bytes::varnum::*;
55

6-
use rand;
7-
use rand::{ Rand, Rng };
6+
use rand::Rng;
7+
use rand::distributions::Distribution;
8+
use rand::distributions::Standard;
9+
use rand::thread_rng;
810

911
use std;
1012
use std::collections::HashSet;
@@ -32,11 +34,11 @@ pub enum Compression {
3234
Lzw,
3335
}
3436

35-
impl Rand for Compression {
36-
fn rand<R: Rng>(rng: &mut R) -> Self {
37+
impl Distribution<Compression> for Standard {
38+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Compression {
3739
use self::Compression::*;
38-
rng.choose(&[Identity, Gzip, /* Deflate is apprently broken https://github.com/alexcrichton/flate2-rs/issues/151 , */ Brotli /*, Lzw doesn't work yet */])
39-
.unwrap() // The array is not empty.
40+
rng.choose(&[Identity, Gzip, /* Deflate is apprently broken https://github.com/alexcrichton/flate2-rs/issues/151 , */ Brotli /*, Lzw doesn't work yet */])
41+
.unwrap()
4042
.clone()
4143
}
4244
}
@@ -79,7 +81,7 @@ impl Compression {
7981
Some("br") => Compression::Brotli,
8082
Some("gzip") => Compression::Gzip,
8183
Some("deflate") => Compression::Deflate,
82-
Some("random") => Compression::rand(&mut rand::weak_rng()),
84+
Some("random") => thread_rng().gen(),
8385
Some(_) => {
8486
return None;
8587
}

crates/binjs_io/src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use std::fmt::{ Debug, Formatter };
2626
use std::cell::RefCell;
2727
use std::rc::Rc;
2828

29-
use rand::{ Rand, Rng };
29+
use rand::Rng;
30+
use rand::distributions::{ Distribution, Standard };
3031

3132
pub use bytes::compress::Compression;
3233

@@ -163,9 +164,9 @@ impl CompressionTarget {
163164

164165
/// Support picking a random compression target.
165166
/// Used for testing.
166-
impl Rand for CompressionTarget {
167-
fn rand<R: Rng>(rng: &mut R) -> Self {
168-
Self::new(Compression::rand(rng))
167+
impl Distribution<CompressionTarget> for Standard {
168+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> CompressionTarget {
169+
CompressionTarget::new(rng.gen())
169170
}
170171
}
171172
impl std::io::Write for CompressionTarget {
@@ -213,20 +214,20 @@ pub enum Format {
213214

214215
/// Support picking a random format.
215216
/// Used for testing.
216-
impl Rand for Format {
217-
fn rand<'a, R: Rng>(rng: &'a mut R) -> Self {
217+
impl Distribution<Format> for Standard {
218+
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Format {
218219
let generators = [
219-
Rc::new(|_| Self::simple()) as Rc<Fn(&'a mut R) -> Format>,
220+
Rc::new(|_| Format::simple()) as Rc<Fn(&'a mut R) -> Format>,
220221
Rc::new(|rng| {
221222
use multipart::{ Statistics, Targets };
222223
let stats = Rc::new(RefCell::new(Statistics::default()
223224
.with_source_bytes(0)));
224225

225226
Format::Multipart {
226227
targets: Targets {
227-
strings_table: CompressionTarget::rand(rng),
228-
grammar_table: CompressionTarget::rand(rng),
229-
tree: CompressionTarget::rand(rng),
228+
strings_table: rng.gen(),
229+
grammar_table: rng.gen(),
230+
tree: rng.gen(),
230231
},
231232
stats
232233
}
@@ -254,9 +255,9 @@ impl Format {
254255
Format::Multipart { stats, .. } =>
255256
Format::Multipart {
256257
targets: multipart::Targets {
257-
strings_table: CompressionTarget::rand(rng),
258-
grammar_table: CompressionTarget::rand(rng),
259-
tree: CompressionTarget::rand(rng),
258+
strings_table: rng.gen(),
259+
grammar_table: rng.gen(),
260+
tree: rng.gen(),
260261
},
261262
stats
262263
}

src/util/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use binjs_generic::syntax::ASTError;
22

33
use rand;
4+
use rand::distributions::Alphanumeric;
45
use json::JsonValue as JSON;
56
use json::object::Object as Object;
67

@@ -11,17 +12,17 @@ use std::path::*;
1112
pub fn get_temporary_file(extension: &str) -> std::result::Result<(PathBuf, File), std::io::Error> {
1213
use rand::Rng;
1314
let directory = std::env::temp_dir();
14-
let mut rng = rand::os::OsRng::new()
15+
let mut rng = rand::rngs::OsRng::new()
1516
.unwrap();
16-
let mut ascii = rng.gen_ascii_chars();
17+
let ascii = rng.sample(Alphanumeric);
1718
let mut buf = Vec::with_capacity(8);
1819
let mut error = None;
1920
const ATTEMPTS : usize = 1024;
2021
for _ in 0..ATTEMPTS { // Limit number of attempts
2122
// FIXME: There must be a nicer way to do this.
2223
buf.clear();
2324
for _ in 0..8 {
24-
buf.push(ascii.next().unwrap());
25+
buf.push(ascii);
2526
}
2627
let name : String = buf.iter().collect();
2728
let path = directory.as_path()

tests/test_roundtrip.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use binjs::specialized::es6::ast::{ IOPath, Script, Visitor, Walker, WalkPath };
1818
use std::io::Cursor;
1919
use std::thread;
2020

21-
use rand::Rng;
21+
use rand::RngCore;
2222

2323
/// This test takes 1h+ on Travis, which is too long, so we need to
2424
/// reduce it. So each individual file + options combination has
@@ -36,9 +36,9 @@ fn progress() {
3636
///
3737
/// Skipping is generally needed to avoid timeouts, because we
3838
/// are testing lots of files.
39-
fn should_skip(rng: &mut Rng) -> bool {
40-
let float = rng.next_f64();
41-
float < CHANCES_TO_SKIP
39+
fn should_skip(rng: &mut RngCore) -> bool {
40+
let float = rng.next_u64();
41+
float < CHANCES_TO_SKIP as u64
4242
}
4343

4444
/// A visitor designed to reset offsets to 0.

0 commit comments

Comments
 (0)