Skip to content

Commit 925ff65

Browse files
committed
std: Recreate a rand module
This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc #13851 [breaking-change]
1 parent bee4e6a commit 925ff65

Some content is hidden

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

47 files changed

+963
-844
lines changed

mk/crates.mk

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ DEPS_core :=
6060
DEPS_rlibc :=
6161
DEPS_alloc := core libc native:jemalloc
6262
DEPS_debug := std
63-
DEPS_std := core libc alloc native:rustrt native:backtrace
63+
DEPS_std := core rand libc alloc native:rustrt native:backtrace
6464
DEPS_graphviz := std
65-
DEPS_green := std rand native:context_switch
65+
DEPS_green := std native:context_switch
6666
DEPS_rustuv := std native:uv native:uv_support
6767
DEPS_native := std
6868
DEPS_syntax := std term serialize collections log fmt_macros debug
@@ -77,16 +77,16 @@ DEPS_glob := std
7777
DEPS_serialize := std collections log
7878
DEPS_term := std collections log
7979
DEPS_semver := std
80-
DEPS_uuid := std serialize rand
80+
DEPS_uuid := std serialize
8181
DEPS_sync := std alloc
8282
DEPS_getopts := std
83-
DEPS_collections := std rand debug
83+
DEPS_collections := std debug
8484
DEPS_fourcc := syntax std
8585
DEPS_hexfloat := syntax std
86-
DEPS_num := std rand
86+
DEPS_num := std
8787
DEPS_test := std collections getopts serialize term time regex
8888
DEPS_time := std serialize sync
89-
DEPS_rand := std
89+
DEPS_rand := core
9090
DEPS_url := std collections
9191
DEPS_workcache := std serialize collections log
9292
DEPS_log := std sync
@@ -104,6 +104,7 @@ TOOL_SOURCE_rustc := $(S)src/driver/driver.rs
104104
ONLY_RLIB_core := 1
105105
ONLY_RLIB_rlibc := 1
106106
ONLY_RLIB_alloc := 1
107+
ONLY_RLIB_rand := 1
107108

108109
################################################################################
109110
# You should not need to edit below this line

src/doc/guide-tasks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ Here is a small example showing how to use Arcs. We wish to run concurrently sev
329329
a single large vector of floats. Each task needs the full vector to perform its duty.
330330

331331
~~~
332-
extern crate rand;
333332
extern crate sync;
334333
335334
use sync::Arc;
335+
use std::rand;
336336
337337
fn pnorm(nums: &[f64], p: uint) -> f64 {
338338
nums.iter().fold(0.0, |a, b| a + b.powf(p as f64)).powf(1.0 / (p as f64))
@@ -358,7 +358,7 @@ created by the line
358358

359359
~~~
360360
# extern crate sync;
361-
# extern crate rand;
361+
# use std::rand;
362362
# use sync::Arc;
363363
# fn main() {
364364
# let numbers = Vec::from_fn(1000000, |_| rand::random::<f64>());
@@ -372,7 +372,7 @@ reference to the underlying vector as if it were local.
372372

373373
~~~
374374
# extern crate sync;
375-
# extern crate rand;
375+
# use std::rand;
376376
# use sync::Arc;
377377
# fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 }
378378
# fn main() {

src/libcollections/bitv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ mod tests {
980980
use bitv;
981981

982982
use std::uint;
983-
use rand;
984-
use rand::Rng;
983+
use std::rand;
984+
use std::rand::Rng;
985985

986986
static BENCH_BITS : uint = 1 << 14;
987987

src/libcollections/deque.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub mod bench {
4444
extern crate test;
4545
use self::test::Bencher;
4646
use std::container::MutableMap;
47-
use rand;
48-
use rand::Rng;
47+
use std::rand;
48+
use std::rand::Rng;
4949

5050
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
5151
map: &mut M,

src/libcollections/dlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ mod tests {
610610
extern crate test;
611611
use self::test::Bencher;
612612
use deque::Deque;
613-
use rand;
613+
use std::rand;
614614
use super::{DList, Node, ListInsertion};
615615

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

src/libcollections/hashmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use std::iter::{range, range_inclusive};
2424
use std::mem::replace;
2525
use std::num;
2626
use std::option::{Option, Some, None};
27-
use rand;
28-
use rand::Rng;
27+
use std::rand;
28+
use std::rand::Rng;
2929
use std::result::{Ok, Err};
3030
use std::slice::ImmutableVector;
3131

src/libcollections/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#![deny(deprecated_owned_vector)]
2626

27-
extern crate rand;
2827
extern crate debug;
2928

3029
#[cfg(test)] extern crate test;

src/libcollections/treemap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ impl<T: TotalOrd> Extendable<T> for TreeSet<T> {
10001000
mod test_treemap {
10011001
use super::{TreeMap, TreeNode};
10021002

1003-
use rand::Rng;
1004-
use rand;
1003+
use std::rand::Rng;
1004+
use std::rand;
10051005

10061006
#[test]
10071007
fn find_empty() {

src/libcollections/trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ mod test_map {
915915
mod bench_map {
916916
extern crate test;
917917
use super::TrieMap;
918-
use rand::{weak_rng, Rng};
918+
use std::rand::{weak_rng, Rng};
919919
use self::test::Bencher;
920920

921921
#[bench]

src/libcore/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mod tests {
119119
use prelude::*;
120120
use super::*;
121121
use realstd::owned::{Box, AnyOwnExt};
122-
use realstd::str::{Str, StrAllocating};
122+
use realstd::str::Str;
123123

124124
#[deriving(Eq, Show)]
125125
struct Test;

0 commit comments

Comments
 (0)