Skip to content

Commit 81c0223

Browse files
committed
auto merge of #14427 : alexcrichton/rust/librand, r=huonw
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 libcore 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. cc #13851 [breaking-change]
2 parents 0935beb + 925ff65 commit 81c0223

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

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

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

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

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

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

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

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

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

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

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

src/libcore/char.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ mod test {
607607
use slice::ImmutableVector;
608608
use option::{Some, None};
609609
use realstd::string::String;
610-
use realstd::str::{Str, StrAllocating};
610+
use realstd::str::Str;
611611

612612
#[test]
613613
fn test_is_lowercase() {

src/libcore/fmt/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
596596
#[cfg(test)]
597597
pub fn format(args: &Arguments) -> ::realstd::string::String {
598598
use str;
599-
use realstd::str::StrAllocating;
600599
use realstd::io::MemWriter;
601600

602601
fn mywrite<T: ::realstd::io::Writer>(t: &mut T, b: &[u8]) {

src/libcore/fmt/num.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ mod tests {
194194
use fmt::radix;
195195
use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
196196
use super::{GenericRadix, Radix};
197-
use realstd::str::{Str, StrAllocating};
197+
use realstd::str::Str;
198198

199199
#[test]
200200
fn test_radix_base() {
@@ -399,71 +399,71 @@ mod bench {
399399
mod uint {
400400
use super::test::Bencher;
401401
use fmt::radix;
402-
use rand::{XorShiftRng, Rng};
402+
use realstd::rand::{weak_rng, Rng};
403403

404404
#[bench]
405405
fn format_bin(b: &mut Bencher) {
406-
let mut rng = XorShiftRng::new().unwrap();
406+
let mut rng = weak_rng();
407407
b.iter(|| { format!("{:t}", rng.gen::<uint>()); })
408408
}
409409

410410
#[bench]
411411
fn format_oct(b: &mut Bencher) {
412-
let mut rng = XorShiftRng::new().unwrap();
412+
let mut rng = weak_rng();
413413
b.iter(|| { format!("{:o}", rng.gen::<uint>()); })
414414
}
415415

416416
#[bench]
417417
fn format_dec(b: &mut Bencher) {
418-
let mut rng = XorShiftRng::new().unwrap();
418+
let mut rng = weak_rng();
419419
b.iter(|| { format!("{:u}", rng.gen::<uint>()); })
420420
}
421421

422422
#[bench]
423423
fn format_hex(b: &mut Bencher) {
424-
let mut rng = XorShiftRng::new().unwrap();
424+
let mut rng = weak_rng();
425425
b.iter(|| { format!("{:x}", rng.gen::<uint>()); })
426426
}
427427

428428
#[bench]
429429
fn format_base_36(b: &mut Bencher) {
430-
let mut rng = XorShiftRng::new().unwrap();
430+
let mut rng = weak_rng();
431431
b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
432432
}
433433
}
434434

435435
mod int {
436436
use super::test::Bencher;
437437
use fmt::radix;
438-
use rand::{XorShiftRng, Rng};
438+
use realstd::rand::{weak_rng, Rng};
439439

440440
#[bench]
441441
fn format_bin(b: &mut Bencher) {
442-
let mut rng = XorShiftRng::new().unwrap();
442+
let mut rng = weak_rng();
443443
b.iter(|| { format!("{:t}", rng.gen::<int>()); })
444444
}
445445

446446
#[bench]
447447
fn format_oct(b: &mut Bencher) {
448-
let mut rng = XorShiftRng::new().unwrap();
448+
let mut rng = weak_rng();
449449
b.iter(|| { format!("{:o}", rng.gen::<int>()); })
450450
}
451451

452452
#[bench]
453453
fn format_dec(b: &mut Bencher) {
454-
let mut rng = XorShiftRng::new().unwrap();
454+
let mut rng = weak_rng();
455455
b.iter(|| { format!("{:d}", rng.gen::<int>()); })
456456
}
457457

458458
#[bench]
459459
fn format_hex(b: &mut Bencher) {
460-
let mut rng = XorShiftRng::new().unwrap();
460+
let mut rng = weak_rng();
461461
b.iter(|| { format!("{:x}", rng.gen::<int>()); })
462462
}
463463

464464
#[bench]
465465
fn format_base_36(b: &mut Bencher) {
466-
let mut rng = XorShiftRng::new().unwrap();
466+
let mut rng = weak_rng();
467467
b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
468468
}
469469
}

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#[cfg(test)] extern crate realcore = "core";
6060
#[cfg(test)] extern crate libc;
6161
#[cfg(test)] extern crate native;
62-
#[cfg(test)] extern crate rand;
6362
#[cfg(test)] extern crate realstd = "std";
6463

6564
#[cfg(test)] pub use cmp = realcore::cmp;

src/libcore/result.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,10 @@ pub fn fold_<T,E,Iter:Iterator<Result<T,E>>>(iterator: Iter) -> Result<(),E> {
637637
#[cfg(test)]
638638
mod tests {
639639
use realstd::vec::Vec;
640-
use realstd::string::String;
641640

642641
use result::{collect, fold, fold_};
643642
use prelude::*;
644-
use realstd::str::{Str, StrAllocating};
643+
use realstd::str::Str;
645644
use iter::range;
646645

647646
pub fn op1() -> Result<int, &'static str> { Ok(666) }

src/libcore/tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ mod tests {
294294
use super::*;
295295
use clone::Clone;
296296
use cmp::*;
297-
use realstd::str::{Str, StrAllocating};
297+
use realstd::str::Str;
298298

299299
#[test]
300300
fn test_clone() {

src/libflate/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {
108108

109109
#[cfg(test)]
110110
mod tests {
111-
extern crate rand;
112-
113111
use super::{inflate_bytes, deflate_bytes};
114-
use self::rand::Rng;
112+
use std::rand;
113+
use std::rand::Rng;
115114

116115
#[test]
117116
#[allow(deprecated_owned_vector)]
@@ -120,7 +119,8 @@ mod tests {
120119
let mut words = vec!();
121120
for _ in range(0, 20) {
122121
let range = r.gen_range(1u, 10);
123-
words.push(r.gen_vec::<u8>(range));
122+
let v = r.gen_iter::<u8>().take(range).collect::<Vec<u8>>();
123+
words.push(v);
124124
}
125125
for _ in range(0, 20) {
126126
let mut input = vec![];

src/libgreen/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@
212212

213213
#[cfg(test)] #[phase(syntax, link)] extern crate log;
214214
#[cfg(test)] extern crate rustuv;
215-
extern crate rand;
216215
extern crate libc;
217216
extern crate alloc;
218217

src/libgreen/sched.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::sync::deque;
1717
use std::unstable::mutex::NativeMutex;
1818
use std::raw;
1919

20-
use rand::{XorShiftRng, Rng, Rand};
20+
use std::rand::{XorShiftRng, Rng, Rand};
2121

2222
use TaskState;
2323
use context::Context;
@@ -977,8 +977,9 @@ impl ClosureConverter for UnsafeTaskReceiver {
977977
// worry there.
978978
#[cfg(windows)]
979979
fn new_sched_rng() -> XorShiftRng {
980-
match XorShiftRng::new() {
981-
Ok(r) => r,
980+
use std::rand::OSRng;
981+
match OSRng::new() {
982+
Ok(mut r) => r.gen(),
982983
Err(e) => {
983984
rtabort!("sched: failed to create seeded RNG: {}", e)
984985
}
@@ -988,7 +989,7 @@ fn new_sched_rng() -> XorShiftRng {
988989
fn new_sched_rng() -> XorShiftRng {
989990
use libc;
990991
use std::mem;
991-
use rand::SeedableRng;
992+
use std::rand::SeedableRng;
992993

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

src/libnum/bigint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ mod biguint_tests {
13721372
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
13731373
use std::num::{ToPrimitive, FromPrimitive};
13741374
use std::num::CheckedDiv;
1375-
use rand::{task_rng};
1375+
use std::rand::task_rng;
13761376
use std::u64;
13771377

13781378
#[test]
@@ -2220,7 +2220,7 @@ mod bigint_tests {
22202220
use std::num::CheckedDiv;
22212221
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
22222222
use std::num::{ToPrimitive, FromPrimitive};
2223-
use rand::{task_rng};
2223+
use std::rand::task_rng;
22242224
use std::u64;
22252225

22262226
#[test]

0 commit comments

Comments
 (0)