about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/flate.rs1
-rw-r--r--src/libcore/hashmap.rs1
-rw-r--r--src/libcore/io.rs29
-rw-r--r--src/libcore/os.rs3
-rw-r--r--src/libcore/rand.rs122
-rw-r--r--src/libcore/repr.rs2
-rw-r--r--src/libcore/run.rs14
-rw-r--r--src/libcore/task/local_data_priv.rs4
-rw-r--r--src/libcore/unstable/at_exit.rs7
9 files changed, 133 insertions, 50 deletions
diff --git a/src/libcore/flate.rs b/src/libcore/flate.rs
index f9348ae5380..c830648e9df 100644
--- a/src/libcore/flate.rs
+++ b/src/libcore/flate.rs
@@ -17,6 +17,7 @@ Simple compression
 use libc;
 use libc::{c_void, size_t, c_int};
 use ptr;
+use rand::RngUtil;
 use vec;
 
 #[cfg(test)] use rand;
diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs
index 2adcee495a7..931866999c4 100644
--- a/src/libcore/hashmap.rs
+++ b/src/libcore/hashmap.rs
@@ -20,6 +20,7 @@ pub mod linear {
     use hash::Hash;
     use iter;
     use option::{None, Option, Some};
+    use rand::RngUtil;
     use rand;
     use uint;
     use vec;
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index a9dfecfe9b1..4942b058785 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -785,8 +785,7 @@ pub fn fd_writer(fd: fd_t, cleanup: bool) -> @Writer {
 
 
 pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
-    -> Result<Writer, ~str> {
-
+                   -> Result<@Writer, ~str> {
     #[cfg(windows)]
     fn wb() -> c_int {
       (O_WRONLY | libc::consts::os::extra::O_BINARY) as c_int
@@ -1079,22 +1078,24 @@ impl<T:Writer> WriterUtil for T {
 }
 
 #[allow(non_implicitly_copyable_typarams)]
-pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<Writer, ~str> {
+pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
     mk_file_writer(path, flags).chain(|w| result::Ok(w))
 }
 
 
 // FIXME: fileflags // #2004
-pub fn buffered_file_writer(path: &Path) -> Result<Writer, ~str> {
+pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> {
     unsafe {
         let f = do os::as_c_charp(path.to_str()) |pathbuf| {
             do os::as_c_charp("w") |modebuf| {
                 libc::fopen(pathbuf, modebuf)
             }
         };
-        return if f as uint == 0u { result::Err(~"error opening "
-                                                + path.to_str()) }
-        else { result::Ok(FILE_writer(f, true)) }
+        return if f as uint == 0u {
+            result::Err(~"error opening " + path.to_str())
+        } else {
+            result::Ok(FILE_writer(f, true))
+        }
     }
 }
 
@@ -1142,14 +1143,14 @@ pub pure fn BytesWriter() -> BytesWriter {
     BytesWriter { bytes: ~[], mut pos: 0u }
 }
 
-pub pure fn with_bytes_writer(f: &fn(Writer)) -> ~[u8] {
+pub pure fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
     let wr = @BytesWriter();
-    f(wr as Writer);
+    f(wr as @Writer);
     let @BytesWriter{bytes, _} = wr;
     return bytes;
 }
 
-pub pure fn with_str_writer(f: &fn(Writer)) -> ~str {
+pub pure fn with_str_writer(f: &fn(@Writer)) -> ~str {
     let mut v = with_bytes_writer(f);
 
     // FIXME (#3758): This should not be needed.
@@ -1277,8 +1278,8 @@ pub mod fsync {
     pub trait FSyncable { fn fsync(&self, l: Level) -> int; }
 
     // Call o.fsync after executing blk
-    pub fn obj_sync(o: FSyncable, opt_level: Option<Level>,
-                    blk: &fn(v: Res<FSyncable>)) {
+    pub fn obj_sync(o: @FSyncable, opt_level: Option<Level>,
+                    blk: &fn(v: Res<@FSyncable>)) {
         blk(Res(Arg {
             val: o, opt_level: opt_level,
             fsync_fn: |o, l| o.fsync(l)
@@ -1305,12 +1306,12 @@ mod tests {
             ~"A hoopy frood who really knows where his towel is.";
         debug!(copy frood);
         {
-            let out: io::Writer =
+            let out: @io::Writer =
                 result::get(
                     &io::file_writer(tmpfile, ~[io::Create, io::Truncate]));
             out.write_str(frood);
         }
-        let inp: io::Reader = result::get(&io::file_reader(tmpfile));
+        let inp: @io::Reader = result::get(&io::file_reader(tmpfile));
         let frood2: ~str = inp.read_c_str();
         debug!(copy frood2);
         fail_unless!(frood == frood2);
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index 86ea7a491fc..746c403c7bf 100644
--- a/src/libcore/os.rs
+++ b/src/libcore/os.rs
@@ -1265,6 +1265,7 @@ mod tests {
     use os::{remove_file, setenv};
     use os;
     use path::Path;
+    use rand::RngUtil;
     use rand;
     use run;
     use str;
@@ -1282,7 +1283,7 @@ mod tests {
     }
 
     fn make_rand_name() -> ~str {
-        let rng: rand::Rng = rand::Rng();
+        let rng: @rand::Rng = rand::Rng();
         let n = ~"TEST" + rng.gen_str(10u);
         fail_unless!(getenv(n).is_none());
         n
diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs
index 7668513a3ca..4cb5e58b733 100644
--- a/src/libcore/rand.rs
+++ b/src/libcore/rand.rs
@@ -22,97 +22,100 @@ use libc::size_t;
 
 /// A type that can be randomly generated using an RNG
 pub trait Rand {
-    static fn rand(rng: rand::Rng) -> Self;
+    static fn rand(rng: @rand::Rng) -> Self;
 }
 
 impl Rand for int {
-    static fn rand(rng: rand::Rng) -> int {
+    static fn rand(rng: @rand::Rng) -> int {
         rng.gen_int()
     }
 }
 
 impl Rand for i8 {
-    static fn rand(rng: rand::Rng) -> i8 {
+    static fn rand(rng: @rand::Rng) -> i8 {
         rng.gen_i8()
     }
 }
 
 impl Rand for i16 {
-    static fn rand(rng: rand::Rng) -> i16 {
+    static fn rand(rng: @rand::Rng) -> i16 {
         rng.gen_i16()
     }
 }
 
 impl Rand for i32 {
-    static fn rand(rng: rand::Rng) -> i32 {
+    static fn rand(rng: @rand::Rng) -> i32 {
         rng.gen_i32()
     }
 }
 
 impl Rand for i64 {
-    static fn rand(rng: rand::Rng) -> i64 {
+    static fn rand(rng: @rand::Rng) -> i64 {
         rng.gen_i64()
     }
 }
 
 impl Rand for u8 {
-    static fn rand(rng: rand::Rng) -> u8 {
+    static fn rand(rng: @rand::Rng) -> u8 {
         rng.gen_u8()
     }
 }
 
 impl Rand for u16 {
-    static fn rand(rng: rand::Rng) -> u16 {
+    static fn rand(rng: @rand::Rng) -> u16 {
         rng.gen_u16()
     }
 }
 
 impl Rand for u32 {
-    static fn rand(rng: rand::Rng) -> u32 {
+    static fn rand(rng: @rand::Rng) -> u32 {
         rng.gen_u32()
     }
 }
 
 impl Rand for u64 {
-    static fn rand(rng: rand::Rng) -> u64 {
+    static fn rand(rng: @rand::Rng) -> u64 {
         rng.gen_u64()
     }
 }
 
 impl Rand for float {
-    static fn rand(rng: rand::Rng) -> float {
+    static fn rand(rng: @rand::Rng) -> float {
         rng.gen_float()
     }
 }
 
 impl Rand for f32 {
-    static fn rand(rng: rand::Rng) -> f32 {
+    static fn rand(rng: @rand::Rng) -> f32 {
         rng.gen_f32()
     }
 }
 
 impl Rand for f64 {
-    static fn rand(rng: rand::Rng) -> f64 {
+    static fn rand(rng: @rand::Rng) -> f64 {
         rng.gen_f64()
     }
 }
 
 impl Rand for char {
-    static fn rand(rng: rand::Rng) -> char {
+    static fn rand(rng: @rand::Rng) -> char {
         rng.gen_char()
     }
 }
 
 impl Rand for bool {
-    static fn rand(rng: rand::Rng) -> bool {
+    static fn rand(rng: @rand::Rng) -> bool {
         rng.gen_bool()
     }
 }
 
 impl<T:Rand> Rand for Option<T> {
-    static fn rand(rng: rand::Rng) -> Option<T> {
-        if rng.gen_bool() { Some(Rand::rand(rng)) }
-        else { None }
+    static fn rand(rng: @rand::Rng) -> Option<T> {
+        if rng.gen_bool() {
+            Some(Rand::rand(rng))
+        } else {
+            None
+        }
     }
 }
 
@@ -145,8 +148,83 @@ pub struct Weighted<T> {
     item: T,
 }
 
+pub trait RngUtil {
+    fn gen<T:Rand>(&self) -> T;
+    /// Return a random int
+    fn gen_int(&self) -> int;
+    fn gen_int_range(&self, start: int, end: int) -> int;
+    /// Return a random i8
+    fn gen_i8(&self) -> i8;
+    /// Return a random i16
+    fn gen_i16(&self) -> i16;
+    /// Return a random i32
+    fn gen_i32(&self) -> i32;
+    /// Return a random i64
+    fn gen_i64(&self) -> i64;
+    /// Return a random uint
+    fn gen_uint(&self) -> uint;
+    /**
+     * Return a uint randomly chosen from the range [start, end),
+     * failing if start >= end
+     */
+    fn gen_uint_range(&self, start: uint, end: uint) -> uint;
+    /// Return a random u8
+    fn gen_u8(&self) -> u8;
+    /// Return a random u16
+    fn gen_u16(&self) -> u16;
+    /// Return a random u32
+    fn gen_u32(&self) -> u32;
+    /// Return a random u64
+    fn gen_u64(&self) -> u64;
+    /// Return a random float in the interval [0,1]
+    fn gen_float(&self) -> float;
+    /// Return a random f32 in the interval [0,1]
+    fn gen_f32(&self) -> f32;
+    /// Return a random f64 in the interval [0,1]
+    fn gen_f64(&self) -> f64;
+    /// Return a random char
+    fn gen_char(&self) -> char;
+    /**
+     * Return a char randomly chosen from chars, failing if chars is empty
+     */
+    fn gen_char_from(&self, chars: &str) -> char;
+    /// Return a random bool
+    fn gen_bool(&self) -> bool;
+    /// Return a bool with a 1 in n chance of true
+    fn gen_weighted_bool(&self, n: uint) -> bool;
+    /**
+     * Return a random string of the specified length composed of A-Z,a-z,0-9
+     */
+    fn gen_str(&self, len: uint) -> ~str;
+    /// Return a random byte string of the specified length
+    fn gen_bytes(&self, len: uint) -> ~[u8];
+    /// Choose an item randomly, failing if values is empty
+    fn choose<T:Copy>(&self, values: &[T]) -> T;
+    /// Choose Some(item) randomly, returning None if values is empty
+    fn choose_option<T:Copy>(&self, values: &[T]) -> Option<T>;
+    /**
+     * Choose an item respecting the relative weights, failing if the sum of
+     * the weights is 0
+     */
+    fn choose_weighted<T:Copy>(&self, v : &[Weighted<T>]) -> T;
+    /**
+     * Choose Some(item) respecting the relative weights, returning none if
+     * the sum of the weights is 0
+     */
+    fn choose_weighted_option<T:Copy>(&self, v: &[Weighted<T>]) -> Option<T>;
+    /**
+     * Return a vec containing copies of the items, in order, where
+     * the weight of the item determines how many copies there are
+     */
+    fn weighted_vec<T:Copy>(&self, v: &[Weighted<T>]) -> ~[T];
+    /// Shuffle a vec
+    fn shuffle<T:Copy>(&self, values: &[T]) -> ~[T];
+    /// Shuffle a mutable vec in place
+    fn shuffle_mut<T>(&self, values: &mut [T]);
+}
+
 /// Extension methods for random number generators
-pub impl Rng {
+impl RngUtil for @Rng {
     /// Return a random value for a Rand type
     fn gen<T:Rand>(&self) -> T {
         Rand::rand(*self)
@@ -407,7 +485,7 @@ pub fn seed() -> ~[u8] {
 }
 
 /// Create a random number generator with a system specified seed
-pub fn Rng() -> Rng {
+pub fn Rng() -> @Rng {
     seeded_rng(seed())
 }
 
@@ -449,7 +527,7 @@ impl Rng for XorShiftState {
     }
 }
 
-pub pure fn xorshift() -> Rng {
+pub pure fn xorshift() -> @Rng {
     // constants taken from http://en.wikipedia.org/wiki/Xorshift
     seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
 }
@@ -467,7 +545,7 @@ fn tls_rng_state(_v: @RandRes) {}
  * seeded by the system. Intended to be used in method chaining style, ie
  * task_rng().gen_int().
  */
-pub fn task_rng() -> Rng {
+pub fn task_rng() -> @Rng {
     let r : Option<@RandRes>;
     unsafe {
         r = task::local_data::local_data_get(tls_rng_state);
diff --git a/src/libcore/repr.rs b/src/libcore/repr.rs
index ad85c5e5cef..e6a4a99df44 100644
--- a/src/libcore/repr.rs
+++ b/src/libcore/repr.rs
@@ -41,7 +41,7 @@ trait EscapedCharWriter {
     fn write_escaped_char(&self, ch: char);
 }
 
-impl EscapedCharWriter for Writer {
+impl EscapedCharWriter for @Writer {
     fn write_escaped_char(&self, ch: char) {
         match ch {
             '\t' => self.write_str("\\t"),
diff --git a/src/libcore/run.rs b/src/libcore/run.rs
index 0d027ba1163..d3affbc69fe 100644
--- a/src/libcore/run.rs
+++ b/src/libcore/run.rs
@@ -45,13 +45,13 @@ pub trait Program {
     fn get_id(&mut self) -> pid_t;
 
     /// Returns an io::writer that can be used to write to stdin
-    fn input(&mut self) -> io::Writer;
+    fn input(&mut self) -> @io::Writer;
 
     /// Returns an io::reader that can be used to read from stdout
-    fn output(&mut self) -> io::Reader;
+    fn output(&mut self) -> @io::Reader;
 
     /// Returns an io::reader that can be used to read from stderr
-    fn err(&mut self) -> io::Reader;
+    fn err(&mut self) -> @io::Reader;
 
     /// Closes the handle to the child processes standard input
     fn close_input(&mut self);
@@ -207,7 +207,7 @@ pub fn run_program(prog: &str, args: &[~str]) -> int {
  *
  * A class with a <program> field
  */
-pub 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();
@@ -274,13 +274,13 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {
 
     impl Program for ProgRes {
         fn get_id(&mut self) -> pid_t { return self.r.pid; }
-        fn input(&mut self) -> io::Writer {
+        fn input(&mut self) -> @io::Writer {
             io::fd_writer(self.r.in_fd, false)
         }
-        fn output(&mut self) -> io::Reader {
+        fn output(&mut self) -> @io::Reader {
             io::FILE_reader(self.r.out_file, false)
         }
-        fn err(&mut self) -> io::Reader {
+        fn err(&mut self) -> @io::Reader {
             io::FILE_reader(self.r.err_file, false)
         }
         fn close_input(&mut self) { close_repr_input(&mut self.r); }
diff --git a/src/libcore/task/local_data_priv.rs b/src/libcore/task/local_data_priv.rs
index bb05520e1a3..6a933ef515f 100644
--- a/src/libcore/task/local_data_priv.rs
+++ b/src/libcore/task/local_data_priv.rs
@@ -23,7 +23,7 @@ use super::rt::rust_task;
 pub trait LocalData { }
 impl<T:Durable> LocalData for @T { }
 
-impl Eq for LocalData {
+impl Eq for @LocalData {
     pure fn eq(&self, other: &@LocalData) -> bool {
         unsafe {
             let ptr_a: (uint, uint) = cast::reinterpret_cast(&(*self));
@@ -36,7 +36,7 @@ impl Eq for LocalData {
 
 // If TLS is used heavily in future, this could be made more efficient with a
 // proper map.
-type TaskLocalElement = (*libc::c_void, *libc::c_void, LocalData);
+type TaskLocalElement = (*libc::c_void, *libc::c_void, @LocalData);
 // Has to be a pointer at outermost layer; the foreign call returns void *.
 type TaskLocalMap = @mut ~[Option<TaskLocalElement>];
 
diff --git a/src/libcore/unstable/at_exit.rs b/src/libcore/unstable/at_exit.rs
index f878161eca1..99ba5030f83 100644
--- a/src/libcore/unstable/at_exit.rs
+++ b/src/libcore/unstable/at_exit.rs
@@ -8,12 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use sys;
 use cast;
+use libc::{c_void, size_t};
+use rand::RngUtil;
+use rand;
+use sys;
 use task;
 use vec;
-use rand;
-use libc::{c_void, size_t};
 
 #[cfg(test)] use uint;