Skip to content

Commit 0bb6f0c

Browse files
authored
Rollup merge of rust-lang#85835 - Seppel3210:master, r=yaahc
Implement Extend<(A, B)> for (Extend<A>, Extend<B>) I oriented myself at the implementation of `Iterator::unzip` and also rewrote the impl in terms of `(A, B)::extend` after that. Since (A, B) now also implements Extend we could also mention in the documentation of unzip that it can do "nested unzipping" (you could unzip `Iterator<Item=(A, (B, C))>` into `(Vec<A>, (Vec<B>, Vec<C>))` for example) but I'm not sure of that so I'm asking here 🙂 (P.S. I saw a couple of people asking if there is an unzip3 but there isn't. So this could be a way to get equivalent functionality)
2 parents 362e0f5 + 3d0c5d0 commit 0bb6f0c

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

library/core/src/iter/traits/collect.rs

+58
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,61 @@ impl Extend<()> for () {
360360
}
361361
fn extend_one(&mut self, _item: ()) {}
362362
}
363+
364+
#[stable(feature = "extend_for_tuple", since = "1.56.0")]
365+
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
366+
where
367+
ExtendA: Extend<A>,
368+
ExtendB: Extend<B>,
369+
{
370+
/// Allows to `extend` a tuple of collections that also implement `Extend`.
371+
///
372+
/// See also: [`Iterator::unzip`]
373+
///
374+
/// # Examples
375+
/// ```
376+
/// let mut tuple = (vec![0], vec![1]);
377+
/// tuple.extend(vec![(2, 3), (4, 5), (6, 7)]);
378+
/// assert_eq!(tuple.0, vec![0, 2, 4, 6]);
379+
/// assert_eq!(tuple.1, vec![1, 3, 5, 7]);
380+
///
381+
/// // also allows for arbitrarily nested tuples
382+
/// let mut nested_tuple = (vec![(1, -1)], vec![(2, -2)]);
383+
/// nested_tuple.extend(vec![((3, -3), (4, -4)), ((5, -5), (6, -6))]);
384+
///
385+
/// assert_eq!(nested_tuple.0, vec![(1, -1), (3, -3), (5, -5)]);
386+
/// assert_eq!(nested_tuple.1, vec![(2, -2), (4, -4), (6, -6)]);
387+
/// ```
388+
fn extend<T: IntoIterator<Item = (A, B)>>(&mut self, into_iter: T) {
389+
let (a, b) = self;
390+
let iter = into_iter.into_iter();
391+
392+
fn extend<'a, A, B>(
393+
a: &'a mut impl Extend<A>,
394+
b: &'a mut impl Extend<B>,
395+
) -> impl FnMut((), (A, B)) + 'a {
396+
move |(), (t, u)| {
397+
a.extend_one(t);
398+
b.extend_one(u);
399+
}
400+
}
401+
402+
let (lower_bound, _) = iter.size_hint();
403+
if lower_bound > 0 {
404+
a.extend_reserve(lower_bound);
405+
b.extend_reserve(lower_bound);
406+
}
407+
408+
iter.fold((), extend(a, b));
409+
}
410+
411+
fn extend_one(&mut self, item: (A, B)) {
412+
self.0.extend_one(item.0);
413+
self.1.extend_one(item.1);
414+
}
415+
416+
fn extend_reserve(&mut self, additional: usize) {
417+
self.0.extend_reserve(additional);
418+
self.1.extend_reserve(additional);
419+
}
420+
}

library/core/src/iter/traits/iterator.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,14 @@ pub trait Iterator {
28412841
///
28422842
/// assert_eq!(left, [1, 3]);
28432843
/// assert_eq!(right, [2, 4]);
2844+
///
2845+
/// // you can also unzip multiple nested tuples at once
2846+
/// let a = [(1, (2, 3)), (4, (5, 6))];
2847+
///
2848+
/// let (x, (y, z)): (Vec<_>, (Vec<_>, Vec<_>)) = a.iter().cloned().unzip();
2849+
/// assert_eq!(x, [1, 4]);
2850+
/// assert_eq!(y, [2, 5]);
2851+
/// assert_eq!(z, [3, 6]);
28442852
/// ```
28452853
#[stable(feature = "rust1", since = "1.0.0")]
28462854
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
@@ -2849,28 +2857,9 @@ pub trait Iterator {
28492857
FromB: Default + Extend<B>,
28502858
Self: Sized + Iterator<Item = (A, B)>,
28512859
{
2852-
fn extend<'a, A, B>(
2853-
ts: &'a mut impl Extend<A>,
2854-
us: &'a mut impl Extend<B>,
2855-
) -> impl FnMut((), (A, B)) + 'a {
2856-
move |(), (t, u)| {
2857-
ts.extend_one(t);
2858-
us.extend_one(u);
2859-
}
2860-
}
2861-
2862-
let mut ts: FromA = Default::default();
2863-
let mut us: FromB = Default::default();
2864-
2865-
let (lower_bound, _) = self.size_hint();
2866-
if lower_bound > 0 {
2867-
ts.extend_reserve(lower_bound);
2868-
us.extend_reserve(lower_bound);
2869-
}
2870-
2871-
self.fold((), extend(&mut ts, &mut us));
2872-
2873-
(ts, us)
2860+
let mut unzipped: (FromA, FromB) = Default::default();
2861+
unzipped.extend(self);
2862+
unzipped
28742863
}
28752864

28762865
/// Creates an iterator which copies all of its elements.

0 commit comments

Comments
 (0)