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 @@ Note that recursive use is not permitted.
use cast::reinterpret_cast;
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
* unique vector.
@ -57,27 +51,27 @@ type DVec_<A> = {
mut data: ~[A]
};
enum DVec<A> {
pub enum DVec<A> {
DVec_(DVec_<A>)
}
/// Creates a new, empty dvec
fn DVec<A>() -> DVec<A> {
pub fn DVec<A>() -> DVec<A> {
DVec_({mut data: ~[]})
}
/// 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]})
}
/// 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})
}
/// 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;
move v
}

View File

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

View File

@ -7,13 +7,6 @@ use option::{Some, None};
use libc::{pid_t, c_void, c_int};
use io::ReaderUtil;
export Program;
export run_program;
export start_program;
export program_output;
export spawn_process;
export waitpid;
#[abi = "cdecl"]
extern mod rustrt {
#[legacy_exports];
@ -24,7 +17,7 @@ extern mod rustrt {
}
/// A value representing a child process
trait Program {
pub trait Program {
/// Returns the process id of the program
fn get_id() -> pid_t;
@ -68,7 +61,7 @@ trait Program {
*
* 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)]>,
dir: &Option<~str>,
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
*/
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,
0i32, 0i32, 0i32);
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
*/
fn start_program(prog: &str, args: &[~str]) -> Program {
pub fn start_program(prog: &str, args: &[~str]) -> Program {
let pipe_input = os::pipe();
let pipe_output = 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,
* 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} {
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
fn waitpid(pid: pid_t) -> int {
pub fn waitpid(pid: pid_t) -> int {
return waitpid_os(pid);
#[cfg(windows)]
@ -402,20 +395,18 @@ fn waitpid(pid: pid_t) -> int {
#[cfg(test)]
mod tests {
#[legacy_exports];
use io::WriterUtil;
// Regression test for memory leaks
#[ignore(cfg(windows))] // FIXME (#2626)
fn test_leaks() {
pub fn test_leaks() {
run::run_program("echo", []);
run::start_program("echo", []);
run::program_output("echo", []);
}
#[test]
fn test_pipes() {
pub fn test_pipes() {
let pipe_in = os::pipe();
let pipe_out = os::pipe();
let pipe_err = os::pipe();
@ -441,7 +432,7 @@ mod tests {
}
#[test]
fn waitpid() {
pub fn waitpid() {
let pid = run::spawn_process("false", [],
&None, &None,
0i32, 0i32, 0i32);

View File

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