libcore: De-export dvec, rand, run, and sys

This commit is contained in:
Patrick Walton 2012-09-26 16:01:40 -07:00
parent 4f15b0d975
commit 18bce94a5a
4 changed files with 59 additions and 91 deletions

View File

@ -16,12 +16,6 @@
use cast::reinterpret_cast; use cast::reinterpret_cast;
use ptr::null; use ptr::null;
export DVec;
export from_elem;
export from_vec;
export extensions;
export unwrap;
/** /**
* A growable, modifiable vector type that accumulates elements into a * A growable, modifiable vector type that accumulates elements into a
* unique vector. * unique vector.
@ -57,27 +51,27 @@
mut data: ~[A] mut data: ~[A]
}; };
enum DVec<A> { pub enum DVec<A> {
DVec_(DVec_<A>) DVec_(DVec_<A>)
} }
/// Creates a new, empty dvec /// Creates a new, empty dvec
fn DVec<A>() -> DVec<A> { pub fn DVec<A>() -> DVec<A> {
DVec_({mut data: ~[]}) DVec_({mut data: ~[]})
} }
/// Creates a new dvec with a single element /// Creates a new dvec with a single element
fn from_elem<A>(+e: A) -> DVec<A> { pub fn from_elem<A>(+e: A) -> DVec<A> {
DVec_({mut data: ~[move e]}) DVec_({mut data: ~[move e]})
} }
/// Creates a new dvec with the contents of a vector /// Creates a new dvec with the contents of a vector
fn from_vec<A>(+v: ~[A]) -> DVec<A> { pub fn from_vec<A>(+v: ~[A]) -> DVec<A> {
DVec_({mut data: move v}) DVec_({mut data: move v})
} }
/// Consumes the vector and returns its contents /// Consumes the vector and returns its contents
fn unwrap<A>(+d: DVec<A>) -> ~[A] { pub fn unwrap<A>(+d: DVec<A>) -> ~[A] {
let DVec_({data: v}) <- d; let DVec_({data: v}) <- d;
move v move v
} }

View File

@ -1,8 +1,5 @@
//! Random number generation //! Random number generation
export Rng, seed, seeded_rng, Weighted, extensions;
export xorshift, seeded_xorshift;
#[allow(non_camel_case_types)] // runtime type #[allow(non_camel_case_types)] // runtime type
enum rctx {} enum rctx {}
@ -17,13 +14,13 @@ enum rctx {}
} }
/// A random number generator /// A random number generator
trait Rng { pub trait Rng {
/// Return the next random integer /// Return the next random integer
fn next() -> u32; fn next() -> u32;
} }
/// A value with a particular weight compared to other values /// A value with a particular weight compared to other values
type Weighted<T> = { weight: uint, item: T }; pub type Weighted<T> = { weight: uint, item: T };
/// Extension methods for random number generators /// Extension methods for random number generators
impl Rng { impl Rng {
@ -260,12 +257,12 @@ impl @RandRes: Rng {
} }
/// Create a new random seed for seeded_rng /// Create a new random seed for seeded_rng
fn seed() -> ~[u8] { pub fn seed() -> ~[u8] {
rustrt::rand_seed() rustrt::rand_seed()
} }
/// Create a random number generator with a system specified seed /// Create a random number generator with a system specified seed
fn Rng() -> Rng { pub fn Rng() -> Rng {
@RandRes(rustrt::rand_new()) as Rng @RandRes(rustrt::rand_new()) as Rng
} }
@ -275,7 +272,7 @@ fn Rng() -> Rng {
* all other generators constructed with the same seed. The seed may be any * all other generators constructed with the same seed. The seed may be any
* length. * length.
*/ */
fn seeded_rng(seed: ~[u8]) -> Rng { pub fn seeded_rng(seed: ~[u8]) -> Rng {
@RandRes(rustrt::rand_new_seeded(seed)) as Rng @RandRes(rustrt::rand_new_seeded(seed)) as Rng
} }
@ -299,21 +296,19 @@ fn next() -> u32 {
} }
} }
fn xorshift() -> Rng { pub fn xorshift() -> Rng {
// constants taken from http://en.wikipedia.org/wiki/Xorshift // constants taken from http://en.wikipedia.org/wiki/Xorshift
seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32) seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
} }
fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng { pub fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng {
{mut x: x, mut y: y, mut z: z, mut w: w} as Rng {mut x: x, mut y: y, mut z: z, mut w: w} as Rng
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub mod tests {
#[legacy_exports];
#[test] #[test]
fn rng_seeded() { pub fn rng_seeded() {
let seed = rand::seed(); let seed = rand::seed();
let ra = rand::seeded_rng(seed); let ra = rand::seeded_rng(seed);
let rb = rand::seeded_rng(seed); let rb = rand::seeded_rng(seed);
@ -321,7 +316,7 @@ fn rng_seeded() {
} }
#[test] #[test]
fn rng_seeded_custom_seed() { pub fn rng_seeded_custom_seed() {
// much shorter than generated seeds which are 1024 bytes // much shorter than generated seeds which are 1024 bytes
let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8]; let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8];
let ra = rand::seeded_rng(seed); let ra = rand::seeded_rng(seed);
@ -330,7 +325,7 @@ fn rng_seeded_custom_seed() {
} }
#[test] #[test]
fn rng_seeded_custom_seed2() { pub fn rng_seeded_custom_seed2() {
let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8]; let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8];
let ra = rand::seeded_rng(seed); let ra = rand::seeded_rng(seed);
// Regression test that isaac is actually using the above vector // Regression test that isaac is actually using the above vector
@ -341,7 +336,7 @@ fn rng_seeded_custom_seed2() {
} }
#[test] #[test]
fn gen_int_range() { pub fn gen_int_range() {
let r = rand::Rng(); let r = rand::Rng();
let a = r.gen_int_range(-3, 42); let a = r.gen_int_range(-3, 42);
assert a >= -3 && a < 42; assert a >= -3 && a < 42;
@ -352,12 +347,12 @@ fn gen_int_range() {
#[test] #[test]
#[should_fail] #[should_fail]
#[ignore(cfg(windows))] #[ignore(cfg(windows))]
fn gen_int_from_fail() { pub fn gen_int_from_fail() {
rand::Rng().gen_int_range(5, -2); rand::Rng().gen_int_range(5, -2);
} }
#[test] #[test]
fn gen_uint_range() { pub fn gen_uint_range() {
let r = rand::Rng(); let r = rand::Rng();
let a = r.gen_uint_range(3u, 42u); let a = r.gen_uint_range(3u, 42u);
assert a >= 3u && a < 42u; assert a >= 3u && a < 42u;
@ -368,12 +363,12 @@ fn gen_uint_range() {
#[test] #[test]
#[should_fail] #[should_fail]
#[ignore(cfg(windows))] #[ignore(cfg(windows))]
fn gen_uint_range_fail() { pub fn gen_uint_range_fail() {
rand::Rng().gen_uint_range(5u, 2u); rand::Rng().gen_uint_range(5u, 2u);
} }
#[test] #[test]
fn gen_float() { pub fn gen_float() {
let r = rand::Rng(); let r = rand::Rng();
let a = r.gen_float(); let a = r.gen_float();
let b = r.gen_float(); let b = r.gen_float();
@ -381,14 +376,14 @@ fn gen_float() {
} }
#[test] #[test]
fn gen_weighted_bool() { pub fn gen_weighted_bool() {
let r = rand::Rng(); let r = rand::Rng();
assert r.gen_weighted_bool(0u) == true; assert r.gen_weighted_bool(0u) == true;
assert r.gen_weighted_bool(1u) == true; assert r.gen_weighted_bool(1u) == true;
} }
#[test] #[test]
fn gen_str() { pub fn gen_str() {
let r = rand::Rng(); let r = rand::Rng();
log(debug, r.gen_str(10u)); log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u)); log(debug, r.gen_str(10u));
@ -399,7 +394,7 @@ fn gen_str() {
} }
#[test] #[test]
fn gen_bytes() { pub fn gen_bytes() {
let r = rand::Rng(); let r = rand::Rng();
assert r.gen_bytes(0u).len() == 0u; assert r.gen_bytes(0u).len() == 0u;
assert r.gen_bytes(10u).len() == 10u; assert r.gen_bytes(10u).len() == 10u;
@ -407,13 +402,13 @@ fn gen_bytes() {
} }
#[test] #[test]
fn choose() { pub fn choose() {
let r = rand::Rng(); let r = rand::Rng();
assert r.choose([1, 1, 1]) == 1; assert r.choose([1, 1, 1]) == 1;
} }
#[test] #[test]
fn choose_option() { pub fn choose_option() {
let r = rand::Rng(); let r = rand::Rng();
let x: Option<int> = r.choose_option([]); let x: Option<int> = r.choose_option([]);
assert x.is_none(); assert x.is_none();
@ -421,7 +416,7 @@ fn choose_option() {
} }
#[test] #[test]
fn choose_weighted() { pub fn choose_weighted() {
let r = rand::Rng(); let r = rand::Rng();
assert r.choose_weighted(~[{weight: 1u, item: 42}]) == 42; assert r.choose_weighted(~[{weight: 1u, item: 42}]) == 42;
assert r.choose_weighted(~[ assert r.choose_weighted(~[
@ -431,7 +426,7 @@ fn choose_weighted() {
} }
#[test] #[test]
fn choose_weighted_option() { pub fn choose_weighted_option() {
let r = rand::Rng(); let r = rand::Rng();
assert r.choose_weighted_option(~[{weight: 1u, item: 42}]) == assert r.choose_weighted_option(~[{weight: 1u, item: 42}]) ==
Some(42); Some(42);
@ -444,7 +439,7 @@ fn choose_weighted_option() {
} }
#[test] #[test]
fn weighted_vec() { pub fn weighted_vec() {
let r = rand::Rng(); let r = rand::Rng();
let empty: ~[int] = ~[]; let empty: ~[int] = ~[];
assert r.weighted_vec(~[]) == empty; assert r.weighted_vec(~[]) == empty;
@ -456,7 +451,7 @@ fn weighted_vec() {
} }
#[test] #[test]
fn shuffle() { pub fn shuffle() {
let r = rand::Rng(); let r = rand::Rng();
let empty: ~[int] = ~[]; let empty: ~[int] = ~[];
assert r.shuffle(~[]) == empty; assert r.shuffle(~[]) == empty;

View File

@ -7,13 +7,6 @@
use libc::{pid_t, c_void, c_int}; use libc::{pid_t, c_void, c_int};
use io::ReaderUtil; use io::ReaderUtil;
export Program;
export run_program;
export start_program;
export program_output;
export spawn_process;
export waitpid;
#[abi = "cdecl"] #[abi = "cdecl"]
extern mod rustrt { extern mod rustrt {
#[legacy_exports]; #[legacy_exports];
@ -24,7 +17,7 @@ fn rust_run_program(argv: **libc::c_char, envp: *c_void,
} }
/// A value representing a child process /// A value representing a child process
trait Program { pub trait Program {
/// Returns the process id of the program /// Returns the process id of the program
fn get_id() -> pid_t; fn get_id() -> pid_t;
@ -68,7 +61,7 @@ trait Program {
* *
* The process id of the spawned process * The process id of the spawned process
*/ */
fn spawn_process(prog: &str, args: &[~str], pub fn spawn_process(prog: &str, args: &[~str],
env: &Option<~[(~str,~str)]>, env: &Option<~[(~str,~str)]>,
dir: &Option<~str>, dir: &Option<~str>,
in_fd: c_int, out_fd: c_int, err_fd: c_int) in_fd: c_int, out_fd: c_int, err_fd: c_int)
@ -166,7 +159,7 @@ fn with_dirp<T>(d: &Option<~str>,
* *
* The process id * The process id
*/ */
fn run_program(prog: &str, args: &[~str]) -> int { pub fn run_program(prog: &str, args: &[~str]) -> int {
let pid = spawn_process(prog, args, &None, &None, let pid = spawn_process(prog, args, &None, &None,
0i32, 0i32, 0i32); 0i32, 0i32, 0i32);
if pid == -1 as pid_t { fail; } if pid == -1 as pid_t { fail; }
@ -189,7 +182,7 @@ fn run_program(prog: &str, args: &[~str]) -> int {
* *
* A class with a <program> field * A class with a <program> field
*/ */
fn start_program(prog: &str, args: &[~str]) -> Program { pub fn start_program(prog: &str, args: &[~str]) -> Program {
let pipe_input = os::pipe(); let pipe_input = os::pipe();
let pipe_output = os::pipe(); let pipe_output = os::pipe();
let pipe_err = os::pipe(); let pipe_err = os::pipe();
@ -278,7 +271,7 @@ fn read_all(rd: io::Reader) -> ~str {
* A record, {status: int, out: str, err: str} containing the exit code, * A record, {status: int, out: str, err: str} containing the exit code,
* the contents of stdout and the contents of stderr. * the contents of stdout and the contents of stderr.
*/ */
fn program_output(prog: &str, args: &[~str]) -> pub fn program_output(prog: &str, args: &[~str]) ->
{status: int, out: ~str, err: ~str} { {status: int, out: ~str, err: ~str} {
let pipe_in = os::pipe(); let pipe_in = os::pipe();
@ -359,7 +352,7 @@ fn readclose(fd: c_int) -> ~str {
} }
/// Waits for a process to exit and returns the exit code /// Waits for a process to exit and returns the exit code
fn waitpid(pid: pid_t) -> int { pub fn waitpid(pid: pid_t) -> int {
return waitpid_os(pid); return waitpid_os(pid);
#[cfg(windows)] #[cfg(windows)]
@ -402,20 +395,18 @@ fn WEXITSTATUS(status: i32) -> i32 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[legacy_exports];
use io::WriterUtil; use io::WriterUtil;
// Regression test for memory leaks // Regression test for memory leaks
#[ignore(cfg(windows))] // FIXME (#2626) #[ignore(cfg(windows))] // FIXME (#2626)
fn test_leaks() { pub fn test_leaks() {
run::run_program("echo", []); run::run_program("echo", []);
run::start_program("echo", []); run::start_program("echo", []);
run::program_output("echo", []); run::program_output("echo", []);
} }
#[test] #[test]
fn test_pipes() { pub fn test_pipes() {
let pipe_in = os::pipe(); let pipe_in = os::pipe();
let pipe_out = os::pipe(); let pipe_out = os::pipe();
let pipe_err = os::pipe(); let pipe_err = os::pipe();
@ -441,7 +432,7 @@ fn test_pipes() {
} }
#[test] #[test]
fn waitpid() { pub fn waitpid() {
let pid = run::spawn_process("false", [], let pid = run::spawn_process("false", [],
&None, &None, &None, &None,
0i32, 0i32, 0i32); 0i32, 0i32, 0i32);

View File

@ -7,21 +7,10 @@
use cmp::{Eq, Ord}; use cmp::{Eq, Ord};
use libc::c_void; use libc::c_void;
export FreeGlue; pub type FreeGlue = fn(*TypeDesc, *c_void);
export TypeDesc;
export Closure;
export get_type_desc;
export size_of;
export min_align_of;
export pref_align_of;
export refcount;
export log_str;
export shape_eq, shape_lt, shape_le;
type FreeGlue = fn(*TypeDesc, *c_void);
// Corresponds to runtime type_desc type // Corresponds to runtime type_desc type
enum TypeDesc = { pub enum TypeDesc = {
size: uint, size: uint,
align: uint, align: uint,
take_glue: uint, take_glue: uint,
@ -31,7 +20,7 @@ enum TypeDesc = {
}; };
/// The representation of a Rust closure /// The representation of a Rust closure
struct Closure { pub struct Closure {
code: *(), code: *(),
env: *(), env: *(),
} }
@ -47,15 +36,15 @@ struct Closure {
/// Compares contents of two pointers using the default method. /// Compares contents of two pointers using the default method.
/// Equivalent to `*x1 == *x2`. Useful for hashtables. /// Equivalent to `*x1 == *x2`. Useful for hashtables.
pure fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool { pub pure fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool {
*x1 == *x2 *x1 == *x2
} }
pure fn shape_lt<T:Ord>(x1: &T, x2: &T) -> bool { pub pure fn shape_lt<T:Ord>(x1: &T, x2: &T) -> bool {
*x1 < *x2 *x1 < *x2
} }
pure fn shape_le<T:Ord>(x1: &T, x2: &T) -> bool { pub pure fn shape_le<T:Ord>(x1: &T, x2: &T) -> bool {
*x1 <= *x2 *x1 <= *x2
} }
@ -66,13 +55,13 @@ struct Closure {
* performing dark magick. * performing dark magick.
*/ */
#[inline(always)] #[inline(always)]
pure fn get_type_desc<T>() -> *TypeDesc { pub pure fn get_type_desc<T>() -> *TypeDesc {
unsafe { rusti::get_tydesc::<T>() as *TypeDesc } unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
} }
/// Returns the size of a type /// Returns the size of a type
#[inline(always)] #[inline(always)]
pure fn size_of<T>() -> uint { pub pure fn size_of<T>() -> uint {
unsafe { rusti::size_of::<T>() } unsafe { rusti::size_of::<T>() }
} }
@ -83,26 +72,26 @@ struct Closure {
* than the preferred alignment. * than the preferred alignment.
*/ */
#[inline(always)] #[inline(always)]
pure fn min_align_of<T>() -> uint { pub pure fn min_align_of<T>() -> uint {
unsafe { rusti::min_align_of::<T>() } unsafe { rusti::min_align_of::<T>() }
} }
/// Returns the preferred alignment of a type /// Returns the preferred alignment of a type
#[inline(always)] #[inline(always)]
pure fn pref_align_of<T>() -> uint { pub pure fn pref_align_of<T>() -> uint {
unsafe { rusti::pref_align_of::<T>() } unsafe { rusti::pref_align_of::<T>() }
} }
/// Returns the refcount of a shared box (as just before calling this) /// Returns the refcount of a shared box (as just before calling this)
#[inline(always)] #[inline(always)]
pure fn refcount<T>(+t: @T) -> uint { pub pure fn refcount<T>(+t: @T) -> uint {
unsafe { unsafe {
let ref_ptr: *uint = cast::reinterpret_cast(&t); let ref_ptr: *uint = cast::reinterpret_cast(&t);
*ref_ptr - 1 *ref_ptr - 1
} }
} }
pure fn log_str<T>(t: &T) -> ~str { pub pure fn log_str<T>(t: &T) -> ~str {
unsafe { unsafe {
do io::with_str_writer |wr| { do io::with_str_writer |wr| {
repr::write_repr(wr, t) repr::write_repr(wr, t)
@ -111,11 +100,10 @@ struct Closure {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub mod tests {
#[legacy_exports];
#[test] #[test]
fn size_of_basic() { pub fn size_of_basic() {
assert size_of::<u8>() == 1u; assert size_of::<u8>() == 1u;
assert size_of::<u16>() == 2u; assert size_of::<u16>() == 2u;
assert size_of::<u32>() == 4u; assert size_of::<u32>() == 4u;
@ -125,20 +113,20 @@ fn size_of_basic() {
#[test] #[test]
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")] #[cfg(target_arch = "arm")]
fn size_of_32() { pub fn size_of_32() {
assert size_of::<uint>() == 4u; assert size_of::<uint>() == 4u;
assert size_of::<*uint>() == 4u; assert size_of::<*uint>() == 4u;
} }
#[test] #[test]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn size_of_64() { pub fn size_of_64() {
assert size_of::<uint>() == 8u; assert size_of::<uint>() == 8u;
assert size_of::<*uint>() == 8u; assert size_of::<*uint>() == 8u;
} }
#[test] #[test]
fn align_of_basic() { pub fn align_of_basic() {
assert pref_align_of::<u8>() == 1u; assert pref_align_of::<u8>() == 1u;
assert pref_align_of::<u16>() == 2u; assert pref_align_of::<u16>() == 2u;
assert pref_align_of::<u32>() == 4u; assert pref_align_of::<u32>() == 4u;
@ -147,20 +135,20 @@ fn align_of_basic() {
#[test] #[test]
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")] #[cfg(target_arch = "arm")]
fn align_of_32() { pub fn align_of_32() {
assert pref_align_of::<uint>() == 4u; assert pref_align_of::<uint>() == 4u;
assert pref_align_of::<*uint>() == 4u; assert pref_align_of::<*uint>() == 4u;
} }
#[test] #[test]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn align_of_64() { pub fn align_of_64() {
assert pref_align_of::<uint>() == 8u; assert pref_align_of::<uint>() == 8u;
assert pref_align_of::<*uint>() == 8u; assert pref_align_of::<*uint>() == 8u;
} }
#[test] #[test]
fn synthesize_closure() unsafe { pub fn synthesize_closure() unsafe {
let x = 10; let x = 10;
let f: fn(int) -> int = |y| x + y; let f: fn(int) -> int = |y| x + y;