From b50df90d53711df9b0c7429a250e556b11d296fd Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Tue, 26 Mar 2024 22:57:19 +0100 Subject: [PATCH 1/2] Rename Iterator::intersperse() to ::separate() https://github.com/rust-lang/rust/issues/79524#issuecomment-2002474737 --- compiler/rustc_codegen_llvm/src/attributes.rs | 3 +- .../src/check/intrinsicck.rs | 2 +- .../src/hir_ty_lowering/mod.rs | 4 +- .../wrong_number_of_generic_args.rs | 2 +- .../nice_region_error/static_impl_trait.rs | 2 +- compiler/rustc_parse/src/parser/ty.rs | 4 +- .../rustc_resolve/src/late/diagnostics.rs | 4 +- compiler/rustc_session/src/config.rs | 7 +-- compiler/rustc_target/src/spec/mod.rs | 2 +- library/core/src/iter/adapters/intersperse.rs | 45 ++++++-------- library/core/src/iter/adapters/mod.rs | 2 +- library/core/src/iter/mod.rs | 2 +- library/core/src/iter/traits/iterator.rs | 31 +++++----- .../core/tests/iter/adapters/intersperse.rs | 61 +++++++++---------- src/librustdoc/clean/types.rs | 2 +- src/librustdoc/clean/utils.rs | 2 +- src/librustdoc/core.rs | 2 +- src/librustdoc/doctest.rs | 2 +- src/librustdoc/html/highlight.rs | 2 +- src/librustdoc/html/markdown.rs | 4 +- src/librustdoc/html/render/sidebar.rs | 2 +- .../clippy_lints/src/init_numbered_fields.rs | 2 +- 22 files changed, 87 insertions(+), 102 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index f9eaa0d94cbbd..32fe345c04133 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -489,8 +489,7 @@ pub fn from_fn_attrs<'ll, 'tcx>( let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str()); let function_features = function_features.iter().map(|s| s.as_str()); - let target_features: String = - global_features.chain(function_features).intersperse(",").collect(); + let target_features: String = global_features.chain(function_features).separate(",").collect(); if !target_features.is_empty() { to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features)); } diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs index df4db3ec3fbd3..264c1ccb325a1 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs @@ -381,7 +381,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> { features .iter() .map(|f| f.as_str()) - .intersperse(", ") + .separate(", ") .collect::(), ); self.tcx.dcx().span_err(*op_sp, msg); diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index a119ea450b435..e2772b9f424c0 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1667,7 +1667,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { types_and_spans[..types_and_spans.len() - 1] .iter() .map(|(x, _)| x.as_str()) - .intersperse(", ") + .separate(", ") .collect::() ), [(only, _)] => only.to_string(), @@ -1696,7 +1696,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { kinds[..kinds.len() - 1] .iter() .map(|&x| x) - .intersperse(", ") + .separate(", ") .collect::() ), "s", diff --git a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs index dcab571eedf56..c59ce36224ebc 100644 --- a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs @@ -478,7 +478,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { } _ => param.name.as_str(), }) - .intersperse(", ") + .separate(", ") .collect() } diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs index fe70b631cdb82..d1b9d5eca4283 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -434,7 +434,7 @@ fn make_elided_region_spans_suggs<'a>( consecutive_brackets += 1; } else if let Some(bracket_span) = bracket_span.take() { let sugg = std::iter::once("<") - .chain(std::iter::repeat(name).take(consecutive_brackets).intersperse(", ")) + .chain(std::iter::repeat(name).take(consecutive_brackets).separate(", ")) .chain([">"]) .collect(); spans_suggs.push((bracket_span.shrink_to_hi(), sugg)); diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 1cea32cb90fea..161c6eb7e9a9b 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -629,7 +629,7 @@ impl<'a> Parser<'a> { let sugg = if !lifetimes.is_empty() { let snippet = - lifetimes.iter().map(|param| param.ident.as_str()).intersperse(", ").collect(); + lifetimes.iter().map(|param| param.ident.as_str()).separate(", ").collect(); let (left, snippet) = if let Some(span) = param_insertion_point { (span, if params.is_empty() { snippet } else { format!(", {snippet}") }) @@ -1134,7 +1134,7 @@ impl<'a> Parser<'a> { let generic_args_span = generic_args.span(); let snippet = format!( "for<{}> ", - lifetimes.iter().map(|lt| lt.ident.as_str()).intersperse(", ").collect::(), + lifetimes.iter().map(|lt| lt.ident.as_str()).separate(", ").collect::(), ); let before_fn_path = fn_path.span.shrink_to_lo(); self.dcx() diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index bb4294fbcfb2b..c694cc0258a42 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -3005,9 +3005,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } MissingLifetimeKind::Brackets => { let sugg: String = std::iter::once("<") - .chain( - std::iter::repeat(existing_name.as_str()).take(lt.count).intersperse(", "), - ) + .chain(std::iter::repeat(existing_name.as_str()).take(lt.count).separate(", ")) .chain([">"]) .collect(); (lt.span.shrink_to_hi(), sugg) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index c06fe29c5673f..e003b2efb938c 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2771,11 +2771,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M // Check `-C link-self-contained` for consistency: individual components cannot be both enabled // and disabled at the same time. if let Some(erroneous_components) = cg.link_self_contained.check_consistency() { - let names: String = erroneous_components - .into_iter() - .map(|c| c.as_str().unwrap()) - .intersperse(", ") - .collect(); + let names: String = + erroneous_components.into_iter().map(|c| c.as_str().unwrap()).separate(", ").collect(); early_dcx.early_fatal(format!( "some `-C link-self-contained` components were both enabled and disabled: {names}" )); diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 966da2c5eda5c..e638280c871ad 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -387,7 +387,7 @@ impl LinkerFlavor { .iter() .filter(|cli| compatible(**cli)) .map(|cli| cli.desc()) - .intersperse(", ") + .separate(", ") .collect() }) } diff --git a/library/core/src/iter/adapters/intersperse.rs b/library/core/src/iter/adapters/intersperse.rs index c97a59b614f9d..f1bb9796d26bd 100644 --- a/library/core/src/iter/adapters/intersperse.rs +++ b/library/core/src/iter/adapters/intersperse.rs @@ -3,11 +3,11 @@ use crate::iter::{Fuse, FusedIterator}; /// An iterator adapter that places a separator between all elements. /// -/// This `struct` is created by [`Iterator::intersperse`]. See its documentation +/// This `struct` is created by [`Iterator::separate`]. See its documentation /// for more information. #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] #[derive(Debug, Clone)] -pub struct Intersperse +pub struct Separate where I::Item: Clone, { @@ -18,14 +18,14 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl FusedIterator for Intersperse +impl FusedIterator for Separate where I: FusedIterator, I::Item: Clone, { } -impl Intersperse +impl Separate where I::Item: Clone, { @@ -35,7 +35,7 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl Iterator for Intersperse +impl Iterator for Separate where I: Iterator, I::Item: Clone, @@ -63,7 +63,7 @@ where } fn size_hint(&self) -> (usize, Option) { - intersperse_size_hint(&self.iter, self.started, self.next_item.is_some()) + separate_size_hint(&self.iter, self.started, self.next_item.is_some()) } fn fold(self, init: B, f: F) -> B @@ -72,23 +72,16 @@ where F: FnMut(B, Self::Item) -> B, { let separator = self.separator; - intersperse_fold( - self.iter, - init, - f, - move || separator.clone(), - self.started, - self.next_item, - ) + separate_fold(self.iter, init, f, move || separator.clone(), self.started, self.next_item) } } /// An iterator adapter that places a separator between all elements. /// -/// This `struct` is created by [`Iterator::intersperse_with`]. See its +/// This `struct` is created by [`Iterator::separate_with`]. See its /// documentation for more information. #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -pub struct IntersperseWith +pub struct SeparateWith where I: Iterator, { @@ -99,7 +92,7 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl FusedIterator for IntersperseWith +impl FusedIterator for SeparateWith where I: FusedIterator, G: FnMut() -> I::Item, @@ -107,14 +100,14 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl fmt::Debug for IntersperseWith +impl fmt::Debug for SeparateWith where I: Iterator + fmt::Debug, I::Item: fmt::Debug, G: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("IntersperseWith") + f.debug_struct("SeparateWith") .field("started", &self.started) .field("separator", &self.separator) .field("iter", &self.iter) @@ -124,7 +117,7 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl Clone for IntersperseWith +impl Clone for SeparateWith where I: Iterator + Clone, I::Item: Clone, @@ -140,7 +133,7 @@ where } } -impl IntersperseWith +impl SeparateWith where I: Iterator, G: FnMut() -> I::Item, @@ -151,7 +144,7 @@ where } #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -impl Iterator for IntersperseWith +impl Iterator for SeparateWith where I: Iterator, G: FnMut() -> I::Item, @@ -179,7 +172,7 @@ where } fn size_hint(&self) -> (usize, Option) { - intersperse_size_hint(&self.iter, self.started, self.next_item.is_some()) + separate_size_hint(&self.iter, self.started, self.next_item.is_some()) } fn fold(self, init: B, f: F) -> B @@ -187,11 +180,11 @@ where Self: Sized, F: FnMut(B, Self::Item) -> B, { - intersperse_fold(self.iter, init, f, self.separator, self.started, self.next_item) + separate_fold(self.iter, init, f, self.separator, self.started, self.next_item) } } -fn intersperse_size_hint(iter: &I, started: bool, next_is_some: bool) -> (usize, Option) +fn separate_size_hint(iter: &I, started: bool, next_is_some: bool) -> (usize, Option) where I: Iterator, { @@ -208,7 +201,7 @@ where ) } -fn intersperse_fold( +fn separate_fold( mut iter: I, init: B, mut f: F, diff --git a/library/core/src/iter/adapters/mod.rs b/library/core/src/iter/adapters/mod.rs index cc514bd914f14..330c5839b3943 100644 --- a/library/core/src/iter/adapters/mod.rs +++ b/library/core/src/iter/adapters/mod.rs @@ -54,7 +54,7 @@ pub use self::flatten::Flatten; pub use self::copied::Copied; #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -pub use self::intersperse::{Intersperse, IntersperseWith}; +pub use self::intersperse::{Separate, SeparateWith}; #[stable(feature = "iter_map_while", since = "1.57.0")] pub use self::map_while::MapWhile; diff --git a/library/core/src/iter/mod.rs b/library/core/src/iter/mod.rs index 44fef3e145b78..10ee8e6d8d8cb 100644 --- a/library/core/src/iter/mod.rs +++ b/library/core/src/iter/mod.rs @@ -458,7 +458,7 @@ pub use self::adapters::{ Skip, SkipWhile, Take, TakeWhile, Zip, }; #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -pub use self::adapters::{Intersperse, IntersperseWith}; +pub use self::adapters::{Separate, SeparateWith}; pub(crate) use self::adapters::try_process; pub(crate) use self::traits::UncheckedIterator; diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index d2c9e1554b4f8..3953c3b6deadd 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -12,7 +12,7 @@ use super::super::{ Inspect, Map, MapWhile, MapWindows, Peekable, Rev, Scan, Skip, SkipWhile, StepBy, Take, TakeWhile, }; -use super::super::{Intersperse, IntersperseWith, Product, Sum, Zip}; +use super::super::{Product, Separate, SeparateWith, Sum, Zip}; fn _assert_is_object_safe(_: &dyn Iterator) {} @@ -608,7 +608,7 @@ pub trait Iterator { /// items of the original iterator. /// /// In case `separator` does not implement [`Clone`] or needs to be - /// computed every time, use [`intersperse_with`]. + /// computed every time, use [`separate_with`]. /// /// # Examples /// @@ -617,7 +617,7 @@ pub trait Iterator { /// ``` /// #![feature(iter_intersperse)] /// - /// let mut a = [0, 1, 2].iter().intersperse(&100); + /// let mut a = [0, 1, 2].iter().separate(&100); /// assert_eq!(a.next(), Some(&0)); // The first element from `a`. /// assert_eq!(a.next(), Some(&100)); // The separator. /// assert_eq!(a.next(), Some(&1)); // The next element from `a`. @@ -626,25 +626,26 @@ pub trait Iterator { /// assert_eq!(a.next(), None); // The iterator is finished. /// ``` /// - /// `intersperse` can be very useful to join an iterator's items using a common element: + /// `separate` can be very useful to join an iterator's items using a common element: /// ``` /// #![feature(iter_intersperse)] /// - /// let hello = ["Hello", "World", "!"].iter().copied().intersperse(" ").collect::(); + /// let hello = ["Hello", "World", "!"].iter().copied().separate(" ").collect::(); /// assert_eq!(hello, "Hello World !"); /// ``` /// /// [`Clone`]: crate::clone::Clone - /// [`intersperse_with`]: Iterator::intersperse_with + /// [`separate_with`]: Iterator::separate_with #[inline] #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] #[rustc_do_not_const_check] - fn intersperse(self, separator: Self::Item) -> Intersperse + #[doc(alias = "intersperse")] + fn separate(self, separator: Self::Item) -> Separate where Self: Sized, Self::Item: Clone, { - Intersperse::new(self, separator) + Separate::new(self, separator) } /// Creates a new iterator which places an item generated by `separator` @@ -656,7 +657,7 @@ pub trait Iterator { /// two items and after the last item is yielded. /// /// If the iterator's item implements [`Clone`], it may be easier to use - /// [`intersperse`]. + /// [`separate`]. /// /// # Examples /// @@ -669,7 +670,7 @@ pub trait Iterator { /// struct NotClone(usize); /// /// let v = [NotClone(0), NotClone(1), NotClone(2)]; - /// let mut it = v.into_iter().intersperse_with(|| NotClone(99)); + /// let mut it = v.into_iter().separate_with(|| NotClone(99)); /// /// assert_eq!(it.next(), Some(NotClone(0))); // The first element from `v`. /// assert_eq!(it.next(), Some(NotClone(99))); // The separator. @@ -679,7 +680,7 @@ pub trait Iterator { /// assert_eq!(it.next(), None); // The iterator is finished. /// ``` /// - /// `intersperse_with` can be used in situations where the separator needs + /// `separate_with` can be used in situations where the separator needs /// to be computed: /// ``` /// #![feature(iter_intersperse)] @@ -690,20 +691,20 @@ pub trait Iterator { /// let mut happy_emojis = [" ❤️ ", " 😀 "].iter().copied(); /// let separator = || happy_emojis.next().unwrap_or(" 🦀 "); /// - /// let result = src.intersperse_with(separator).collect::(); + /// let result = src.separate_with(separator).collect::(); /// assert_eq!(result, "Hello ❤️ to 😀 all 🦀 people 🦀 !!"); /// ``` /// [`Clone`]: crate::clone::Clone - /// [`intersperse`]: Iterator::intersperse + /// [`separate`]: Iterator::separate #[inline] #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] #[rustc_do_not_const_check] - fn intersperse_with(self, separator: G) -> IntersperseWith + fn separate_with(self, separator: G) -> SeparateWith where Self: Sized, G: FnMut() -> Self::Item, { - IntersperseWith::new(self, separator) + SeparateWith::new(self, separator) } /// Takes a closure and creates an iterator which calls that closure on each diff --git a/library/core/tests/iter/adapters/intersperse.rs b/library/core/tests/iter/adapters/intersperse.rs index 72ae59b6b2f5f..e7ca8c17e019a 100644 --- a/library/core/tests/iter/adapters/intersperse.rs +++ b/library/core/tests/iter/adapters/intersperse.rs @@ -1,30 +1,30 @@ use core::iter::*; #[test] -fn test_intersperse() { - let v = std::iter::empty().intersperse(0u32).collect::>(); +fn test_separate() { + let v = std::iter::empty().separate(0u32).collect::>(); assert_eq!(v, vec![]); - let v = std::iter::once(1).intersperse(0).collect::>(); + let v = std::iter::once(1).separate(0).collect::>(); assert_eq!(v, vec![1]); let xs = ["a", "", "b", "c"]; - let v: Vec<&str> = xs.iter().map(|x| *x).intersperse(", ").collect(); + let v: Vec<&str> = xs.iter().map(|x| *x).separate(", ").collect(); let text: String = v.concat(); assert_eq!(text, "a, , b, c".to_string()); let ys = [0, 1, 2, 3]; - let mut it = ys[..0].iter().map(|x| *x).intersperse(1); + let mut it = ys[..0].iter().map(|x| *x).separate(1); assert!(it.next() == None); } #[test] -fn test_intersperse_size_hint() { - let iter = std::iter::empty::().intersperse(0); +fn test_separate_size_hint() { + let iter = std::iter::empty::().separate(0); assert_eq!(iter.size_hint(), (0, Some(0))); let xs = ["a", "", "b", "c"]; - let mut iter = xs.iter().map(|x| *x).intersperse(", "); + let mut iter = xs.iter().map(|x| *x).separate(", "); assert_eq!(iter.size_hint(), (7, Some(7))); assert_eq!(iter.next(), Some("a")); @@ -32,36 +32,36 @@ fn test_intersperse_size_hint() { assert_eq!(iter.next(), Some(", ")); assert_eq!(iter.size_hint(), (5, Some(5))); - assert_eq!([].iter().intersperse(&()).size_hint(), (0, Some(0))); + assert_eq!([].iter().separate(&()).size_hint(), (0, Some(0))); } #[test] -fn test_fold_specialization_intersperse() { - let mut iter = (1..2).intersperse(0); +fn test_fold_specialization_separate() { + let mut iter = (1..2).separate(0); iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); - let mut iter = (1..3).intersperse(0); + let mut iter = (1..3).separate(0); iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); - let mut iter = (1..4).intersperse(0); + let mut iter = (1..4).separate(0); iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); } #[test] -fn test_try_fold_specialization_intersperse_ok() { - let mut iter = (1..2).intersperse(0); +fn test_try_fold_specialization_separate_ok() { + let mut iter = (1..2).separate(0); iter.clone().try_for_each(|x| { assert_eq!(Some(x), iter.next()); Some(()) }); - let mut iter = (1..3).intersperse(0); + let mut iter = (1..3).separate(0); iter.clone().try_for_each(|x| { assert_eq!(Some(x), iter.next()); Some(()) }); - let mut iter = (1..4).intersperse(0); + let mut iter = (1..4).separate(0); iter.clone().try_for_each(|x| { assert_eq!(Some(x), iter.next()); Some(()) @@ -69,14 +69,14 @@ fn test_try_fold_specialization_intersperse_ok() { } #[test] -fn test_intersperse_with() { +fn test_separate_with() { #[derive(PartialEq, Debug)] struct NotClone { u: u32, } let r = [NotClone { u: 0 }, NotClone { u: 1 }] .into_iter() - .intersperse_with(|| NotClone { u: 2 }) + .separate_with(|| NotClone { u: 2 }) .collect::>(); assert_eq!(r, vec![NotClone { u: 0 }, NotClone { u: 2 }, NotClone { u: 1 }]); @@ -85,19 +85,19 @@ fn test_intersperse_with() { ctr *= 2; ctr }; - let r = (0..3).intersperse_with(separator).collect::>(); + let r = (0..3).separate_with(separator).collect::>(); assert_eq!(r, vec![0, 200, 1, 400, 2]); } #[test] -fn test_intersperse_fold() { - let v = (1..4).intersperse(9).fold(Vec::new(), |mut acc, x| { +fn test_separate_fold() { + let v = (1..4).separate(9).fold(Vec::new(), |mut acc, x| { acc.push(x); acc }); assert_eq!(v.as_slice(), [1, 9, 2, 9, 3]); - let mut iter = (1..4).intersperse(9); + let mut iter = (1..4).separate(9); assert_eq!(iter.next(), Some(1)); let v = iter.fold(Vec::new(), |mut acc, x| { acc.push(x); @@ -114,25 +114,22 @@ fn test_intersperse_fold() { } } - let v = NoneAtStart(0).intersperse(1000).fold(0, |a, b| a + b); + let v = NoneAtStart(0).separate(1000).fold(0, |a, b| a + b); assert_eq!(v, 0); } #[test] -fn test_intersperse_collect_string() { +fn test_separate_collect_string() { let contents = [1, 2, 3]; - let contents_string = contents - .into_iter() - .map(|id| id.to_string()) - .intersperse(", ".to_owned()) - .collect::(); + let contents_string = + contents.into_iter().map(|id| id.to_string()).separate(", ".to_owned()).collect::(); assert_eq!(contents_string, "1, 2, 3"); } #[test] -fn test_try_fold_specialization_intersperse_err() { - let orig_iter = ["a", "b"].iter().copied().intersperse("-"); +fn test_try_fold_specialization_separate_err() { + let orig_iter = ["a", "b"].iter().copied().separate("-"); // Abort after the first item. let mut iter = orig_iter.clone(); diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index a51f6360df2a4..7923ded6dd31e 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2192,7 +2192,7 @@ impl Path { self.segments .iter() .map(|s| if s.name == kw::PathRoot { "" } else { s.name.as_str() }) - .intersperse("::") + .separate("::") .collect() } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 977b4bb45b620..99c9f92c45d76 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -410,7 +410,7 @@ fn format_integer_with_underscore_sep(num: &str) -> String { num_chars[..num_start_index] .iter() - .chain(num_chars[num_start_index..].rchunks(chunk_size).rev().intersperse(&['_']).flatten()) + .chain(num_chars[num_start_index..].rchunks(chunk_size).rev().separate(&['_']).flatten()) .collect() } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 80a30ac272771..2995b821c5d5e 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -486,7 +486,7 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> { path.segments .iter() .map(|segment| segment.ident.as_str()) - .intersperse("::") + .separate("::") .collect::() ); rustc_errors::struct_span_code_err!( diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 22a3cf4d44db8..02d08e6b3d344 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -485,7 +485,7 @@ fn run_test( true } }) - .intersperse_with(|| "\n") + .separate_with(|| "\n") .collect::(); // Add a \n to the end to properly terminate the last line, diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index aa5998876d9ab..fa724b2edaec8 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -980,7 +980,7 @@ fn string_without_closing_tag( let mut text_s = text.to_string(); if text_s.contains("::") { - text_s = text_s.split("::").intersperse("::").fold(String::new(), |mut path, t| { + text_s = text_s.split("::").separate("::").fold(String::new(), |mut path, t| { match t { "self" | "Self" => write!( &mut path, diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 64f0e096cd02e..eaf1f0ecc59ea 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -286,7 +286,7 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'_, 'a, I> { }; let lines = original_text.lines().filter_map(|l| map_line(l).for_html()); - let text = lines.intersperse("\n".into()).collect::(); + let text = lines.separate("\n".into()).collect::(); let explicit_edition = edition.is_some(); let edition = edition.unwrap_or(self.edition); @@ -300,7 +300,7 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'_, 'a, I> { let test = original_text .lines() .map(|l| map_line(l).for_code()) - .intersperse("\n".into()) + .separate("\n".into()) .collect::(); let krate = krate.as_ref().map(|s| s.as_str()); diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs index 3d28937eb99e1..2cf651f8e999a 100644 --- a/src/librustdoc/html/render/sidebar.rs +++ b/src/librustdoc/html/render/sidebar.rs @@ -123,7 +123,7 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf let sidebar_path = if it.is_mod() { &cx.current[..cx.current.len() - 1] } else { &cx.current[..] }; let path: String = if sidebar_path.len() > 1 || !title.is_empty() { - let path = sidebar_path.iter().map(|s| s.as_str()).intersperse("::").collect(); + let path = sidebar_path.iter().map(|s| s.as_str()).separate("::").collect(); if sidebar_path.len() == 1 { format!("crate {path}") } else { path } } else { "".into() diff --git a/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs b/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs index e486563808a5d..7153c05480f64 100644 --- a/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs +++ b/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs @@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for NumberedFields { expr_spans .into_iter_sorted() .map(|(_, span)| snippet_with_applicability(cx, span, "..", &mut appl)) - .intersperse(Cow::Borrowed(", ")) + .separate(Cow::Borrowed(", ")) .collect::() ); span_lint_and_sugg( From 763b8ec73b3d4ba98491f2eb01fe831cd2243779 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Tue, 26 Mar 2024 23:11:45 +0100 Subject: [PATCH 2/2] Rename iter::adapters::intersperse to ::separate https://github.com/rust-lang/rust/issues/79524#issuecomment-2002474737 --- library/core/src/iter/adapters/mod.rs | 4 ++-- .../core/src/iter/adapters/{intersperse.rs => separate.rs} | 0 library/core/tests/iter/adapters/mod.rs | 2 +- .../core/tests/iter/adapters/{intersperse.rs => separate.rs} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename library/core/src/iter/adapters/{intersperse.rs => separate.rs} (100%) rename library/core/tests/iter/adapters/{intersperse.rs => separate.rs} (100%) diff --git a/library/core/src/iter/adapters/mod.rs b/library/core/src/iter/adapters/mod.rs index 330c5839b3943..25e6cc9994d50 100644 --- a/library/core/src/iter/adapters/mod.rs +++ b/library/core/src/iter/adapters/mod.rs @@ -14,13 +14,13 @@ mod filter_map; mod flatten; mod fuse; mod inspect; -mod intersperse; mod map; mod map_while; mod map_windows; mod peekable; mod rev; mod scan; +mod separate; mod skip; mod skip_while; mod step_by; @@ -54,7 +54,7 @@ pub use self::flatten::Flatten; pub use self::copied::Copied; #[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")] -pub use self::intersperse::{Separate, SeparateWith}; +pub use self::separate::{Separate, SeparateWith}; #[stable(feature = "iter_map_while", since = "1.57.0")] pub use self::map_while::MapWhile; diff --git a/library/core/src/iter/adapters/intersperse.rs b/library/core/src/iter/adapters/separate.rs similarity index 100% rename from library/core/src/iter/adapters/intersperse.rs rename to library/core/src/iter/adapters/separate.rs diff --git a/library/core/tests/iter/adapters/mod.rs b/library/core/tests/iter/adapters/mod.rs index dedb4c0a9dd5f..95e9cd09e7eae 100644 --- a/library/core/tests/iter/adapters/mod.rs +++ b/library/core/tests/iter/adapters/mod.rs @@ -11,11 +11,11 @@ mod flat_map; mod flatten; mod fuse; mod inspect; -mod intersperse; mod map; mod map_windows; mod peekable; mod scan; +mod separate; mod skip; mod skip_while; mod step_by; diff --git a/library/core/tests/iter/adapters/intersperse.rs b/library/core/tests/iter/adapters/separate.rs similarity index 100% rename from library/core/tests/iter/adapters/intersperse.rs rename to library/core/tests/iter/adapters/separate.rs