Skip to content

Commit

Permalink
replace core::tuple functions with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Dretch committed Jul 16, 2012
1 parent 000d12f commit fe43d66
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/libcore/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import option::{some, none};
import option = option::option;
import path = path::path;
import str::extensions;
import tuple::extensions;
import vec::extensions;
import option::extensions;
import option_iter::extensions;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ unsafe fn key_to_key_value<T>(key: local_data_key<T>) -> *libc::c_void {
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
// Use reintepret_cast -- transmute would leak (forget) the closure.
let pair: (*libc::c_void, *libc::c_void) = unsafe::reinterpret_cast(key);
tuple::first(pair)
pair.first()
}

// If returning some(..), returns with @T with the map's reference. Careful!
Expand Down
39 changes: 22 additions & 17 deletions src/libcore/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
//! Operations on tuples
/// Return the first element of a pair
pure fn first<T:copy, U:copy>(pair: (T, U)) -> T {
let (t, _) = pair;
ret t;
}

/// Return the second element of a pair
pure fn second<T:copy, U:copy>(pair: (T, U)) -> U {
let (_, u) = pair;
ret u;
}
impl extensions <T:copy, U:copy> for (T, U) {

/// Return the first element of self
pure fn first() -> T {
let (t, _) = self;
ret t;
}

/// Return the second element of self
pure fn second() -> U {
let (_, u) = self;
ret u;
}

/// Return the results of swapping the two elements of self
pure fn swap() -> (U, T) {
let (t, u) = self;
ret (u, t);
}

/// Return the results of swapping the two elements of a pair
pure fn swap<T:copy, U:copy>(pair: (T, U)) -> (U, T) {
let (t, u) = pair;
ret (u, t);
}


#[test]
fn test_tuple() {
assert first((948, 4039.48)) == 948;
assert second((34.5, ~"foo")) == ~"foo";
assert swap(('a', 2)) == (2, 'a');
assert (948, 4039.48).first() == 948;
assert (34.5, ~"foo").second() == ~"foo";
assert ('a', 2).swap() == (2, 'a');
}

4 changes: 2 additions & 2 deletions src/libstd/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn merge_sort<T: copy>(le: le<T>, v: ~[const T]) -> ~[T] {

fn merge_sort_<T: copy>(le: le<T>, v: ~[const T], slice: slice)
-> ~[T] {
let begin = tuple::first(slice);
let end = tuple::second(slice);
let begin = slice.first();
let end = slice.second();

let v_len = end - begin;
if v_len == 0u { ret ~[]; }
Expand Down
4 changes: 2 additions & 2 deletions src/rustdoc/attr_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ fn merge_method_attrs(
};

do vec::map2(docs, attrs) |doc, attrs| {
assert doc.name == tuple::first(attrs);
let desc = tuple::second(attrs);
assert doc.name == attrs.first();
let desc = attrs.second();

{
desc: desc
Expand Down
4 changes: 2 additions & 2 deletions src/rustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn usage() {
println(~"Usage: rustdoc ~[options] <cratefile>\n");
println(~"Options:\n");
for opts().each |opt| {
println(#fmt(" %s", tuple::second(opt)));
println(#fmt(" %s", opt.second()));
}
println(~"");
}
Expand Down Expand Up @@ -99,7 +99,7 @@ fn parse_config_(
program_output: program_output
) -> result<config, ~str> {
let args = vec::tail(args);
let opts = tuple::first(vec::unzip(opts()));
let opts = vec::unzip(opts()).first();
alt getopts::getopts(args, opts) {
result::ok(match) {
if vec::len(match.free) == 1u {
Expand Down
4 changes: 2 additions & 2 deletions src/rustdoc/markdown_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ mod test {
) -> ~str {
let (writer_factory, po) = markdown_writer::future_writer_factory();
write_markdown(doc, writer_factory);
ret tuple::second(comm::recv(po));
ret comm::recv(po).second();
}

fn write_markdown_str_srv(
Expand All @@ -811,7 +811,7 @@ mod test {
let (writer_factory, po) = markdown_writer::future_writer_factory();
let pass = mk_pass(writer_factory);
pass.f(srv, doc);
ret tuple::second(comm::recv(po));
ret comm::recv(po).second();
}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/test/bench/task-perf-alloc-unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std;

import tuple::{first, second};
import std::list::{list, cons, nil};
import std::time::precise_time_s;

Expand Down Expand Up @@ -75,8 +74,8 @@ fn recurse_or_fail(depth: int, st: option<st>) {
unique: ~cons((), @*st.unique),
fn_box: fn@() -> @nillist { @cons((), fn_box()) },
fn_unique: fn~() -> ~nillist { ~cons((), @*fn_unique()) },
tuple: (@cons((), first(st.tuple)),
~cons((), @*second(st.tuple))),
tuple: (@cons((), st.tuple.first()),
~cons((), @*st.tuple.second())),
vec: st.vec + ~[@cons((), st.vec.last())],
res: r(@cons((), st.res._l))
})
Expand Down

0 comments on commit fe43d66

Please sign in to comment.