From fe43d6661387383db067c711e624a77df30004a2 Mon Sep 17 00:00:00 2001 From: Gareth Daniel Smith Date: Mon, 16 Jul 2012 22:32:59 +0100 Subject: [PATCH] replace core::tuple functions with methods --- src/libcore/core.rs | 1 + src/libcore/task.rs | 2 +- src/libcore/tuple.rs | 39 +++++++++++++----------- src/libstd/sort.rs | 4 +-- src/rustdoc/attr_pass.rs | 4 +-- src/rustdoc/config.rs | 4 +-- src/rustdoc/markdown_pass.rs | 4 +-- src/test/bench/task-perf-alloc-unwind.rs | 5 ++- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/libcore/core.rs b/src/libcore/core.rs index 4cadf018df823..c70be013fb74d 100644 --- a/src/libcore/core.rs +++ b/src/libcore/core.rs @@ -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; diff --git a/src/libcore/task.rs b/src/libcore/task.rs index b9a642c16b513..0efc15cda2ad4 100644 --- a/src/libcore/task.rs +++ b/src/libcore/task.rs @@ -878,7 +878,7 @@ unsafe fn key_to_key_value(key: local_data_key) -> *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! diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index fe6f15f5d6909..0724d01afaf49 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -1,28 +1,33 @@ //! Operations on tuples -/// Return the first element of a pair -pure fn first(pair: (T, U)) -> T { - let (t, _) = pair; - ret t; -} -/// Return the second element of a pair -pure fn second(pair: (T, U)) -> U { - let (_, u) = pair; - ret u; -} +impl extensions 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(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'); } diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index e0adae610ba32..6558e14c84dfb 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -22,8 +22,8 @@ fn merge_sort(le: le, v: ~[const T]) -> ~[T] { fn merge_sort_(le: le, 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 ~[]; } diff --git a/src/rustdoc/attr_pass.rs b/src/rustdoc/attr_pass.rs index c884c40e70fbc..edec940417f84 100644 --- a/src/rustdoc/attr_pass.rs +++ b/src/rustdoc/attr_pass.rs @@ -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 diff --git a/src/rustdoc/config.rs b/src/rustdoc/config.rs index 10bcbdb140459..f6be9dd7b5461 100644 --- a/src/rustdoc/config.rs +++ b/src/rustdoc/config.rs @@ -62,7 +62,7 @@ fn usage() { println(~"Usage: rustdoc ~[options] \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 { 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 { diff --git a/src/rustdoc/markdown_pass.rs b/src/rustdoc/markdown_pass.rs index 9c36af9237bf3..64b6f9ab1f6f4 100644 --- a/src/rustdoc/markdown_pass.rs +++ b/src/rustdoc/markdown_pass.rs @@ -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] diff --git a/src/test/bench/task-perf-alloc-unwind.rs b/src/test/bench/task-perf-alloc-unwind.rs index 240febf8f25fb..64959988eb331 100644 --- a/src/test/bench/task-perf-alloc-unwind.rs +++ b/src/test/bench/task-perf-alloc-unwind.rs @@ -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) { 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)) })