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

Bare traits 2 #5348

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ to pointers to the trait name, used as a type.
# impl Shape for int { }
# let mycircle = 0;

let myshape: Shape = @mycircle as @Shape;
let myshape: @Shape = @mycircle as @Shape;
~~~~

The resulting value is a managed box containing the value that was cast,
Expand Down
1 change: 1 addition & 0 deletions src/libcore/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/libcore/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 15 additions & 14 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1306,12 +1307,12 @@ mod tests {
~"A hoopy frood who really knows where his towel is.";
log(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();
log(debug, copy frood2);
fail_unless!(frood == frood2);
Expand Down
3 changes: 2 additions & 1 deletion src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ mod tests {
use os::{remove_file, setenv};
use os;
use path::Path;
use rand::RngUtil;
use rand;
use run;
use str;
Expand All @@ -1284,7 +1285,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
Expand Down
122 changes: 100 additions & 22 deletions src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading