From 760a97c624daf632c1f3ac960f1a651046714fe7 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 12 Aug 2025 14:49:07 -0700 Subject: [PATCH 1/2] `impl IntoParallelIterator for Box<[T]>` and its refs --- src/slice/mod.rs | 18 ++++++++++++++++++ src/vec.rs | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/src/slice/mod.rs b/src/slice/mod.rs index 590262769..7c5aac97e 100644 --- a/src/slice/mod.rs +++ b/src/slice/mod.rs @@ -770,6 +770,15 @@ impl<'data, T: Sync> IntoParallelIterator for &'data [T] { } } +impl<'data, T: Sync> IntoParallelIterator for &'data Box<[T]> { + type Item = &'data T; + type Iter = Iter<'data, T>; + + fn into_par_iter(self) -> Self::Iter { + Iter { slice: self } + } +} + impl<'data, T: Send> IntoParallelIterator for &'data mut [T] { type Item = &'data mut T; type Iter = IterMut<'data, T>; @@ -779,6 +788,15 @@ impl<'data, T: Send> IntoParallelIterator for &'data mut [T] { } } +impl<'data, T: Send> IntoParallelIterator for &'data mut Box<[T]> { + type Item = &'data mut T; + type Iter = IterMut<'data, T>; + + fn into_par_iter(self) -> Self::Iter { + IterMut { slice: self } + } +} + /// Parallel iterator over immutable items in a slice #[derive(Debug)] pub struct Iter<'data, T> { diff --git a/src/vec.rs b/src/vec.rs index 00e951eaf..ad011894b 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -48,6 +48,15 @@ impl IntoParallelIterator for Vec { } } +impl IntoParallelIterator for Box<[T]> { + type Item = T; + type Iter = IntoIter; + + fn into_par_iter(self) -> Self::Iter { + IntoIter { vec: self.into() } + } +} + impl ParallelIterator for IntoIter { type Item = T; From 2a85fbf8f8d0af9b8c0ee1453f432ee6ed4b3c04 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 12 Aug 2025 14:59:05 -0700 Subject: [PATCH 2/2] `impl FromParallelIterator<_> for Box` via `String` --- src/iter/from_par_iter.rs | 81 +++++++++++++-------------------------- 1 file changed, 26 insertions(+), 55 deletions(-) diff --git a/src/iter/from_par_iter.rs b/src/iter/from_par_iter.rs index 1d636394d..448a4b102 100644 --- a/src/iter/from_par_iter.rs +++ b/src/iter/from_par_iter.rs @@ -176,65 +176,36 @@ where } } -/// Collects characters from a parallel iterator into a string. -impl FromParallelIterator for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator, - { - collect_extended(par_iter) - } -} +macro_rules! collect_string { + ($desc:literal, $item:ty $(, $a:lifetime)?) => { + #[doc = concat!("Collects ", $desc, " from a parallel iterator into a string.")] + impl$(<$a>)? FromParallelIterator<$item> for String { + fn from_par_iter(par_iter: I) -> Self + where + I: IntoParallelIterator, + { + collect_extended(par_iter) + } + } -/// Collects characters from a parallel iterator into a string. -impl<'a> FromParallelIterator<&'a char> for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator, - { - collect_extended(par_iter) + #[doc = concat!("Collects ", $desc, " from a parallel iterator into a boxed string.")] + impl$(<$a>)? FromParallelIterator<$item> for Box { + fn from_par_iter(par_iter: I) -> Self + where + I: IntoParallelIterator, + { + String::from_par_iter(par_iter).into_boxed_str() + } + } } } -/// Collects string slices from a parallel iterator into a string. -impl<'a> FromParallelIterator<&'a str> for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator, - { - collect_extended(par_iter) - } -} - -/// Collects strings from a parallel iterator into one large string. -impl FromParallelIterator for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator, - { - collect_extended(par_iter) - } -} - -/// Collects boxed strings from a parallel iterator into one large string. -impl FromParallelIterator> for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator>, - { - collect_extended(par_iter) - } -} - -/// Collects string slices from a parallel iterator into a string. -impl<'a> FromParallelIterator> for String { - fn from_par_iter(par_iter: I) -> Self - where - I: IntoParallelIterator>, - { - collect_extended(par_iter) - } -} +collect_string!("characters", char); +collect_string!("characters", &'a char, 'a); +collect_string!("string slices", &'a str, 'a); +collect_string!("string slices", Cow<'a, str>, 'a); +collect_string!("boxed strings", Box); +collect_string!("strings", String); /// Collects OS-string slices from a parallel iterator into an OS-string. impl<'a> FromParallelIterator<&'a OsStr> for OsString {