Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Recreate a rand module #14427

Merged
merged 1 commit into from
May 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ DEPS_core :=
DEPS_rlibc :=
DEPS_alloc := core libc native:jemalloc
DEPS_debug := std
DEPS_std := core libc alloc native:rustrt native:backtrace
DEPS_std := core rand libc alloc native:rustrt native:backtrace
DEPS_graphviz := std
DEPS_green := std rand native:context_switch
DEPS_green := std native:context_switch
DEPS_rustuv := std native:uv native:uv_support
DEPS_native := std
DEPS_syntax := std term serialize collections log fmt_macros debug
Expand All @@ -77,16 +77,16 @@ DEPS_glob := std
DEPS_serialize := std collections log
DEPS_term := std collections log
DEPS_semver := std
DEPS_uuid := std serialize rand
DEPS_uuid := std serialize
DEPS_sync := std alloc
DEPS_getopts := std
DEPS_collections := std rand debug
DEPS_collections := std debug
DEPS_fourcc := syntax std
DEPS_hexfloat := syntax std
DEPS_num := std rand
DEPS_num := std
DEPS_test := std collections getopts serialize term time regex
DEPS_time := std serialize sync
DEPS_rand := std
DEPS_rand := core
DEPS_url := std collections
DEPS_workcache := std serialize collections log
DEPS_log := std sync
Expand All @@ -104,6 +104,7 @@ TOOL_SOURCE_rustc := $(S)src/driver/driver.rs
ONLY_RLIB_core := 1
ONLY_RLIB_rlibc := 1
ONLY_RLIB_alloc := 1
ONLY_RLIB_rand := 1

################################################################################
# You should not need to edit below this line
Expand Down
6 changes: 3 additions & 3 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,10 @@ Here is a small example showing how to use Arcs. We wish to run concurrently sev
a single large vector of floats. Each task needs the full vector to perform its duty.

~~~
extern crate rand;
extern crate sync;

use sync::Arc;
use std::rand;

fn pnorm(nums: &[f64], p: uint) -> f64 {
nums.iter().fold(0.0, |a, b| a + b.powf(p as f64)).powf(1.0 / (p as f64))
Expand All @@ -358,7 +358,7 @@ created by the line

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

~~~
# extern crate sync;
# extern crate rand;
# use std::rand;
# use sync::Arc;
# fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 }
# fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@ mod tests {
use bitv;

use std::uint;
use rand;
use rand::Rng;
use std::rand;
use std::rand::Rng;

static BENCH_BITS : uint = 1 << 14;

Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub mod bench {
extern crate test;
use self::test::Bencher;
use std::container::MutableMap;
use rand;
use rand::Rng;
use std::rand;
use std::rand::Rng;

pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
map: &mut M,
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ mod tests {
extern crate test;
use self::test::Bencher;
use deque::Deque;
use rand;
use std::rand;
use super::{DList, Node, ListInsertion};

pub fn check_links<T>(list: &DList<T>) {
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::iter::{range, range_inclusive};
use std::mem::replace;
use std::num;
use std::option::{Option, Some, None};
use rand;
use rand::Rng;
use std::rand;
use std::rand::Rng;
use std::result::{Ok, Err};
use std::slice::ImmutableVector;

Expand Down
1 change: 0 additions & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#![deny(deprecated_owned_vector)]

extern crate rand;
extern crate debug;

#[cfg(test)] extern crate test;
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,8 @@ impl<T: TotalOrd> Extendable<T> for TreeSet<T> {
mod test_treemap {
use super::{TreeMap, TreeNode};

use rand::Rng;
use rand;
use std::rand::Rng;
use std::rand;

#[test]
fn find_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ mod test_map {
mod bench_map {
extern crate test;
use super::TrieMap;
use rand::{weak_rng, Rng};
use std::rand::{weak_rng, Rng};
use self::test::Bencher;

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tests {
use prelude::*;
use super::*;
use realstd::owned::{Box, AnyOwnExt};
use realstd::str::{Str, StrAllocating};
use realstd::str::Str;

#[deriving(Eq, Show)]
struct Test;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ mod test {
use slice::ImmutableVector;
use option::{Some, None};
use realstd::string::String;
use realstd::str::{Str, StrAllocating};
use realstd::str::Str;

#[test]
fn test_is_lowercase() {
Expand Down
1 change: 0 additions & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
#[cfg(test)]
pub fn format(args: &Arguments) -> ::realstd::string::String {
use str;
use realstd::str::StrAllocating;
use realstd::io::MemWriter;

fn mywrite<T: ::realstd::io::Writer>(t: &mut T, b: &[u8]) {
Expand Down
26 changes: 13 additions & 13 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod tests {
use fmt::radix;
use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
use super::{GenericRadix, Radix};
use realstd::str::{Str, StrAllocating};
use realstd::str::Str;

#[test]
fn test_radix_base() {
Expand Down Expand Up @@ -399,71 +399,71 @@ mod bench {
mod uint {
use super::test::Bencher;
use fmt::radix;
use rand::{XorShiftRng, Rng};
use realstd::rand::{weak_rng, Rng};

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

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

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

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

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

mod int {
use super::test::Bencher;
use fmt::radix;
use rand::{XorShiftRng, Rng};
use realstd::rand::{weak_rng, Rng};

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

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

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

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

#[bench]
fn format_base_36(b: &mut Bencher) {
let mut rng = XorShiftRng::new().unwrap();
let mut rng = weak_rng();
b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
}
}
Expand Down
1 change: 0 additions & 1 deletion src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#[cfg(test)] extern crate realcore = "core";
#[cfg(test)] extern crate libc;
#[cfg(test)] extern crate native;
#[cfg(test)] extern crate rand;
#[cfg(test)] extern crate realstd = "std";

#[cfg(test)] pub use cmp = realcore::cmp;
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,11 +637,10 @@ pub fn fold_<T,E,Iter:Iterator<Result<T,E>>>(iterator: Iter) -> Result<(),E> {
#[cfg(test)]
mod tests {
use realstd::vec::Vec;
use realstd::string::String;

use result::{collect, fold, fold_};
use prelude::*;
use realstd::str::{Str, StrAllocating};
use realstd::str::Str;
use iter::range;

pub fn op1() -> Result<int, &'static str> { Ok(666) }
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ mod tests {
use super::*;
use clone::Clone;
use cmp::*;
use realstd::str::{Str, StrAllocating};
use realstd::str::Str;

#[test]
fn test_clone() {
Expand Down
8 changes: 4 additions & 4 deletions src/libflate/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {

#[cfg(test)]
mod tests {
extern crate rand;

use super::{inflate_bytes, deflate_bytes};
use self::rand::Rng;
use std::rand;
use std::rand::Rng;

#[test]
#[allow(deprecated_owned_vector)]
Expand All @@ -120,7 +119,8 @@ mod tests {
let mut words = vec!();
for _ in range(0, 20) {
let range = r.gen_range(1u, 10);
words.push(r.gen_vec::<u8>(range));
let v = r.gen_iter::<u8>().take(range).collect::<Vec<u8>>();
words.push(v);
}
for _ in range(0, 20) {
let mut input = vec![];
Expand Down
1 change: 0 additions & 1 deletion src/libgreen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@

#[cfg(test)] #[phase(syntax, link)] extern crate log;
#[cfg(test)] extern crate rustuv;
extern crate rand;
extern crate libc;
extern crate alloc;

Expand Down
9 changes: 5 additions & 4 deletions src/libgreen/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::sync::deque;
use std::unstable::mutex::NativeMutex;
use std::raw;

use rand::{XorShiftRng, Rng, Rand};
use std::rand::{XorShiftRng, Rng, Rand};

use TaskState;
use context::Context;
Expand Down Expand Up @@ -977,8 +977,9 @@ impl ClosureConverter for UnsafeTaskReceiver {
// worry there.
#[cfg(windows)]
fn new_sched_rng() -> XorShiftRng {
match XorShiftRng::new() {
Ok(r) => r,
use std::rand::OSRng;
match OSRng::new() {
Ok(mut r) => r.gen(),
Err(e) => {
rtabort!("sched: failed to create seeded RNG: {}", e)
}
Expand All @@ -988,7 +989,7 @@ fn new_sched_rng() -> XorShiftRng {
fn new_sched_rng() -> XorShiftRng {
use libc;
use std::mem;
use rand::SeedableRng;
use std::rand::SeedableRng;

let fd = "/dev/urandom".with_c_str(|name| {
unsafe { libc::open(name, libc::O_RDONLY, 0) }
Expand Down
4 changes: 2 additions & 2 deletions src/libnum/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ mod biguint_tests {
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
use std::num::{ToPrimitive, FromPrimitive};
use std::num::CheckedDiv;
use rand::{task_rng};
use std::rand::task_rng;
use std::u64;

#[test]
Expand Down Expand Up @@ -2220,7 +2220,7 @@ mod bigint_tests {
use std::num::CheckedDiv;
use std::num::{Zero, One, FromStrRadix, ToStrRadix};
use std::num::{ToPrimitive, FromPrimitive};
use rand::{task_rng};
use std::rand::task_rng;
use std::u64;

#[test]
Expand Down
Loading