about summary refs log tree commit diff
diff options
context:
space:
mode:
authorblake2-ppc <blake2-ppc>2013-09-09 04:46:32 +0200
committerblake2-ppc <blake2-ppc>2013-09-10 05:39:59 +0200
commit6212729315be2ac80785ffcecfe0a80c9955c4cf (patch)
tree1cf9b631d4734f71560f8c191f219c84d3e3b69a
parent79e78c4b0c49003c8191f7094651753ecfabfd24 (diff)
downloadrust-6212729315be2ac80785ffcecfe0a80c9955c4cf.tar.gz
rust-6212729315be2ac80785ffcecfe0a80c9955c4cf.zip
std::vec: Change fn unzip to take an iterator argument
Remove unzip_slice since it's redundant. Old unzip is equivalent to the
`|x| unzip(x.move_iter())`
-rw-r--r--src/librustdoc/config.rs2
-rw-r--r--src/libstd/select.rs8
-rw-r--r--src/libstd/vec.rs36
3 files changed, 14 insertions, 32 deletions
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index dd43e22fc0c..e6d80e1443b 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -112,7 +112,7 @@ pub fn parse_config_(
     process_output: Process
 ) -> Result<Config, ~str> {
     let args = args.tail();
-    let opts = vec::unzip(opts()).first();
+    let opts = vec::unzip(opts().move_iter()).first();
     match getopts::getopts(args, opts) {
         Ok(matches) => {
             if matches.free.len() == 1 {
diff --git a/src/libstd/select.rs b/src/libstd/select.rs
index 9f4fd8db788..695217a62f6 100644
--- a/src/libstd/select.rs
+++ b/src/libstd/select.rs
@@ -148,7 +148,7 @@ mod test {
         // Unfortunately this does not actually test the block_on early-break
         // codepath in select -- racing between the sender and the receiver in
         // separate tasks is necessary to get around the optimistic check.
-        let (ports, chans) = unzip(from_fn(num_ports, |_| oneshot::<()>()));
+        let (ports, chans) = unzip(range(0, num_ports).map(|_| oneshot::<()>()));
         let mut dead_chans = ~[];
         let mut ports = ports;
         for (i, chan) in chans.move_iter().enumerate() {
@@ -165,7 +165,7 @@ mod test {
 
         // Same thing with streams instead.
         // FIXME(#7971): This should be in a macro but borrowck isn't smart enough.
-        let (ports, chans) = unzip(from_fn(num_ports, |_| stream::<()>()));
+        let (ports, chans) = unzip(range(0, num_ports).map(|_| stream::<()>()));
         let mut dead_chans = ~[];
         let mut ports = ports;
         for (i, chan) in chans.move_iter().enumerate() {
@@ -209,7 +209,7 @@ mod test {
         // Sends 10 buffered packets, and uses select to retrieve them all.
         // Puts the port in a different spot in the vector each time.
         do run_in_newsched_task {
-            let (ports, _) = unzip(from_fn(10, |_| stream()));
+            let (ports, _) = unzip(range(0u, 10).map(|_| stream::<int>()));
             let (port, chan) = stream();
             do 10.times { chan.send(31337); }
             let mut ports = ports;
@@ -327,7 +327,7 @@ mod test {
                     let (p,c) = oneshot();
                     let c = Cell::new(c);
                     do task::spawn {
-                        let (dead_ps, dead_cs) = unzip(from_fn(5, |_| oneshot::<()>()));
+                        let (dead_ps, dead_cs) = unzip(range(0u, 5).map(|_| oneshot::<()>()));
                         let mut ports = dead_ps;
                         select(ports); // should get killed; nothing should leak
                         c.take().send(()); // must not happen
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 4cc5c4f14ff..10a0bf27836 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -389,37 +389,19 @@ impl<'self,T:Clone> VectorVector<T> for &'self [&'self [T]] {
     }
 }
 
-// FIXME: if issue #586 gets implemented, could have a postcondition
-// saying the two result lists have the same length -- or, could
-// return a nominal record with a constraint saying that, instead of
-// returning a tuple (contingent on issue #869)
 /**
- * Convert a vector of pairs into a pair of vectors, by reference. As unzip().
- */
-pub fn unzip_slice<T:Clone,U:Clone>(v: &[(T, U)]) -> (~[T], ~[U]) {
-    let mut ts = ~[];
-    let mut us = ~[];
-    for p in v.iter() {
-        let (t, u) = (*p).clone();
-        ts.push(t);
-        us.push(u);
-    }
-    (ts, us)
-}
-
-/**
- * Convert a vector of pairs into a pair of vectors.
+ * Convert an iterator of pairs into a pair of vectors.
  *
  * Returns a tuple containing two vectors where the i-th element of the first
- * vector contains the first element of the i-th tuple of the input vector,
+ * vector contains the first element of the i-th tuple of the input iterator,
  * and the i-th element of the second vector contains the second element
- * of the i-th tuple of the input vector.
+ * of the i-th tuple of the input iterator.
  */
-pub fn unzip<T,U>(v: ~[(T, U)]) -> (~[T], ~[U]) {
-    let mut ts = ~[];
-    let mut us = ~[];
-    for p in v.move_iter() {
-        let (t, u) = p;
+pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (~[T], ~[U]) {
+    let (lo, _) = iter.size_hint();
+    let mut ts = with_capacity(lo);
+    let mut us = with_capacity(lo);
+    for (t, u) in iter {
         ts.push(t);
         us.push(u);
     }
@@ -2891,7 +2873,7 @@ mod tests {
     fn test_zip_unzip() {
         let z1 = ~[(1, 4), (2, 5), (3, 6)];
 
-        let (left, right) = unzip(z1);
+        let (left, right) = unzip(z1.iter().map(|&x| x));
 
         assert_eq!((1, 4), (left[0], right[0]));
         assert_eq!((2, 5), (left[1], right[1]));