Skip to content

Commit

Permalink
Overhaul constructor naming in libs
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Mar 13, 2012
1 parent aea8736 commit cd72b1f
Show file tree
Hide file tree
Showing 65 changed files with 227 additions and 227 deletions.
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Expand Up @@ -226,7 +226,7 @@ fn check_expected_errors(expected_errors: [errors::expected_error],
procres: procres) {

// true if we found the error in question
let found_flags = vec::to_mut(vec::init_elt(
let found_flags = vec::to_mut(vec::from_elem(
vec::len(expected_errors), false));

if procres.status == 0 {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/extfmt.rs
Expand Up @@ -385,7 +385,7 @@ mod rt {

// FIXME: This might be useful in str: but needs to be utf8 safe first
fn str_init_elt(n_elts: uint, c: char) -> str {
let svec = vec::init_elt::<u8>(n_elts, c as u8);
let svec = vec::from_elem::<u8>(n_elts, c as u8);

ret str::from_bytes(svec);
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/os.rs
Expand Up @@ -64,7 +64,7 @@ fn as_c_charp<T>(s: str, f: fn(*c_char) -> T) -> T {

fn fill_charp_buf(f: fn(*mutable c_char, size_t) -> bool)
-> option<str> {
let buf = vec::to_mut(vec::init_elt(tmpbuf_sz, 0u8 as c_char));
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
vec::as_mut_buf(buf) { |b|
if f(b, tmpbuf_sz as size_t) {
some(str::from_cstr(b as str::sbuf))
Expand All @@ -89,7 +89,7 @@ mod win32 {
let mut res = none;
let mut done = false;
while !done {
let buf = vec::to_mut(vec::init_elt(n, 0u16));
let buf = vec::to_mut(vec::from_elem(n, 0u16));
vec::as_mut_buf(buf) {|b|
let k : dword = f(b, tmpbuf_sz as dword);
if k == (0 as dword) {
Expand Down Expand Up @@ -588,7 +588,7 @@ mod tests {

fn make_rand_name() -> str {
import rand;
let rng: rand::rng = rand::mk_rng();
let rng: rand::rng = rand::rng();
let n = "TEST" + rng.gen_str(10u);
assert option::is_none(getenv(n));
n
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/rand.rs
Expand Up @@ -27,7 +27,7 @@ iface rng {
resource rand_res(c: *rctx) { rustrt::rand_free(c); }

#[doc = "Create a random number generator"]
fn mk_rng() -> rng {
fn rng() -> rng {
impl of rng for @rand_res {
fn next() -> u32 { ret rustrt::rand_next(**self); }
fn next_float() -> float {
Expand Down Expand Up @@ -70,11 +70,11 @@ mod tests {

#[test]
fn test() {
let r1: rand::rng = rand::mk_rng();
let r1: rand::rng = rand::rng();
log(debug, r1.next());
log(debug, r1.next());
{
let r2 = rand::mk_rng();
let r2 = rand::rng();
log(debug, r1.next());
log(debug, r2.next());
log(debug, r1.next());
Expand All @@ -94,7 +94,7 @@ mod tests {

#[test]
fn genstr() {
let r: rand::rng = rand::mk_rng();
let r: rand::rng = rand::rng();
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
Expand Down
47 changes: 24 additions & 23 deletions src/libcore/task.rs
Expand Up @@ -31,7 +31,6 @@ export task_opts;
export task_builder::{};

export default_task_opts;
export mk_task_builder;
export get_opts;
export set_opts;
export add_wrapper;
Expand Down Expand Up @@ -154,11 +153,13 @@ Provides detailed control over the properties and behavior of new tasks.
// when you try to reuse the builder to spawn a new task. We'll just
// sidestep that whole issue by making builder's uncopyable and making
// the run function move them in.
enum task_builder = {
mutable opts: task_opts,
mutable gen_body: fn@(+fn~()) -> fn~(),
can_not_copy: option<comm::port<()>>
};
enum task_builder {
task_builder_({
mutable opts: task_opts,
mutable gen_body: fn@(+fn~()) -> fn~(),
can_not_copy: option<comm::port<()>>
})
}


/* Task construction */
Expand All @@ -178,12 +179,12 @@ fn default_task_opts() -> task_opts {
}
}

fn mk_task_builder() -> task_builder {
fn task_builder() -> task_builder {
#[doc = "Construct a task_builder"];

let body_identity = fn@(+body: fn~()) -> fn~() { body };

task_builder({
task_builder_({
mutable opts: default_task_opts(),
mutable gen_body: body_identity,
can_not_copy: none
Expand Down Expand Up @@ -345,10 +346,10 @@ fn spawn(+f: fn~()) {
Sets up a new task with its own call stack and schedules it to run
the provided unique closure.
This function is equivalent to `run(mk_task_builder(), f)`.
This function is equivalent to `run(new_task_builder(), f)`.
"];

run(mk_task_builder(), f);
run(task_builder(), f);
}

fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
Expand All @@ -374,10 +375,10 @@ fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
};
// Likewise, the parent has both a 'po' and 'ch'
This function is equivalent to `run_listener(mk_task_builder(), f)`.
This function is equivalent to `run_listener(new_task_builder(), f)`.
"];

run_listener(mk_task_builder(), f)
run_listener(task_builder(), f)
}

fn spawn_sched(mode: sched_mode, +f: fn~()) {
Expand All @@ -394,7 +395,7 @@ fn spawn_sched(mode: sched_mode, +f: fn~()) {
greater than zero.
"];

let mut builder = mk_task_builder();
let mut builder = task_builder();
set_opts(builder, {
sched: some({
mode: mode,
Expand All @@ -419,7 +420,7 @@ fn try<T:send>(+f: fn~() -> T) -> result::t<T,()> {

let po = comm::port();
let ch = comm::chan(po);
let mut builder = mk_task_builder();
let mut builder = task_builder();
unsupervise(builder);
let result = future_result(builder);
run(builder) {||
Expand Down Expand Up @@ -612,7 +613,7 @@ fn test_spawn_raw_notify() {
fn test_run_basic() {
let po = comm::port();
let ch = comm::chan(po);
let builder = mk_task_builder();
let builder = task_builder();
run(builder) {||
comm::send(ch, ());
}
Expand All @@ -623,7 +624,7 @@ fn test_run_basic() {
fn test_add_wrapper() {
let po = comm::port();
let ch = comm::chan(po);
let builder = mk_task_builder();
let builder = task_builder();
add_wrapper(builder) {|body|
fn~() {
body();
Expand All @@ -637,12 +638,12 @@ fn test_add_wrapper() {
#[test]
#[ignore(cfg(target_os = "win32"))]
fn test_future_result() {
let builder = mk_task_builder();
let builder = task_builder();
let result = future_result(builder);
run(builder) {||}
assert future::get(result) == success;

let builder = mk_task_builder();
let builder = task_builder();
let result = future_result(builder);
unsupervise(builder);
run(builder) {|| fail }
Expand All @@ -653,7 +654,7 @@ fn test_future_result() {
fn test_future_task() {
let po = comm::port();
let ch = comm::chan(po);
let builder = mk_task_builder();
let builder = task_builder();
let task1 = future_task(builder);
run(builder) {|| comm::send(ch, get_task()) }
assert future::get(task1) == comm::recv(po);
Expand Down Expand Up @@ -849,7 +850,7 @@ fn test_avoid_copying_the_body_spawn_listener() {
#[test]
fn test_avoid_copying_the_body_run() {
avoid_copying_the_body {|f|
let builder = mk_task_builder();
let builder = task_builder();
run(builder) {||
f();
}
Expand All @@ -859,7 +860,7 @@ fn test_avoid_copying_the_body_run() {
#[test]
fn test_avoid_copying_the_body_run_listener() {
avoid_copying_the_body {|f|
let builder = mk_task_builder();
let builder = task_builder();
run_listener(builder, fn~[move f](_po: comm::port<int>) {
f();
});
Expand All @@ -878,7 +879,7 @@ fn test_avoid_copying_the_body_try() {
#[test]
fn test_avoid_copying_the_body_future_task() {
avoid_copying_the_body {|f|
let builder = mk_task_builder();
let builder = task_builder();
future_task(builder);
run(builder) {||
f();
Expand All @@ -889,7 +890,7 @@ fn test_avoid_copying_the_body_future_task() {
#[test]
fn test_avoid_copying_the_body_unsupervise() {
avoid_copying_the_body {|f|
let builder = mk_task_builder();
let builder = task_builder();
unsupervise(builder);
run(builder) {||
f();
Expand Down
30 changes: 15 additions & 15 deletions src/libcore/vec.rs
Expand Up @@ -8,8 +8,8 @@ export is_not_empty;
export same_length;
export reserve;
export len;
export init_fn;
export init_elt;
export from_fn;
export from_elem;
export to_mut;
export from_mut;
export head;
Expand Down Expand Up @@ -129,7 +129,7 @@ Creates and initializes an immutable vector.
Creates an immutable vector of size `n_elts` and initializes the elements
to the value returned by the function `op`.
"]
fn init_fn<T>(n_elts: uint, op: init_op<T>) -> [T] {
fn from_fn<T>(n_elts: uint, op: init_op<T>) -> [T] {
let mut v = [];
reserve(v, n_elts);
let mut i: uint = 0u;
Expand All @@ -143,7 +143,7 @@ Creates and initializes an immutable vector.
Creates an immutable vector of size `n_elts` and initializes the elements
to the value `t`.
"]
fn init_elt<T: copy>(n_elts: uint, t: T) -> [T] {
fn from_elem<T: copy>(n_elts: uint, t: T) -> [T] {
let mut v = [];
reserve(v, n_elts);
let mut i: uint = 0u;
Expand Down Expand Up @@ -182,7 +182,7 @@ fn tailn<T: copy>(v: [const T], n: uint) -> [T] {
slice(v, n, len(v))
}

// FIXME: This name is sort of confusing next to init_fn, etc
// FIXME: This name is sort of confusing next to from_fn, etc
// but this is the name haskell uses for this function,
// along with head/tail/last.
#[doc = "Returns all but the last elemnt of a vector"]
Expand Down Expand Up @@ -999,16 +999,16 @@ mod tests {
}

#[test]
fn test_init_fn() {
// Test on-stack init_fn.
let v = init_fn(3u, square);
fn test_from_fn() {
// Test on-stack from_fn.
let v = from_fn(3u, square);
assert (len(v) == 3u);
assert (v[0] == 0u);
assert (v[1] == 1u);
assert (v[2] == 4u);

// Test on-heap init_fn.
v = init_fn(5u, square);
// Test on-heap from_fn.
v = from_fn(5u, square);
assert (len(v) == 5u);
assert (v[0] == 0u);
assert (v[1] == 1u);
Expand All @@ -1018,15 +1018,15 @@ mod tests {
}

#[test]
fn test_init_elt() {
// Test on-stack init_elt.
let v = init_elt(2u, 10u);
fn test_from_elem() {
// Test on-stack from_elem.
let v = from_elem(2u, 10u);
assert (len(v) == 2u);
assert (v[0] == 10u);
assert (v[1] == 10u);

// Test on-heap init_elt.
v = init_elt(6u, 20u);
// Test on-heap from_elem.
v = from_elem(6u, 20u);
assert (v[0] == 20u);
assert (v[1] == 20u);
assert (v[2] == 20u);
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/bitv.rs
Expand Up @@ -37,7 +37,7 @@ Constructs a bitvector
"]
fn create(nbits: uint, init: bool) -> t {
let elt = if init { !0u } else { 0u };
let storage = vec::to_mut(vec::init_elt(nbits / uint_bits + 1u, elt));
let storage = vec::to_mut(vec::from_elem(nbits / uint_bits + 1u, elt));
ret @{storage: storage, nbits: nbits};
}

Expand Down Expand Up @@ -84,7 +84,7 @@ fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(v0, v1, sub); }

#[doc = "Makes a copy of a bitvector"]
fn clone(v: t) -> t {
let storage = vec::to_mut(vec::init_elt(v.nbits / uint_bits + 1u, 0u));
let storage = vec::to_mut(vec::from_elem(v.nbits / uint_bits + 1u, 0u));
let len = vec::len(v.storage);
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
ret @{storage: storage, nbits: v.nbits};
Expand Down Expand Up @@ -190,7 +190,7 @@ Each uint in the resulting vector has either value 0u or 1u.
"]
fn to_vec(v: t) -> [uint] {
let sub = bind init_to_vec(v, _);
ret vec::init_fn::<uint>(v.nbits, sub);
ret vec::from_fn::<uint>(v.nbits, sub);
}


Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
let act;
let exp;
act = create(0u, false);
exp = vec::init_elt::<uint>(0u, 0u);
exp = vec::from_elem::<uint>(0u, 0u);
assert (eq_vec(act, exp));
}

Expand Down
10 changes: 5 additions & 5 deletions src/libstd/c_vec.rs
Expand Up @@ -26,7 +26,7 @@ taken to ensure that a reference to the c_vec::t is still held if needed.
"];

export t;
export create, create_with_dtor;
export c_vec, c_vec_with_dtor;
export get, set;
export len;
export ptr;
Expand Down Expand Up @@ -60,7 +60,7 @@ Create a c_vec::t from a native buffer with a given length.
* base - A native pointer to a buffer
* len - The number of elements in the buffer
"]
unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
unsafe fn c_vec<T>(base: *mutable T, len: uint) -> t<T> {
ret t({base: base,
len: len,
rsrc: @dtor_res(option::none)
Expand All @@ -78,7 +78,7 @@ and a function to run upon destruction.
* dtor - A function to run when the value is destructed, useful
for freeing the buffer, etc.
"]
unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
unsafe fn c_vec_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
-> t<T> {
ret t({base: base,
len: len,
Expand Down Expand Up @@ -133,8 +133,8 @@ mod tests {

assert mem as int != 0;

ret unsafe { create_with_dtor(mem as *mutable u8, n,
bind free(mem)) };
ret unsafe { c_vec_with_dtor(mem as *mutable u8, n,
bind free(mem)) };
}

#[test]
Expand Down

0 comments on commit cd72b1f

Please sign in to comment.