Skip to content

Commit 0aa3b88

Browse files
committed
auto merge of #12650 : huonw/rust/librand, r=alexcrichton
Move std::rand to a separate rand crate This functionality is not super-core and so doesn't need to be included in std. It's possible that std may need rand (it does a little bit now, for io::test) in which case the functionality required could be moved to a secret hidden module and reexposed by librand. Unfortunately, using #[deprecated] here is hard: there's too much to mock to make it feasible, since we have to ensure that programs still typecheck to reach the linting phase. Also, deprecates/removes `rand::rng` (this time using `#[deprecated]`), since it's too easy to accidentally use inside a loop, making things very slow (have to read randomness from the OS and seed the RNG each time.)
2 parents 74bfa71 + 689f197 commit 0aa3b88

File tree

91 files changed

+329
-280
lines changed

Some content is hidden

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

91 files changed

+329
-280
lines changed

mk/crates.mk

+7-6
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
################################################################################
5151

5252
TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
53-
uuid serialize sync getopts collections num test time
53+
uuid serialize sync getopts collections num test time rand
5454
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat
5555
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5656
TOOLS := compiletest rustdoc rustc
5757

5858
DEPS_std := native:rustrt native:compiler-rt
59-
DEPS_extra := std term sync serialize getopts collections time
60-
DEPS_green := std native:context_switch
59+
DEPS_extra := std term sync serialize getopts collections time rand
60+
DEPS_green := std rand native:context_switch
6161
DEPS_rustuv := std native:uv native:uv_support
6262
DEPS_native := std
6363
DEPS_syntax := std term serialize collections
@@ -71,15 +71,16 @@ DEPS_glob := std
7171
DEPS_serialize := std collections
7272
DEPS_term := std collections
7373
DEPS_semver := std
74-
DEPS_uuid := std serialize
74+
DEPS_uuid := std serialize rand
7575
DEPS_sync := std
7676
DEPS_getopts := std
77-
DEPS_collections := std
77+
DEPS_collections := std rand
7878
DEPS_fourcc := syntax std
7979
DEPS_hexfloat := syntax std
80-
DEPS_num := std
80+
DEPS_num := std rand
8181
DEPS_test := std extra collections getopts serialize term
8282
DEPS_time := std serialize
83+
DEPS_rand := std
8384

8485
TOOL_DEPS_compiletest := test green rustuv getopts
8586
TOOL_DEPS_rustdoc := rustdoc native

src/doc/guide-tasks.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ concurrency at this writing:
5050
* [`sync::DuplexStream`] - An extension of `pipes::stream` that allows both sending and receiving,
5151
* [`sync::SyncChan`] - An extension of `pipes::stream` that provides synchronous message sending,
5252
* [`sync::SyncPort`] - An extension of `pipes::stream` that acknowledges each message received,
53-
* [`sync::rendezvous`] - Creates a stream whose channel, upon sending a message, blocks until the
53+
* [`sync::rendezvous`] - Creates a stream whose channel, upon sending a message, blocks until the
5454
message is received.
5555
* [`sync::Arc`] - The Arc (atomically reference counted) type, for safely sharing immutable data,
5656
* [`sync::RWArc`] - A dual-mode Arc protected by a reader-writer lock,
5757
* [`sync::MutexArc`] - An Arc with mutable data protected by a blocking mutex,
5858
* [`sync::Semaphore`] - A counting, blocking, bounded-waiting semaphore,
59-
* [`sync::Mutex`] - A blocking, bounded-waiting, mutual exclusion lock with an associated
59+
* [`sync::Mutex`] - A blocking, bounded-waiting, mutual exclusion lock with an associated
6060
FIFO condition variable,
6161
* [`sync::RWLock`] - A blocking, no-starvation, reader-writer lock with an associated condvar,
6262
* [`sync::Barrier`] - A barrier enables multiple tasks to synchronize the beginning
@@ -343,8 +343,8 @@ a single large vector of floats. Each task needs the full vector to perform its
343343

344344
~~~
345345
# extern crate sync;
346+
extern crate rand;
346347
# use std::vec;
347-
# use std::rand;
348348
use sync::Arc;
349349
350350
fn pnorm(nums: &~[f64], p: uint) -> f64 {
@@ -376,9 +376,9 @@ created by the line
376376

377377
~~~
378378
# extern crate sync;
379+
# extern crate rand;
379380
# use sync::Arc;
380381
# use std::vec;
381-
# use std::rand;
382382
# fn main() {
383383
# let numbers = vec::from_fn(1000000, |_| rand::random::<f64>());
384384
let numbers_arc=Arc::new(numbers);
@@ -389,9 +389,9 @@ and a clone of it is sent to each task
389389

390390
~~~
391391
# extern crate sync;
392+
# extern crate rand;
392393
# use sync::Arc;
393394
# use std::vec;
394-
# use std::rand;
395395
# fn main() {
396396
# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
397397
# let numbers_arc = Arc::new(numbers);
@@ -406,9 +406,9 @@ Each task recovers the underlying data by
406406

407407
~~~
408408
# extern crate sync;
409+
# extern crate rand;
409410
# use sync::Arc;
410411
# use std::vec;
411-
# use std::rand;
412412
# fn main() {
413413
# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
414414
# let numbers_arc=Arc::new(numbers);

src/doc/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ of type `ABC` can be randomly generated and converted to a string:
25292529
#[deriving(Eq)]
25302530
struct Circle { radius: f64 }
25312531
2532-
#[deriving(Rand, Show)]
2532+
#[deriving(Clone, Show)]
25332533
enum ABC { A, B, C }
25342534
~~~
25352535

src/etc/generate-deriving-span-tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
4040
#[feature(struct_variant)];
4141
extern crate extra;
42+
extern crate rand;
4243
4344
{error_deriving}
4445
struct Error;

src/etc/ziggurat_tables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# except according to those terms.
1212

1313
# This creates the tables used for distributions implemented using the
14-
# ziggurat algorithm in `std::rand::distributions;`. They are
14+
# ziggurat algorithm in `rand::distributions;`. They are
1515
# (basically) the tables as used in the ZIGNOR variant (Doornik 2005).
1616
# They are changed rarely, so the generated file should be checked in
1717
# to git.

src/libcollections/bitv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,8 @@ mod tests {
947947

948948
use std::uint;
949949
use std::vec;
950-
use std::rand;
951-
use std::rand::Rng;
950+
use rand;
951+
use rand::Rng;
952952

953953
static BENCH_BITS : uint = 1 << 14;
954954

src/libcollections/deque.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ pub mod bench {
4444
extern crate test;
4545
use self::test::BenchHarness;
4646
use std::container::MutableMap;
47-
use std::{vec, rand};
48-
use std::rand::Rng;
47+
use std::vec;
48+
use rand;
49+
use rand::Rng;
4950

5051
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
5152
map: &mut M,

src/libcollections/dlist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ mod tests {
633633
extern crate test;
634634
use self::test::BenchHarness;
635635
use deque::Deque;
636-
use std::rand;
636+
use rand;
637637
use super::{DList, Node, ListInsertion};
638638

639639
pub fn check_links<T>(list: &DList<T>) {

src/libcollections/hashmap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ use std::iter::{FilterMap, Chain, Repeat, Zip};
6161
use std::iter;
6262
use std::mem::replace;
6363
use std::num;
64-
use std::rand::Rng;
65-
use std::rand;
64+
use rand::Rng;
65+
use rand;
6666
use std::vec::{Items, MutItems};
6767
use std::vec_ng::Vec;
6868
use std::vec_ng;

src/libcollections/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#[allow(unrecognized_lint)];
2424
#[allow(default_type_param_usage)];
2525

26+
extern crate rand;
27+
2628
#[cfg(test)] extern crate test;
2729

2830
pub use bitv::Bitv;

src/libcollections/treemap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,8 @@ mod test_treemap {
10091009

10101010
use super::{TreeMap, TreeNode};
10111011

1012-
use std::rand::Rng;
1013-
use std::rand;
1012+
use rand::Rng;
1013+
use rand;
10141014

10151015
#[test]
10161016
fn find_empty() {

src/libcollections/trie.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ mod test_map {
898898
mod bench_map {
899899
extern crate test;
900900
use super::TrieMap;
901-
use std::rand::{weak_rng, Rng};
901+
use rand::{weak_rng, Rng};
902902
use self::test::BenchHarness;
903903

904904
#[bench]

src/libextra/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ Rust extras are part of the standard Rust distribution.
3434
#[deny(non_camel_case_types)];
3535
#[deny(missing_doc)];
3636

37-
extern crate sync;
38-
extern crate serialize;
3937
extern crate collections;
38+
extern crate rand;
39+
extern crate serialize;
40+
extern crate sync;
4041
extern crate time;
4142

4243
// Utility modules

src/libextra/tempfile.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
1313

1414
use std::os;
15-
use std::rand::Rng;
16-
use std::rand;
15+
use rand::{task_rng, Rng};
1716
use std::io;
1817
use std::io::fs;
1918

@@ -35,7 +34,7 @@ impl TempDir {
3534
return TempDir::new_in(&abs_tmpdir, suffix);
3635
}
3736

38-
let mut r = rand::rng();
37+
let mut r = task_rng();
3938
for _ in range(0u, 1000) {
4039
let p = tmpdir.join(r.gen_ascii_str(16) + suffix);
4140
match fs::mkdir(&p, io::UserRWX) {

src/libflate/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> CVec<u8> {
9090

9191
#[cfg(test)]
9292
mod tests {
93+
extern crate rand;
94+
9395
use super::{inflate_bytes, deflate_bytes};
94-
use std::rand;
95-
use std::rand::Rng;
96+
use self::rand::Rng;
9697

9798
#[test]
9899
fn test_flate_round_trip() {
99-
let mut r = rand::rng();
100+
let mut r = rand::task_rng();
100101
let mut words = ~[];
101102
for _ in range(0, 20) {
102103
let range = r.gen_range(1u, 10);

src/libgreen/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@
175175
#[feature(macro_rules)];
176176
#[allow(visible_private_types)];
177177

178+
extern crate rand;
179+
178180
use std::mem::replace;
179181
use std::os;
180182
use std::rt::crate_map;

src/libgreen/sched.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std::cast;
12-
use std::rand::{XorShiftRng, Rng, Rand};
1312
use std::rt::local::Local;
1413
use std::rt::rtio::{RemoteCallback, PausableIdleCallback, Callback, EventLoop};
1514
use std::rt::task::BlockedTask;
@@ -18,6 +17,8 @@ use std::sync::deque;
1817
use std::unstable::mutex::NativeMutex;
1918
use std::raw;
2019

20+
use rand::{XorShiftRng, Rng, Rand};
21+
2122
use TaskState;
2223
use context::Context;
2324
use coroutine::Coroutine;
@@ -957,7 +958,7 @@ fn new_sched_rng() -> XorShiftRng {
957958
fn new_sched_rng() -> XorShiftRng {
958959
use std::libc;
959960
use std::mem;
960-
use std::rand::SeedableRng;
961+
use rand::SeedableRng;
961962

962963
let fd = "/dev/urandom".with_c_str(|name| {
963964
unsafe { libc::open(name, libc::O_RDONLY, 0) }

src/libnum/bigint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::from_str::FromStr;
2424
use std::num::CheckedDiv;
2525
use std::num::{Bitwise, ToPrimitive, FromPrimitive};
2626
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
27-
use std::rand::Rng;
27+
use rand::Rng;
2828
use std::str;
2929
use std::uint;
3030
use std::vec;
@@ -1470,7 +1470,7 @@ mod biguint_tests {
14701470
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
14711471
use std::num::{ToPrimitive, FromPrimitive};
14721472
use std::num::CheckedDiv;
1473-
use std::rand::{task_rng};
1473+
use rand::{task_rng};
14741474
use std::str;
14751475
use std::u64;
14761476
use std::vec;
@@ -2205,7 +2205,7 @@ mod bigint_tests {
22052205
use std::num::CheckedDiv;
22062206
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
22072207
use std::num::{ToPrimitive, FromPrimitive};
2208-
use std::rand::{task_rng};
2208+
use rand::{task_rng};
22092209
use std::u64;
22102210

22112211
#[test]

src/libnum/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#[crate_type = "dylib"];
1616
#[license = "MIT/ASL2"];
1717

18+
extern crate rand;
19+
1820
pub mod bigint;
1921
pub mod rational;
2022
pub mod complex;

src/libstd/rand/distributions/exponential.rs renamed to src/librand/distributions/exponential.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
//! The exponential distribution.
1212
13-
use num::Float;
14-
use rand::{Rng, Rand};
15-
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
13+
use std::num::Float;
14+
use {Rng, Rand};
15+
use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
1616

1717
/// A wrapper around an `f64` to generate Exp(1) random numbers.
1818
///
@@ -58,8 +58,7 @@ impl Rand for Exp1 {
5858
/// # Example
5959
///
6060
/// ```rust
61-
/// use std::rand;
62-
/// use std::rand::distributions::{Exp, IndependentSample};
61+
/// use rand::distributions::{Exp, IndependentSample};
6362
///
6463
/// let exp = Exp::new(2.0);
6564
/// let v = exp.ind_sample(&mut rand::task_rng());
@@ -91,10 +90,9 @@ impl IndependentSample<f64> for Exp {
9190

9291
#[cfg(test)]
9392
mod test {
94-
use rand::distributions::*;
95-
use prelude::*;
96-
use rand::*;
97-
use super::*;
93+
use distributions::{Sample, IndependentSample};
94+
use {Rng, task_rng};
95+
use super::Exp;
9896

9997
#[test]
10098
fn test_exp() {
@@ -121,11 +119,10 @@ mod test {
121119
mod bench {
122120
extern crate test;
123121
use self::test::BenchHarness;
124-
use mem::size_of;
125-
use prelude::*;
126-
use rand::{XorShiftRng, RAND_BENCH_N};
127-
use super::*;
128-
use rand::distributions::*;
122+
use std::mem::size_of;
123+
use {XorShiftRng, RAND_BENCH_N};
124+
use super::Exp;
125+
use distributions::Sample;
129126

130127
#[bench]
131128
fn rand_exp(bh: &mut BenchHarness) {

0 commit comments

Comments
 (0)