diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 3bc9cff8072bf..98519d04d3eed 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -152,6 +152,18 @@ pub trait FromIterator: Sized { fn from_iter>(iter: T) -> Self; } +// 为同时实现 Default 和 Extend 的类型实现 FromIterator +#[cfg(feature = "specialization")] +#[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")] +impl> FromIterator for T { + #[rustc_diagnostic_item = "from_iter_default"] + default fn from_iter>(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 @@ -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::>()?; - /// - /// 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: 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::>()?; + // /// + // /// 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: Iter) -> Self { + // let mut res = <($($ty_names,)*)>::default(); + // res.extend(iter); - res - } - } + // res + // } + // } }; } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 2f701171505c7..37699fd9420ee 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -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)]