Skip to content

Commit 6bc8f16

Browse files
committed
std: Remove rand crate and module
This commit removes the `rand` crate from the standard library facade as well as the `__rand` module in the standard library. Neither of these were used in any meaningful way in the standard library itself. The only need for randomness in libstd is to initialize the thread-local keys of a `HashMap`, and that unconditionally used `OsRng` defined in the standard library anyway. The cruft of the `rand` crate and the extra `rand` support in the standard library makes libstd slightly more difficult to port to new platforms, namely WebAssembly which doesn't have any randomness at all (without interfacing with JS). The purpose of this commit is to clarify and streamline randomness in libstd, focusing on how it's only required in one location, hashmap seeds. Note that the `rand` crate out of tree has almost always been a drop-in replacement for the `rand` crate in-tree, so any usage (accidental or purposeful) of the crate in-tree should switch to the `rand` crate on crates.io. This then also has the further benefit of avoiding duplication (mostly) between the two crates!
1 parent fc77b62 commit 6bc8f16

File tree

46 files changed

+161
-4380
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+161
-4380
lines changed

src/Cargo.lock

+5-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bootstrap/compile.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ impl Step for Std {
107107
let mut cargo = builder.cargo(compiler, Mode::Libstd, target, "build");
108108
std_cargo(build, &compiler, target, &mut cargo);
109109
run_cargo(build,
110-
&mut cargo,
111-
&libstd_stamp(build, compiler, target));
110+
&mut cargo,
111+
&libstd_stamp(build, compiler, target));
112112

113113
builder.ensure(StdLink {
114114
compiler: builder.compiler(compiler.stage, build.build),
@@ -359,8 +359,8 @@ impl Step for Test {
359359
let mut cargo = builder.cargo(compiler, Mode::Libtest, target, "build");
360360
test_cargo(build, &compiler, target, &mut cargo);
361361
run_cargo(build,
362-
&mut cargo,
363-
&libtest_stamp(build, compiler, target));
362+
&mut cargo,
363+
&libtest_stamp(build, compiler, target));
364364

365365
builder.ensure(TestLink {
366366
compiler: builder.compiler(compiler.stage, build.build),
@@ -866,12 +866,13 @@ fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path) {
866866
// `std-<hash>.dll.lib` on Windows. The aforementioned methods only
867867
// split the file name by the last extension (`.lib`) while we need
868868
// to split by all extensions (`.dll.lib`).
869+
let expected_len = t!(filename.metadata()).len();
869870
let filename = filename.file_name().unwrap().to_str().unwrap();
870871
let mut parts = filename.splitn(2, '.');
871872
let file_stem = parts.next().unwrap().to_owned();
872873
let extension = parts.next().unwrap().to_owned();
873874

874-
toplevel.push((file_stem, extension));
875+
toplevel.push((file_stem, extension, expected_len));
875876
}
876877
}
877878

@@ -891,11 +892,12 @@ fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path) {
891892
.map(|e| t!(e))
892893
.map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
893894
.collect::<Vec<_>>();
894-
for (prefix, extension) in toplevel {
895-
let candidates = contents.iter().filter(|&&(_, ref filename, _)| {
895+
for (prefix, extension, expected_len) in toplevel {
896+
let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| {
896897
filename.starts_with(&prefix[..]) &&
897898
filename[prefix.len()..].starts_with("-") &&
898-
filename.ends_with(&extension[..])
899+
filename.ends_with(&extension[..]) &&
900+
meta.len() == expected_len
899901
});
900902
let max = candidates.max_by_key(|&&(_, _, ref metadata)| {
901903
FileTime::from_last_modification_time(metadata)

src/bootstrap/dist.rs

-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ impl Step for Src {
743743
"src/liblibc",
744744
"src/libpanic_abort",
745745
"src/libpanic_unwind",
746-
"src/librand",
747746
"src/librustc_asan",
748747
"src/librustc_lsan",
749748
"src/librustc_msan",

src/doc/unstable-book/src/library-features/rand.md

-5
This file was deleted.

src/liballoc/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ path = "lib.rs"
1111
core = { path = "../libcore" }
1212
std_unicode = { path = "../libstd_unicode" }
1313

14+
[dev-dependencies]
15+
rand = "0.3"
16+
1417
[[test]]
1518
name = "collectionstests"
1619
path = "../liballoc/tests/lib.rs"

src/liballoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
extern crate std;
136136
#[cfg(test)]
137137
extern crate test;
138+
#[cfg(test)]
139+
extern crate rand;
138140

139141
extern crate std_unicode;
140142

src/liballoc/linked_list.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,11 @@ unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
12691269

12701270
#[cfg(test)]
12711271
mod tests {
1272-
use std::__rand::{thread_rng, Rng};
12731272
use std::thread;
12741273
use std::vec::Vec;
12751274

1275+
use rand::{thread_rng, Rng};
1276+
12761277
use super::{LinkedList, Node};
12771278

12781279
#[cfg(test)]

src/liballoc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(unicode)]
3131

3232
extern crate std_unicode;
33+
extern crate rand;
3334

3435
use std::hash::{Hash, Hasher};
3536
use std::collections::hash_map::DefaultHasher;

src/liballoc/tests/slice.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
use std::cmp::Ordering::{Equal, Greater, Less};
1212
use std::mem;
13-
use std::__rand::{Rng, thread_rng};
1413
use std::rc::Rc;
1514

15+
use rand::{Rng, thread_rng};
16+
1617
fn square(n: usize) -> usize {
1718
n * n
1819
}

src/libcore/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test = false
1010
bench = false
1111

1212
[dev-dependencies]
13-
rand = { path = "../librand" }
13+
rand = "0.3"
1414

1515
[[test]]
1616
name = "coretests"

src/libcore/tests/num/flt2dec/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
use std::prelude::v1::*;
1212
use std::{str, mem, i16, f32, f64, fmt};
13-
use std::__rand as rand;
14-
use rand::{Rand, XorShiftRng};
13+
use rand::{self, Rand, XorShiftRng};
1514
use rand::distributions::{IndependentSample, Range};
1615

1716
use core::num::flt2dec::{decode, DecodableFloat, FullDecoded, Decoded};

src/librand/Cargo.toml

-12
This file was deleted.

0 commit comments

Comments
 (0)