Skip to content

Commit 6212729

Browse files
author
blake2-ppc
committed
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())`
1 parent 79e78c4 commit 6212729

File tree

3 files changed

+14
-32
lines changed

3 files changed

+14
-32
lines changed

src/librustdoc/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn parse_config_(
112112
process_output: Process
113113
) -> Result<Config, ~str> {
114114
let args = args.tail();
115-
let opts = vec::unzip(opts()).first();
115+
let opts = vec::unzip(opts().move_iter()).first();
116116
match getopts::getopts(args, opts) {
117117
Ok(matches) => {
118118
if matches.free.len() == 1 {

src/libstd/select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ mod test {
148148
// Unfortunately this does not actually test the block_on early-break
149149
// codepath in select -- racing between the sender and the receiver in
150150
// separate tasks is necessary to get around the optimistic check.
151-
let (ports, chans) = unzip(from_fn(num_ports, |_| oneshot::<()>()));
151+
let (ports, chans) = unzip(range(0, num_ports).map(|_| oneshot::<()>()));
152152
let mut dead_chans = ~[];
153153
let mut ports = ports;
154154
for (i, chan) in chans.move_iter().enumerate() {
@@ -165,7 +165,7 @@ mod test {
165165

166166
// Same thing with streams instead.
167167
// FIXME(#7971): This should be in a macro but borrowck isn't smart enough.
168-
let (ports, chans) = unzip(from_fn(num_ports, |_| stream::<()>()));
168+
let (ports, chans) = unzip(range(0, num_ports).map(|_| stream::<()>()));
169169
let mut dead_chans = ~[];
170170
let mut ports = ports;
171171
for (i, chan) in chans.move_iter().enumerate() {
@@ -209,7 +209,7 @@ mod test {
209209
// Sends 10 buffered packets, and uses select to retrieve them all.
210210
// Puts the port in a different spot in the vector each time.
211211
do run_in_newsched_task {
212-
let (ports, _) = unzip(from_fn(10, |_| stream()));
212+
let (ports, _) = unzip(range(0u, 10).map(|_| stream::<int>()));
213213
let (port, chan) = stream();
214214
do 10.times { chan.send(31337); }
215215
let mut ports = ports;
@@ -327,7 +327,7 @@ mod test {
327327
let (p,c) = oneshot();
328328
let c = Cell::new(c);
329329
do task::spawn {
330-
let (dead_ps, dead_cs) = unzip(from_fn(5, |_| oneshot::<()>()));
330+
let (dead_ps, dead_cs) = unzip(range(0u, 5).map(|_| oneshot::<()>()));
331331
let mut ports = dead_ps;
332332
select(ports); // should get killed; nothing should leak
333333
c.take().send(()); // must not happen

src/libstd/vec.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -389,37 +389,19 @@ impl<'self,T:Clone> VectorVector<T> for &'self [&'self [T]] {
389389
}
390390
}
391391

392-
// FIXME: if issue #586 gets implemented, could have a postcondition
393-
// saying the two result lists have the same length -- or, could
394-
// return a nominal record with a constraint saying that, instead of
395-
// returning a tuple (contingent on issue #869)
396392
/**
397-
* Convert a vector of pairs into a pair of vectors, by reference. As unzip().
398-
*/
399-
pub fn unzip_slice<T:Clone,U:Clone>(v: &[(T, U)]) -> (~[T], ~[U]) {
400-
let mut ts = ~[];
401-
let mut us = ~[];
402-
for p in v.iter() {
403-
let (t, u) = (*p).clone();
404-
ts.push(t);
405-
us.push(u);
406-
}
407-
(ts, us)
408-
}
409-
410-
/**
411-
* Convert a vector of pairs into a pair of vectors.
393+
* Convert an iterator of pairs into a pair of vectors.
412394
*
413395
* Returns a tuple containing two vectors where the i-th element of the first
414-
* vector contains the first element of the i-th tuple of the input vector,
396+
* vector contains the first element of the i-th tuple of the input iterator,
415397
* and the i-th element of the second vector contains the second element
416-
* of the i-th tuple of the input vector.
398+
* of the i-th tuple of the input iterator.
417399
*/
418-
pub fn unzip<T,U>(v: ~[(T, U)]) -> (~[T], ~[U]) {
419-
let mut ts = ~[];
420-
let mut us = ~[];
421-
for p in v.move_iter() {
422-
let (t, u) = p;
400+
pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (~[T], ~[U]) {
401+
let (lo, _) = iter.size_hint();
402+
let mut ts = with_capacity(lo);
403+
let mut us = with_capacity(lo);
404+
for (t, u) in iter {
423405
ts.push(t);
424406
us.push(u);
425407
}
@@ -2891,7 +2873,7 @@ mod tests {
28912873
fn test_zip_unzip() {
28922874
let z1 = ~[(1, 4), (2, 5), (3, 6)];
28932875

2894-
let (left, right) = unzip(z1);
2876+
let (left, right) = unzip(z1.iter().map(|&x| x));
28952877

28962878
assert_eq!((1, 4), (left[0], right[0]));
28972879
assert_eq!((2, 5), (left[1], right[1]));

0 commit comments

Comments
 (0)