Skip to content

Commit

Permalink
std: Remove rand crate and module
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
alexcrichton committed Nov 9, 2017
1 parent fc77b62 commit 6bc8f16
Show file tree
Hide file tree
Showing 46 changed files with 161 additions and 4,380 deletions.
14 changes: 5 additions & 9 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ impl Step for Std {
let mut cargo = builder.cargo(compiler, Mode::Libstd, target, "build");
std_cargo(build, &compiler, target, &mut cargo);
run_cargo(build,
&mut cargo,
&libstd_stamp(build, compiler, target));
&mut cargo,
&libstd_stamp(build, compiler, target));

builder.ensure(StdLink {
compiler: builder.compiler(compiler.stage, build.build),
Expand Down Expand Up @@ -359,8 +359,8 @@ impl Step for Test {
let mut cargo = builder.cargo(compiler, Mode::Libtest, target, "build");
test_cargo(build, &compiler, target, &mut cargo);
run_cargo(build,
&mut cargo,
&libtest_stamp(build, compiler, target));
&mut cargo,
&libtest_stamp(build, compiler, target));

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

toplevel.push((file_stem, extension));
toplevel.push((file_stem, extension, expected_len));
}
}

Expand All @@ -891,11 +892,12 @@ fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path) {
.map(|e| t!(e))
.map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
.collect::<Vec<_>>();
for (prefix, extension) in toplevel {
let candidates = contents.iter().filter(|&&(_, ref filename, _)| {
for (prefix, extension, expected_len) in toplevel {
let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| {
filename.starts_with(&prefix[..]) &&
filename[prefix.len()..].starts_with("-") &&
filename.ends_with(&extension[..])
filename.ends_with(&extension[..]) &&
meta.len() == expected_len
});
let max = candidates.max_by_key(|&&(_, _, ref metadata)| {
FileTime::from_last_modification_time(metadata)
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,6 @@ impl Step for Src {
"src/liblibc",
"src/libpanic_abort",
"src/libpanic_unwind",
"src/librand",
"src/librustc_asan",
"src/librustc_lsan",
"src/librustc_msan",
Expand Down
5 changes: 0 additions & 5 deletions src/doc/unstable-book/src/library-features/rand.md

This file was deleted.

3 changes: 3 additions & 0 deletions src/liballoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ path = "lib.rs"
core = { path = "../libcore" }
std_unicode = { path = "../libstd_unicode" }

[dev-dependencies]
rand = "0.3"

[[test]]
name = "collectionstests"
path = "../liballoc/tests/lib.rs"
Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
extern crate std;
#[cfg(test)]
extern crate test;
#[cfg(test)]
extern crate rand;

extern crate std_unicode;

Expand Down
3 changes: 2 additions & 1 deletion src/liballoc/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,10 +1269,11 @@ unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}

#[cfg(test)]
mod tests {
use std::__rand::{thread_rng, Rng};
use std::thread;
use std::vec::Vec;

use rand::{thread_rng, Rng};

use super::{LinkedList, Node};

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![feature(unicode)]

extern crate std_unicode;
extern crate rand;

use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
Expand Down
3 changes: 2 additions & 1 deletion src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

use std::cmp::Ordering::{Equal, Greater, Less};
use std::mem;
use std::__rand::{Rng, thread_rng};
use std::rc::Rc;

use rand::{Rng, thread_rng};

fn square(n: usize) -> usize {
n * n
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test = false
bench = false

[dev-dependencies]
rand = { path = "../librand" }
rand = "0.3"

[[test]]
name = "coretests"
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/tests/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

use std::prelude::v1::*;
use std::{str, mem, i16, f32, f64, fmt};
use std::__rand as rand;
use rand::{Rand, XorShiftRng};
use rand::{self, Rand, XorShiftRng};
use rand::distributions::{IndependentSample, Range};

use core::num::flt2dec::{decode, DecodableFloat, FullDecoded, Decoded};
Expand Down
12 changes: 0 additions & 12 deletions src/librand/Cargo.toml

This file was deleted.

Loading

0 comments on commit 6bc8f16

Please sign in to comment.