2020-09-18 09:51:26 +02:00
|
|
|
/// Conversion from an [`Iterator`].
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// By implementing `FromIterator` for a type, you define how it will be
|
|
|
|
/// created from an iterator. This is common for types which describe a
|
|
|
|
/// collection of some kind.
|
|
|
|
///
|
2022-03-03 19:45:53 -05:00
|
|
|
/// If you want to create a collection from the contents of an iterator, the
|
2022-03-04 09:48:51 -05:00
|
|
|
/// [`Iterator::collect()`] method is preferred. However, when you need to
|
|
|
|
/// specify the container type, [`FromIterator::from_iter()`] can be more
|
|
|
|
/// readable than using a turbofish (e.g. `::<Vec<_>>()`). See the
|
2022-03-03 19:45:53 -05:00
|
|
|
/// [`Iterator::collect()`] documentation for more examples of its use.
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// See also: [`IntoIterator`].
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let five_fives = std::iter::repeat(5).take(5);
|
|
|
|
///
|
|
|
|
/// let v = Vec::from_iter(five_fives);
|
|
|
|
///
|
|
|
|
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
|
|
|
|
/// ```
|
|
|
|
///
|
2020-09-18 09:51:26 +02:00
|
|
|
/// Using [`Iterator::collect()`] to implicitly use `FromIterator`:
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let five_fives = std::iter::repeat(5).take(5);
|
|
|
|
///
|
|
|
|
/// let v: Vec<i32> = five_fives.collect();
|
|
|
|
///
|
|
|
|
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
|
|
|
|
/// ```
|
|
|
|
///
|
2022-03-03 19:45:53 -05:00
|
|
|
/// Using [`FromIterator::from_iter()`] as a more readable alternative to
|
|
|
|
/// [`Iterator::collect()`]:
|
|
|
|
///
|
|
|
|
/// ```
|
2022-03-04 09:45:18 -05:00
|
|
|
/// use std::collections::VecDeque;
|
2022-03-03 19:45:53 -05:00
|
|
|
/// let first = (0..10).collect::<VecDeque<i32>>();
|
|
|
|
/// let second = VecDeque::from_iter(0..10);
|
|
|
|
///
|
|
|
|
/// assert_eq!(first, second);
|
|
|
|
/// ```
|
|
|
|
///
|
2018-12-17 17:29:39 -05:00
|
|
|
/// Implementing `FromIterator` for your type:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // A sample collection, that's just a wrapper over Vec<T>
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct MyCollection(Vec<i32>);
|
|
|
|
///
|
|
|
|
/// // Let's give it some methods so we can create one and add things
|
|
|
|
/// // to it.
|
|
|
|
/// impl MyCollection {
|
|
|
|
/// fn new() -> MyCollection {
|
|
|
|
/// MyCollection(Vec::new())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn add(&mut self, elem: i32) {
|
|
|
|
/// self.0.push(elem);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // and we'll implement FromIterator
|
|
|
|
/// impl FromIterator<i32> for MyCollection {
|
|
|
|
/// fn from_iter<I: IntoIterator<Item=i32>>(iter: I) -> Self {
|
|
|
|
/// let mut c = MyCollection::new();
|
|
|
|
///
|
|
|
|
/// for i in iter {
|
|
|
|
/// c.add(i);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// c
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Now we can make a new iterator...
|
|
|
|
/// let iter = (0..5).into_iter();
|
|
|
|
///
|
|
|
|
/// // ... and make a MyCollection out of it
|
|
|
|
/// let c = MyCollection::from_iter(iter);
|
|
|
|
///
|
|
|
|
/// assert_eq!(c.0, vec![0, 1, 2, 3, 4]);
|
|
|
|
///
|
|
|
|
/// // collect works too!
|
|
|
|
///
|
|
|
|
/// let iter = (0..5).into_iter();
|
|
|
|
/// let c: MyCollection = iter.collect();
|
|
|
|
///
|
|
|
|
/// assert_eq!(c.0, vec![0, 1, 2, 3, 4]);
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_on_unimplemented(
|
2023-05-15 21:08:45 +03:00
|
|
|
on(
|
|
|
|
_Self = "&[{A}]",
|
|
|
|
message = "a slice of type `{Self}` cannot be built since we need to store the elements somewhere",
|
|
|
|
label = "try explicitly collecting into a `Vec<{A}>`",
|
|
|
|
),
|
|
|
|
on(
|
|
|
|
all(A = "{integer}", any(_Self = "&[{integral}]",)),
|
|
|
|
message = "a slice of type `{Self}` cannot be built since we need to store the elements somewhere",
|
|
|
|
label = "try explicitly collecting into a `Vec<{A}>`",
|
|
|
|
),
|
2021-12-01 18:51:27 -08:00
|
|
|
on(
|
|
|
|
_Self = "[{A}]",
|
2022-04-26 21:09:26 -07:00
|
|
|
message = "a slice of type `{Self}` cannot be built since `{Self}` has no definite size",
|
2021-12-01 18:51:27 -08:00
|
|
|
label = "try explicitly collecting into a `Vec<{A}>`",
|
|
|
|
),
|
|
|
|
on(
|
2022-04-26 21:09:26 -07:00
|
|
|
all(A = "{integer}", any(_Self = "[{integral}]",)),
|
|
|
|
message = "a slice of type `{Self}` cannot be built since `{Self}` has no definite size",
|
2021-12-01 18:51:27 -08:00
|
|
|
label = "try explicitly collecting into a `Vec<{A}>`",
|
|
|
|
),
|
2022-04-26 21:09:26 -07:00
|
|
|
on(
|
|
|
|
_Self = "[{A}; _]",
|
|
|
|
message = "an array of type `{Self}` cannot be built directly from an iterator",
|
|
|
|
label = "try collecting into a `Vec<{A}>`, then using `.try_into()`",
|
|
|
|
),
|
|
|
|
on(
|
|
|
|
all(A = "{integer}", any(_Self = "[{integral}; _]",)),
|
|
|
|
message = "an array of type `{Self}` cannot be built directly from an iterator",
|
|
|
|
label = "try collecting into a `Vec<{A}>`, then using `.try_into()`",
|
|
|
|
),
|
2019-12-06 20:18:12 -08:00
|
|
|
message = "a value of type `{Self}` cannot be built from an iterator \
|
|
|
|
over elements of type `{A}`",
|
|
|
|
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
|
2018-12-17 17:29:39 -05:00
|
|
|
)]
|
2021-07-06 13:41:22 +00:00
|
|
|
#[rustc_diagnostic_item = "FromIterator"]
|
2018-12-17 17:29:39 -05:00
|
|
|
pub trait FromIterator<A>: Sized {
|
|
|
|
/// Creates a value from an iterator.
|
|
|
|
///
|
|
|
|
/// See the [module-level documentation] for more.
|
|
|
|
///
|
2020-10-12 13:42:49 -07:00
|
|
|
/// [module-level documentation]: crate::iter
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let five_fives = std::iter::repeat(5).take(5);
|
|
|
|
///
|
|
|
|
/// let v = Vec::from_iter(five_fives);
|
|
|
|
///
|
|
|
|
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-09-26 23:56:38 -04:00
|
|
|
#[rustc_diagnostic_item = "from_iter_fn"]
|
2019-12-06 20:18:12 -08:00
|
|
|
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
|
2018-12-17 17:29:39 -05:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:51:26 +02:00
|
|
|
/// Conversion into an [`Iterator`].
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// By implementing `IntoIterator` for a type, you define how it will be
|
|
|
|
/// converted to an iterator. This is common for types which describe a
|
|
|
|
/// collection of some kind.
|
|
|
|
///
|
|
|
|
/// One benefit of implementing `IntoIterator` is that your type will [work
|
2020-10-12 13:42:49 -07:00
|
|
|
/// with Rust's `for` loop syntax](crate::iter#for-loops-and-intoiterator).
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// See also: [`FromIterator`].
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-12-17 18:36:18 +11:00
|
|
|
/// let v = [1, 2, 3];
|
2018-12-17 17:29:39 -05:00
|
|
|
/// let mut iter = v.into_iter();
|
|
|
|
///
|
|
|
|
/// assert_eq!(Some(1), iter.next());
|
|
|
|
/// assert_eq!(Some(2), iter.next());
|
|
|
|
/// assert_eq!(Some(3), iter.next());
|
|
|
|
/// assert_eq!(None, iter.next());
|
|
|
|
/// ```
|
|
|
|
/// Implementing `IntoIterator` for your type:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // A sample collection, that's just a wrapper over Vec<T>
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct MyCollection(Vec<i32>);
|
|
|
|
///
|
|
|
|
/// // Let's give it some methods so we can create one and add things
|
|
|
|
/// // to it.
|
|
|
|
/// impl MyCollection {
|
|
|
|
/// fn new() -> MyCollection {
|
|
|
|
/// MyCollection(Vec::new())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn add(&mut self, elem: i32) {
|
|
|
|
/// self.0.push(elem);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // and we'll implement IntoIterator
|
|
|
|
/// impl IntoIterator for MyCollection {
|
|
|
|
/// type Item = i32;
|
2019-10-20 21:13:47 +03:00
|
|
|
/// type IntoIter = std::vec::IntoIter<Self::Item>;
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// fn into_iter(self) -> Self::IntoIter {
|
|
|
|
/// self.0.into_iter()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Now we can make a new collection...
|
|
|
|
/// let mut c = MyCollection::new();
|
|
|
|
///
|
|
|
|
/// // ... add some stuff to it ...
|
|
|
|
/// c.add(0);
|
|
|
|
/// c.add(1);
|
|
|
|
/// c.add(2);
|
|
|
|
///
|
|
|
|
/// // ... and then turn it into an Iterator:
|
|
|
|
/// for (i, n) in c.into_iter().enumerate() {
|
|
|
|
/// assert_eq!(i as i32, n);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// It is common to use `IntoIterator` as a trait bound. This allows
|
|
|
|
/// the input collection type to change, so long as it is still an
|
|
|
|
/// iterator. Additional bounds can be specified by restricting on
|
|
|
|
/// `Item`:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn collect_as_strings<T>(collection: T) -> Vec<String>
|
2019-07-31 21:00:35 +02:00
|
|
|
/// where
|
2019-08-09 13:40:54 +02:00
|
|
|
/// T: IntoIterator,
|
|
|
|
/// T::Item: std::fmt::Debug,
|
2018-12-17 17:29:39 -05:00
|
|
|
/// {
|
|
|
|
/// collection
|
|
|
|
/// .into_iter()
|
2022-02-12 23:16:17 +04:00
|
|
|
/// .map(|item| format!("{item:?}"))
|
2018-12-17 17:29:39 -05:00
|
|
|
/// .collect()
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-11-01 12:04:18 +01:00
|
|
|
#[rustc_diagnostic_item = "IntoIterator"]
|
2021-05-06 13:36:07 +02:00
|
|
|
#[rustc_skip_array_during_method_dispatch]
|
2018-12-17 17:29:39 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub trait IntoIterator {
|
|
|
|
/// The type of the elements being iterated over.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
type Item;
|
|
|
|
|
|
|
|
/// Which kind of iterator are we turning this into?
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-06 20:18:12 -08:00
|
|
|
type IntoIter: Iterator<Item = Self::Item>;
|
2018-12-17 17:29:39 -05:00
|
|
|
|
|
|
|
/// Creates an iterator from a value.
|
|
|
|
///
|
|
|
|
/// See the [module-level documentation] for more.
|
|
|
|
///
|
2020-10-12 13:42:49 -07:00
|
|
|
/// [module-level documentation]: crate::iter
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2021-12-17 18:36:18 +11:00
|
|
|
/// let v = [1, 2, 3];
|
2018-12-17 17:29:39 -05:00
|
|
|
/// let mut iter = v.into_iter();
|
|
|
|
///
|
|
|
|
/// assert_eq!(Some(1), iter.next());
|
|
|
|
/// assert_eq!(Some(2), iter.next());
|
|
|
|
/// assert_eq!(Some(3), iter.next());
|
|
|
|
/// assert_eq!(None, iter.next());
|
|
|
|
/// ```
|
2020-08-26 10:17:31 +02:00
|
|
|
#[lang = "into_iter"]
|
2018-12-17 17:29:39 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
fn into_iter(self) -> Self::IntoIter;
|
|
|
|
}
|
|
|
|
|
2022-03-15 06:07:18 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_intoiterator_identity", issue = "90603")]
|
2018-12-17 17:29:39 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl<I: Iterator> IntoIterator for I {
|
2018-12-17 17:29:39 -05:00
|
|
|
type Item = I::Item;
|
|
|
|
type IntoIter = I;
|
|
|
|
|
2021-04-25 21:18:42 +02:00
|
|
|
#[inline]
|
2018-12-17 17:29:39 -05:00
|
|
|
fn into_iter(self) -> I {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extend a collection with the contents of an iterator.
|
|
|
|
///
|
|
|
|
/// Iterators produce a series of values, and collections can also be thought
|
|
|
|
/// of as a series of values. The `Extend` trait bridges this gap, allowing you
|
|
|
|
/// to extend a collection by including the contents of that iterator. When
|
|
|
|
/// extending a collection with an already existing key, that entry is updated
|
|
|
|
/// or, in the case of collections that permit multiple entries with equal
|
|
|
|
/// keys, that entry is inserted.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // You can extend a String with some chars:
|
|
|
|
/// let mut message = String::from("The first three letters are: ");
|
|
|
|
///
|
|
|
|
/// message.extend(&['a', 'b', 'c']);
|
|
|
|
///
|
|
|
|
/// assert_eq!("abc", &message[29..32]);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Implementing `Extend`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // A sample collection, that's just a wrapper over Vec<T>
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct MyCollection(Vec<i32>);
|
|
|
|
///
|
|
|
|
/// // Let's give it some methods so we can create one and add things
|
|
|
|
/// // to it.
|
|
|
|
/// impl MyCollection {
|
|
|
|
/// fn new() -> MyCollection {
|
|
|
|
/// MyCollection(Vec::new())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn add(&mut self, elem: i32) {
|
|
|
|
/// self.0.push(elem);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // since MyCollection has a list of i32s, we implement Extend for i32
|
|
|
|
/// impl Extend<i32> for MyCollection {
|
|
|
|
///
|
|
|
|
/// // This is a bit simpler with the concrete type signature: we can call
|
|
|
|
/// // extend on anything which can be turned into an Iterator which gives
|
|
|
|
/// // us i32s. Because we need i32s to put into MyCollection.
|
|
|
|
/// fn extend<T: IntoIterator<Item=i32>>(&mut self, iter: T) {
|
|
|
|
///
|
|
|
|
/// // The implementation is very straightforward: loop through the
|
|
|
|
/// // iterator, and add() each element to ourselves.
|
|
|
|
/// for elem in iter {
|
|
|
|
/// self.add(elem);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let mut c = MyCollection::new();
|
|
|
|
///
|
|
|
|
/// c.add(5);
|
|
|
|
/// c.add(6);
|
|
|
|
/// c.add(7);
|
|
|
|
///
|
|
|
|
/// // let's extend our collection with three more numbers
|
|
|
|
/// c.extend(vec![1, 2, 3]);
|
|
|
|
///
|
|
|
|
/// // we've added these elements onto the end
|
2022-02-12 23:16:17 +04:00
|
|
|
/// assert_eq!("MyCollection([5, 6, 7, 1, 2, 3])", format!("{c:?}"));
|
2018-12-17 17:29:39 -05:00
|
|
|
/// ```
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub trait Extend<A> {
|
|
|
|
/// Extends a collection with the contents of an iterator.
|
|
|
|
///
|
2020-05-12 20:09:55 -07:00
|
|
|
/// As this is the only required method for this trait, the [trait-level] docs
|
2018-12-17 17:29:39 -05:00
|
|
|
/// contain more details.
|
|
|
|
///
|
2020-09-18 09:51:26 +02:00
|
|
|
/// [trait-level]: Extend
|
2018-12-17 17:29:39 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // You can extend a String with some chars:
|
|
|
|
/// let mut message = String::from("abc");
|
|
|
|
///
|
|
|
|
/// message.extend(['d', 'e', 'f'].iter());
|
|
|
|
///
|
|
|
|
/// assert_eq!("abcdef", &message);
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-06 20:18:12 -08:00
|
|
|
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);
|
2020-05-12 20:09:55 -07:00
|
|
|
|
|
|
|
/// Extends a collection with exactly one element.
|
2020-05-26 14:15:29 -07:00
|
|
|
#[unstable(feature = "extend_one", issue = "72631")]
|
2020-05-12 20:09:55 -07:00
|
|
|
fn extend_one(&mut self, item: A) {
|
|
|
|
self.extend(Some(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves capacity in a collection for the given number of additional elements.
|
|
|
|
///
|
|
|
|
/// The default implementation does nothing.
|
2020-05-26 14:15:29 -07:00
|
|
|
#[unstable(feature = "extend_one", issue = "72631")]
|
2020-05-26 14:01:26 -07:00
|
|
|
fn extend_reserve(&mut self, additional: usize) {
|
|
|
|
let _ = additional;
|
|
|
|
}
|
2018-12-17 17:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "extend_for_unit", since = "1.28.0")]
|
|
|
|
impl Extend<()> for () {
|
|
|
|
fn extend<T: IntoIterator<Item = ()>>(&mut self, iter: T) {
|
|
|
|
iter.into_iter().for_each(drop)
|
|
|
|
}
|
2020-05-12 20:09:55 -07:00
|
|
|
fn extend_one(&mut self, _item: ()) {}
|
2018-12-17 17:29:39 -05:00
|
|
|
}
|
2021-05-30 18:44:37 +02:00
|
|
|
|
2021-08-03 12:12:14 -07:00
|
|
|
#[stable(feature = "extend_for_tuple", since = "1.56.0")]
|
2021-05-30 18:44:37 +02:00
|
|
|
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
|
|
|
|
where
|
|
|
|
ExtendA: Extend<A>,
|
|
|
|
ExtendB: Extend<B>,
|
|
|
|
{
|
2021-06-20 11:19:55 +02:00
|
|
|
/// Allows to `extend` a tuple of collections that also implement `Extend`.
|
|
|
|
///
|
|
|
|
/// See also: [`Iterator::unzip`]
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// let mut tuple = (vec![0], vec![1]);
|
2021-08-17 22:21:37 +02:00
|
|
|
/// tuple.extend([(2, 3), (4, 5), (6, 7)]);
|
|
|
|
/// assert_eq!(tuple.0, [0, 2, 4, 6]);
|
|
|
|
/// assert_eq!(tuple.1, [1, 3, 5, 7]);
|
2021-06-20 11:19:55 +02:00
|
|
|
///
|
2021-08-17 22:21:37 +02:00
|
|
|
/// // also allows for arbitrarily nested tuples as elements
|
|
|
|
/// let mut nested_tuple = (vec![1], (vec![2], vec![3]));
|
|
|
|
/// nested_tuple.extend([(4, (5, 6)), (7, (8, 9))]);
|
2021-06-20 11:19:55 +02:00
|
|
|
///
|
2021-08-17 22:21:37 +02:00
|
|
|
/// let (a, (b, c)) = nested_tuple;
|
|
|
|
/// assert_eq!(a, [1, 4, 7]);
|
|
|
|
/// assert_eq!(b, [2, 5, 8]);
|
|
|
|
/// assert_eq!(c, [3, 6, 9]);
|
2021-06-20 11:19:55 +02:00
|
|
|
/// ```
|
2021-05-30 18:44:37 +02:00
|
|
|
fn extend<T: IntoIterator<Item = (A, B)>>(&mut self, into_iter: T) {
|
|
|
|
let (a, b) = self;
|
|
|
|
let iter = into_iter.into_iter();
|
|
|
|
|
|
|
|
fn extend<'a, A, B>(
|
|
|
|
a: &'a mut impl Extend<A>,
|
|
|
|
b: &'a mut impl Extend<B>,
|
|
|
|
) -> impl FnMut((), (A, B)) + 'a {
|
|
|
|
move |(), (t, u)| {
|
|
|
|
a.extend_one(t);
|
|
|
|
b.extend_one(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (lower_bound, _) = iter.size_hint();
|
|
|
|
if lower_bound > 0 {
|
|
|
|
a.extend_reserve(lower_bound);
|
|
|
|
b.extend_reserve(lower_bound);
|
|
|
|
}
|
|
|
|
|
|
|
|
iter.fold((), extend(a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_one(&mut self, item: (A, B)) {
|
|
|
|
self.0.extend_one(item.0);
|
|
|
|
self.1.extend_one(item.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_reserve(&mut self, additional: usize) {
|
|
|
|
self.0.extend_reserve(additional);
|
|
|
|
self.1.extend_reserve(additional);
|
|
|
|
}
|
|
|
|
}
|