Merge pull request #2929 from Dretch/tuplemethods
replace core::tuple functions with methods
This commit is contained in:
commit
0487663e7d
8 changed files with 34 additions and 29 deletions
|
@ -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;
|
||||
|
|
|
@ -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!
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
@ -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 ~[]; }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(~"");
|
||||
}
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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(
|
||||
|
@ -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]
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
use std;
|
||||
|
||||
import tuple::{first, second};
|
||||
import std::list::{list, cons, nil};
|
||||
import std::time::precise_time_s;
|
||||
|
||||
|
@ -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))
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue