From 165376741046b6453416807ccbff32d670de7d7b Mon Sep 17 00:00:00 2001 From: HernandoR Date: Wed, 16 Jul 2025 08:52:55 +0800 Subject: [PATCH 1/6] feat(lib:core:iter): add default impl for type with Default and Extend trait Signed-off-by: HernandoR --- library/alloc/src/lib.rs | 1 + library/core/src/iter/traits/collect.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 4ad65e678c3da..5ba6c69151b3d 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -123,6 +123,7 @@ #![feature(fmt_internals)] #![feature(fn_traits)] #![feature(formatting_options)] +#![feature(from_iter_default)] #![feature(generic_atomic)] #![feature(hasher_prefixfree_extras)] #![feature(inplace_iteration)] diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 3bc9cff8072bf..cfd8ce21ca5d3 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -152,6 +152,17 @@ pub trait FromIterator: Sized { fn from_iter>(iter: T) -> Self; } +// 为同时实现 Default 和 Extend 的类型实现 FromIterator +#[unstable(feature = "from_iter_default", issue = "58659")] +impl> FromIterator for A { + #[rustc_diagnostic_item = "from_iter_default"] + default fn from_iter>(iter: I) -> Self { + let mut collection = A::default(); + collection.extend(iter); + collection + } +} + /// Conversion into an [`Iterator`]. /// /// By implementing `IntoIterator` for a type, you define how it will be From 8497fe109bdfc96e0a96f382a930d285858f23a4 Mon Sep 17 00:00:00 2001 From: Liu Zhen Date: Wed, 16 Jul 2025 15:18:30 +0800 Subject: [PATCH 2/6] Update collect.rs --- library/core/src/iter/traits/collect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index cfd8ce21ca5d3..ebd7689bb661b 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -153,8 +153,8 @@ pub trait FromIterator: Sized { } // 为同时实现 Default 和 Extend 的类型实现 FromIterator -#[unstable(feature = "from_iter_default", issue = "58659")] impl> FromIterator for A { + #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "from_iter_default"] default fn from_iter>(iter: I) -> Self { let mut collection = A::default(); From e9fba68d1ee099e192f91da5596afe48fb705686 Mon Sep 17 00:00:00 2001 From: Liu Zhen Date: Wed, 16 Jul 2025 16:27:50 +0800 Subject: [PATCH 3/6] update collect.rs Co-authored-by: Marijn Schouten --- library/core/src/iter/traits/collect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index ebd7689bb661b..2ae39eede2763 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -153,8 +153,8 @@ pub trait FromIterator: Sized { } // 为同时实现 Default 和 Extend 的类型实现 FromIterator +#[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")] impl> FromIterator for A { - #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "from_iter_default"] default fn from_iter>(iter: I) -> Self { let mut collection = A::default(); From 0950efcb344222417cc12d8fbc94ad2ec9e1c13e Mon Sep 17 00:00:00 2001 From: HernandoR Date: Wed, 16 Jul 2025 19:37:59 +0800 Subject: [PATCH 4/6] chore swap A and T Signed-off-by: HernandoR --- library/core/src/iter/traits/collect.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 2ae39eede2763..d0136a7d26e7f 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -154,10 +154,10 @@ pub trait FromIterator: Sized { // 为同时实现 Default 和 Extend 的类型实现 FromIterator #[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")] -impl> FromIterator for A { +impl> FromIterator for T { #[rustc_diagnostic_item = "from_iter_default"] - default fn from_iter>(iter: I) -> Self { - let mut collection = A::default(); + default fn from_iter>(iter: I) -> Self { + let mut collection = T::default(); collection.extend(iter); collection } From 2a9c2d3234a3f800b0101fc7514c7e745c3ac8da Mon Sep 17 00:00:00 2001 From: HernandoR Date: Wed, 16 Jul 2025 19:49:26 +0800 Subject: [PATCH 5/6] remove feature gate Signed-off-by: HernandoR --- library/alloc/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 5ba6c69151b3d..4ad65e678c3da 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -123,7 +123,6 @@ #![feature(fmt_internals)] #![feature(fn_traits)] #![feature(formatting_options)] -#![feature(from_iter_default)] #![feature(generic_atomic)] #![feature(hasher_prefixfree_extras)] #![feature(inplace_iteration)] From 3ca979e51b97c017724191b95ee4a7e5391748ac Mon Sep 17 00:00:00 2001 From: HernandoR Date: Wed, 16 Jul 2025 23:30:18 +0800 Subject: [PATCH 6/6] use specialization Signed-off-by: HernandoR --- library/core/src/iter/traits/collect.rs | 67 +++++++++++++------------ library/core/src/lib.rs | 1 + 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index d0136a7d26e7f..98519d04d3eed 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -153,6 +153,7 @@ pub trait FromIterator: Sized { } // 为同时实现 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"] @@ -646,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)]