Skip to content

Commit d477a47

Browse files
authored
Merge pull request #216 from sendilkumarn/rand-update
fix(tool): update rand crate to the latest version
2 parents 47417bb + b391855 commit d477a47

File tree

8 files changed

+40
-33
lines changed

8 files changed

+40
-33
lines changed

Cargo.toml

+1-1
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

+1-1
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

+4-2
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.gen::<f64>())
7779
}
7880
TypeSpec::Void =>
7981
JSON::Null,

crates/binjs_io/Cargo.toml

+1-1
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

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
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;
10+
use rand::seq::SliceRandom;
811

912
use std;
1013
use std::collections::HashSet;
@@ -32,11 +35,12 @@ pub enum Compression {
3235
Lzw,
3336
}
3437

35-
impl Rand for Compression {
36-
fn rand<R: Rng>(rng: &mut R) -> Self {
38+
impl Distribution<Compression> for Standard {
39+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Compression {
3740
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.
41+
let choices = [Identity, Gzip, /* Deflate is apprently broken https://github.com/alexcrichton/flate2-rs/issues/151 , */ Brotli /*, Lzw doesn't work yet */];
42+
choices.choose(rng)
43+
.unwrap()
4044
.clone()
4145
}
4246
}
@@ -79,7 +83,7 @@ impl Compression {
7983
Some("br") => Compression::Brotli,
8084
Some("gzip") => Compression::Gzip,
8185
Some("deflate") => Compression::Deflate,
82-
Some("random") => Compression::rand(&mut rand::weak_rng()),
86+
Some("random") => thread_rng().gen(),
8387
Some(_) => {
8488
return None;
8589
}

crates/binjs_io/src/lib.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ 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 };
31+
use rand::seq::SliceRandom;
3032

3133
pub use bytes::compress::Compression;
3234

@@ -163,9 +165,9 @@ impl CompressionTarget {
163165

164166
/// Support picking a random compression target.
165167
/// Used for testing.
166-
impl Rand for CompressionTarget {
167-
fn rand<R: Rng>(rng: &mut R) -> Self {
168-
Self::new(Compression::rand(rng))
168+
impl Distribution<CompressionTarget> for Standard {
169+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> CompressionTarget {
170+
CompressionTarget::new(rng.gen())
169171
}
170172
}
171173
impl std::io::Write for CompressionTarget {
@@ -213,27 +215,27 @@ pub enum Format {
213215

214216
/// Support picking a random format.
215217
/// Used for testing.
216-
impl Rand for Format {
217-
fn rand<'a, R: Rng>(rng: &'a mut R) -> Self {
218+
impl Distribution<Format> for Standard {
219+
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Format {
218220
let generators = [
219-
Rc::new(|_| Self::simple()) as Rc<Fn(&'a mut R) -> Format>,
221+
Rc::new(|_| Format::simple()) as Rc<Fn(&'a mut R) -> Format>,
220222
Rc::new(|rng| {
221223
use multipart::{ Statistics, Targets };
222224
let stats = Rc::new(RefCell::new(Statistics::default()
223225
.with_source_bytes(0)));
224226

225227
Format::Multipart {
226228
targets: Targets {
227-
strings_table: CompressionTarget::rand(rng),
228-
grammar_table: CompressionTarget::rand(rng),
229-
tree: CompressionTarget::rand(rng),
229+
strings_table: rng.gen(),
230+
grammar_table: rng.gen(),
231+
tree: rng.gen(),
230232
},
231233
stats
232234
}
233235
}),
234236
Rc::new(|_| Format::XML),
235237
];
236-
let pick : Rc<Fn(&'a mut R) -> Format> = rng.choose(&generators)
238+
let pick : Rc<Fn(&'a mut R) -> Format> = generators.choose(rng)
237239
.map(Rc::clone)
238240
.unwrap(); // Never empty
239241
pick(rng)
@@ -254,9 +256,9 @@ impl Format {
254256
Format::Multipart { stats, .. } =>
255257
Format::Multipart {
256258
targets: multipart::Targets {
257-
strings_table: CompressionTarget::rand(rng),
258-
grammar_table: CompressionTarget::rand(rng),
259-
tree: CompressionTarget::rand(rng),
259+
strings_table: rng.gen(),
260+
grammar_table: rng.gen(),
261+
tree: rng.gen(),
260262
},
261263
stats
262264
}

src/util/mod.rs

+4-3
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use binjs::specialized::es6::ast::{ IOPath, Script, Visitor, Walker, WalkPath };
1818
use std::io::Cursor;
1919
use std::thread;
2020

21-
use rand::Rng;
22-
2321
/// This test takes 1h+ on Travis, which is too long, so we need to
2422
/// reduce it. So each individual file + options combination has
2523
/// a `CHANCES_TO_SKIP` probability of being skipped.
@@ -36,8 +34,8 @@ fn progress() {
3634
///
3735
/// Skipping is generally needed to avoid timeouts, because we
3836
/// are testing lots of files.
39-
fn should_skip(rng: &mut Rng) -> bool {
40-
let float = rng.next_f64();
37+
fn should_skip<T: rand::Rng>(rng: &mut T) -> bool {
38+
let float = rng.gen::<f64>();
4139
float < CHANCES_TO_SKIP
4240
}
4341

0 commit comments

Comments
 (0)