Skip to content

add default FromIterator for types with Default and Extend trait #143996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 45 additions & 33 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ pub trait FromIterator<A>: Sized {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
}

// 为同时实现 Default 和 Extend 的类型实现 FromIterator
#[cfg(feature = "specialization")]
#[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")]
impl<A, T: Default + Extend<A>> FromIterator<A> for T {
#[rustc_diagnostic_item = "from_iter_default"]
default fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self {
let mut collection = T::default();
collection.extend(iter);
collection
}
}

/// Conversion into an [`Iterator`].
///
/// By implementing `IntoIterator` for a type, you define how it will be
Expand Down Expand Up @@ -635,40 +647,40 @@ macro_rules! spec_tuple_impl {
}
}

/// This implementation turns an iterator of tuples into a tuple of types which implement
/// [`Default`] and [`Extend`].
///
/// This is similar to [`Iterator::unzip`], but is also composable with other [`FromIterator`]
/// implementations:
///
/// ```rust
/// # fn main() -> Result<(), core::num::ParseIntError> {
/// let string = "1,2,123,4";
///
/// // Example given for a 2-tuple, but 1- through 12-tuples are supported
/// let (numbers, lengths): (Vec<_>, Vec<_>) = string
/// .split(',')
/// .map(|s| s.parse().map(|n: u32| (n, s.len())))
/// .collect::<Result<_, _>>()?;
///
/// assert_eq!(numbers, [1, 2, 123, 4]);
/// assert_eq!(lengths, [1, 1, 3, 1]);
/// # Ok(()) }
/// ```
#[$meta]
$(#[$doctext])?
#[stable(feature = "from_iterator_for_tuple", since = "1.79.0")]
impl<$($ty_names,)* $($extend_ty_names,)*> FromIterator<($($extend_ty_names,)*)> for ($($ty_names,)*)
where
$($ty_names: Default + Extend<$extend_ty_names>,)*
{
fn from_iter<Iter: IntoIterator<Item = ($($extend_ty_names,)*)>>(iter: Iter) -> Self {
let mut res = <($($ty_names,)*)>::default();
res.extend(iter);
// /// This implementation turns an iterator of tuples into a tuple of types which implement
// /// [`Default`] and [`Extend`].
// ///
// /// This is similar to [`Iterator::unzip`], but is also composable with other [`FromIterator`]
// /// implementations:
// ///
// /// ```rust
// /// # fn main() -> Result<(), core::num::ParseIntError> {
// /// let string = "1,2,123,4";
// ///
// /// // Example given for a 2-tuple, but 1- through 12-tuples are supported
// /// let (numbers, lengths): (Vec<_>, Vec<_>) = string
// /// .split(',')
// /// .map(|s| s.parse().map(|n: u32| (n, s.len())))
// /// .collect::<Result<_, _>>()?;
// ///
// /// assert_eq!(numbers, [1, 2, 123, 4]);
// /// assert_eq!(lengths, [1, 1, 3, 1]);
// /// # Ok(()) }
// /// ```
// #[$meta]
// $(#[$doctext])?
// #[stable(feature = "from_iterator_for_tuple", since = "1.79.0")]
// impl<$($ty_names,)* $($extend_ty_names,)*> FromIterator<($($extend_ty_names,)*)> for ($($ty_names,)*)
// where
// $($ty_names: Default + Extend<$extend_ty_names>,)*
// {
// fn from_iter<Iter: IntoIterator<Item = ($($extend_ty_names,)*)>>(iter: Iter) -> Self {
// let mut res = <($($ty_names,)*)>::default();
// res.extend(iter);

res
}
}
// res
// }
// }

};
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![feature(simd_ffi)]
#![feature(specialization)]
#![feature(staged_api)]
#![feature(stmt_expr_attributes)]
#![feature(strict_provenance_lints)]
Expand Down
Loading