summaryrefslogtreecommitdiffstats
path: root/vendor/rustc-rayon/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
commit1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch)
tree3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/rustc-rayon/src
parentReleasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz
rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustc-rayon/src')
-rw-r--r--vendor/rustc-rayon/src/array.rs9
-rw-r--r--vendor/rustc-rayon/src/collections/mod.rs4
-rw-r--r--vendor/rustc-rayon/src/compile_fail/cannot_collect_filtermap_data.rs12
-rw-r--r--vendor/rustc-rayon/src/compile_fail/cannot_zip_filtered_data.rs10
-rw-r--r--vendor/rustc-rayon/src/compile_fail/cell_par_iter.rs10
-rw-r--r--vendor/rustc-rayon/src/compile_fail/must_use.rs2
-rw-r--r--vendor/rustc-rayon/src/compile_fail/no_send_par_iter.rs30
-rw-r--r--vendor/rustc-rayon/src/compile_fail/rc_par_iter.rs12
-rw-r--r--vendor/rustc-rayon/src/iter/chunks.rs50
-rw-r--r--vendor/rustc-rayon/src/iter/collect/test.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/filter.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/filter_map.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/find.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/fold_chunks.rs236
-rw-r--r--vendor/rustc-rayon/src/iter/fold_chunks_with.rs231
-rw-r--r--vendor/rustc-rayon/src/iter/inspect.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/interleave.rs18
-rw-r--r--vendor/rustc-rayon/src/iter/map.rs2
-rw-r--r--vendor/rustc-rayon/src/iter/mod.rs269
-rw-r--r--vendor/rustc-rayon/src/iter/par_bridge.rs159
-rw-r--r--vendor/rustc-rayon/src/iter/plumbing/README.md6
-rw-r--r--vendor/rustc-rayon/src/iter/skip_any.rs144
-rw-r--r--vendor/rustc-rayon/src/iter/skip_any_while.rs166
-rw-r--r--vendor/rustc-rayon/src/iter/step_by.rs1
-rw-r--r--vendor/rustc-rayon/src/iter/take_any.rs144
-rw-r--r--vendor/rustc-rayon/src/iter/take_any_while.rs166
-rw-r--r--vendor/rustc-rayon/src/iter/test.rs24
-rw-r--r--vendor/rustc-rayon/src/iter/try_fold.rs3
-rw-r--r--vendor/rustc-rayon/src/iter/try_reduce.rs3
-rw-r--r--vendor/rustc-rayon/src/iter/try_reduce_with.rs3
-rw-r--r--vendor/rustc-rayon/src/iter/update.rs2
-rw-r--r--vendor/rustc-rayon/src/lib.rs15
-rw-r--r--vendor/rustc-rayon/src/math.rs2
-rw-r--r--vendor/rustc-rayon/src/slice/chunks.rs2
-rw-r--r--vendor/rustc-rayon/src/slice/mergesort.rs22
-rw-r--r--vendor/rustc-rayon/src/slice/mod.rs8
-rw-r--r--vendor/rustc-rayon/src/slice/quicksort.rs79
-rw-r--r--vendor/rustc-rayon/src/slice/rchunks.rs2
-rw-r--r--vendor/rustc-rayon/src/slice/test.rs2
-rw-r--r--vendor/rustc-rayon/src/str.rs6
-rw-r--r--vendor/rustc-rayon/src/vec.rs53
41 files changed, 1593 insertions, 324 deletions
diff --git a/vendor/rustc-rayon/src/array.rs b/vendor/rustc-rayon/src/array.rs
index c9bd8bd05..32a5fdd2f 100644
--- a/vendor/rustc-rayon/src/array.rs
+++ b/vendor/rustc-rayon/src/array.rs
@@ -1,11 +1,8 @@
-#![cfg(has_min_const_generics)]
//! Parallel iterator types for [arrays] (`[T; N]`)
//!
//! You will rarely need to interact with this module directly unless you need
//! to name one of the iterator types.
//!
-//! Everything in this module requires const generics, stabilized in Rust 1.51.
-//!
//! [arrays]: https://doc.rust-lang.org/std/primitive.array.html
use crate::iter::plumbing::*;
@@ -14,7 +11,6 @@ use crate::slice::{Iter, IterMut};
use crate::vec::DrainProducer;
use std::mem::ManuallyDrop;
-/// This implementation requires const generics, stabilized in Rust 1.51.
impl<'data, T: Sync + 'data, const N: usize> IntoParallelIterator for &'data [T; N] {
type Item = &'data T;
type Iter = Iter<'data, T>;
@@ -24,7 +20,6 @@ impl<'data, T: Sync + 'data, const N: usize> IntoParallelIterator for &'data [T;
}
}
-/// This implementation requires const generics, stabilized in Rust 1.51.
impl<'data, T: Send + 'data, const N: usize> IntoParallelIterator for &'data mut [T; N] {
type Item = &'data mut T;
type Iter = IterMut<'data, T>;
@@ -34,7 +29,6 @@ impl<'data, T: Send + 'data, const N: usize> IntoParallelIterator for &'data mut
}
}
-/// This implementation requires const generics, stabilized in Rust 1.51.
impl<T: Send, const N: usize> IntoParallelIterator for [T; N] {
type Item = T;
type Iter = IntoIter<T, N>;
@@ -84,7 +78,8 @@ impl<T: Send, const N: usize> IndexedParallelIterator for IntoIter<T, N> {
unsafe {
// Drain every item, and then the local array can just fall out of scope.
let mut array = ManuallyDrop::new(self.array);
- callback.callback(DrainProducer::new(&mut *array))
+ let producer = DrainProducer::new(array.as_mut_slice());
+ callback.callback(producer)
}
}
}
diff --git a/vendor/rustc-rayon/src/collections/mod.rs b/vendor/rustc-rayon/src/collections/mod.rs
index d9b7988da..3c99f32c4 100644
--- a/vendor/rustc-rayon/src/collections/mod.rs
+++ b/vendor/rustc-rayon/src/collections/mod.rs
@@ -56,7 +56,7 @@ mod drain_guard {
pub(super) fn new(collection: &'a mut C) -> Self {
Self {
// Temporarily steal the inner `Vec` so we can drain in place.
- vec: Vec::from(mem::replace(collection, C::default())),
+ vec: Vec::from(mem::take(collection)),
collection,
}
}
@@ -65,7 +65,7 @@ mod drain_guard {
impl<'a, T, C: From<Vec<T>>> Drop for DrainGuard<'a, T, C> {
fn drop(&mut self) {
// Restore the collection from the `Vec` with its original capacity.
- *self.collection = C::from(mem::replace(&mut self.vec, Vec::new()));
+ *self.collection = C::from(mem::take(&mut self.vec));
}
}
diff --git a/vendor/rustc-rayon/src/compile_fail/cannot_collect_filtermap_data.rs b/vendor/rustc-rayon/src/compile_fail/cannot_collect_filtermap_data.rs
index 65ff87532..bb62ce02f 100644
--- a/vendor/rustc-rayon/src/compile_fail/cannot_collect_filtermap_data.rs
+++ b/vendor/rustc-rayon/src/compile_fail/cannot_collect_filtermap_data.rs
@@ -5,12 +5,10 @@ use rayon::prelude::*;
// zip requires data of exact size, but filter yields only bounded
// size, so check that we cannot apply it.
-fn main() {
- let a: Vec<usize> = (0..1024).collect();
- let mut v = vec![];
- a.par_iter()
- .filter_map(|&x| Some(x as f32))
- .collect_into_vec(&mut v); //~ ERROR no method
-}
+let a: Vec<usize> = (0..1024).collect();
+let mut v = vec![];
+a.par_iter()
+ .filter_map(|&x| Some(x as f32))
+ .collect_into_vec(&mut v); //~ ERROR no method
``` */
diff --git a/vendor/rustc-rayon/src/compile_fail/cannot_zip_filtered_data.rs b/vendor/rustc-rayon/src/compile_fail/cannot_zip_filtered_data.rs
index de43fca57..54fd50de1 100644
--- a/vendor/rustc-rayon/src/compile_fail/cannot_zip_filtered_data.rs
+++ b/vendor/rustc-rayon/src/compile_fail/cannot_zip_filtered_data.rs
@@ -5,12 +5,10 @@ use rayon::prelude::*;
// zip requires data of exact size, but filter yields only bounded
// size, so check that we cannot apply it.
-fn main() {
- let mut a: Vec<usize> = (0..1024).rev().collect();
- let b: Vec<usize> = (0..1024).collect();
+let mut a: Vec<usize> = (0..1024).rev().collect();
+let b: Vec<usize> = (0..1024).collect();
- a.par_iter()
- .zip(b.par_iter().filter(|&&x| x > 3)); //~ ERROR
-}
+a.par_iter()
+ .zip(b.par_iter().filter(|&&x| x > 3)); //~ ERROR
``` */
diff --git a/vendor/rustc-rayon/src/compile_fail/cell_par_iter.rs b/vendor/rustc-rayon/src/compile_fail/cell_par_iter.rs
index 4af04b124..98a1cf96e 100644
--- a/vendor/rustc-rayon/src/compile_fail/cell_par_iter.rs
+++ b/vendor/rustc-rayon/src/compile_fail/cell_par_iter.rs
@@ -5,11 +5,9 @@
use rayon::prelude::*;
use std::cell::Cell;
-fn main() {
- let c = Cell::new(42_i32);
- (0_i32..1024).into_par_iter()
- .map(|_| c.get()) //~ ERROR E0277
- .min();
-}
+let c = Cell::new(42_i32);
+(0_i32..1024).into_par_iter()
+ .map(|_| c.get()) //~ ERROR E0277
+ .min();
``` */
diff --git a/vendor/rustc-rayon/src/compile_fail/must_use.rs b/vendor/rustc-rayon/src/compile_fail/must_use.rs
index ac50a62f3..b4b3d771c 100644
--- a/vendor/rustc-rayon/src/compile_fail/must_use.rs
+++ b/vendor/rustc-rayon/src/compile_fail/must_use.rs
@@ -33,6 +33,8 @@ must_use! {
step_by /** v.par_iter().step_by(2); */
chain /** v.par_iter().chain(&v); */
chunks /** v.par_iter().chunks(2); */
+ fold_chunks /** v.par_iter().fold_chunks(2, || 0, |x, _| x); */
+ fold_chunks_with /** v.par_iter().fold_chunks_with(2, 0, |x, _| x); */
cloned /** v.par_iter().cloned(); */
copied /** v.par_iter().copied(); */
enumerate /** v.par_iter().enumerate(); */
diff --git a/vendor/rustc-rayon/src/compile_fail/no_send_par_iter.rs b/vendor/rustc-rayon/src/compile_fail/no_send_par_iter.rs
index 1362c9809..7573c0346 100644
--- a/vendor/rustc-rayon/src/compile_fail/no_send_par_iter.rs
+++ b/vendor/rustc-rayon/src/compile_fail/no_send_par_iter.rs
@@ -10,13 +10,11 @@ struct NoSend(*const ());
unsafe impl Sync for NoSend {}
-fn main() {
- let x = Some(NoSend(null()));
+let x = Some(NoSend(null()));
- x.par_iter()
- .map(|&x| x) //~ ERROR
- .count(); //~ ERROR
-}
+x.par_iter()
+ .map(|&x| x) //~ ERROR
+ .count(); //~ ERROR
``` */
mod map {}
@@ -31,13 +29,11 @@ struct NoSend(*const ());
unsafe impl Sync for NoSend {}
-fn main() {
- let x = Some(NoSend(null()));
+let x = Some(NoSend(null()));
- x.par_iter()
- .filter_map(|&x| Some(x)) //~ ERROR
- .count(); //~ ERROR
-}
+x.par_iter()
+ .filter_map(|&x| Some(x)) //~ ERROR
+ .count(); //~ ERROR
``` */
mod filter_map {}
@@ -52,13 +48,11 @@ struct NoSend(*const ());
unsafe impl Sync for NoSend {}
-fn main() {
- let x = Some(NoSend(null()));
+let x = Some(NoSend(null()));
- x.par_iter()
- .cloned() //~ ERROR
- .count(); //~ ERROR
-}
+x.par_iter()
+ .cloned() //~ ERROR
+ .count(); //~ ERROR
``` */
mod cloned {}
diff --git a/vendor/rustc-rayon/src/compile_fail/rc_par_iter.rs b/vendor/rustc-rayon/src/compile_fail/rc_par_iter.rs
index feaedb38f..aca63e7ac 100644
--- a/vendor/rustc-rayon/src/compile_fail/rc_par_iter.rs
+++ b/vendor/rustc-rayon/src/compile_fail/rc_par_iter.rs
@@ -6,12 +6,10 @@
use rayon::prelude::*;
use std::rc::Rc;
-fn main() {
- let x = vec![Rc::new(22), Rc::new(23)];
- let mut y = vec![];
- x.into_par_iter() //~ ERROR no method named `into_par_iter`
- .map(|rc| *rc)
- .collect_into_vec(&mut y);
-}
+let x = vec![Rc::new(22), Rc::new(23)];
+let mut y = vec![];
+x.into_par_iter() //~ ERROR no method named `into_par_iter`
+ .map(|rc| *rc)
+ .collect_into_vec(&mut y);
``` */
diff --git a/vendor/rustc-rayon/src/iter/chunks.rs b/vendor/rustc-rayon/src/iter/chunks.rs
index be5f84cc7..ec48278d0 100644
--- a/vendor/rustc-rayon/src/iter/chunks.rs
+++ b/vendor/rustc-rayon/src/iter/chunks.rs
@@ -90,38 +90,46 @@ where
where
P: Producer<Item = T>,
{
- self.callback.callback(ChunkProducer {
- chunk_size: self.size,
- len: self.len,
- base,
- })
+ let producer = ChunkProducer::new(self.size, self.len, base, Vec::from_iter);
+ self.callback.callback(producer)
}
}
}
}
-struct ChunkProducer<P>
-where
- P: Producer,
-{
+pub(super) struct ChunkProducer<P, F> {
chunk_size: usize,
len: usize,
base: P,
+ map: F,
}
-impl<P> Producer for ChunkProducer<P>
+impl<P, F> ChunkProducer<P, F> {
+ pub(super) fn new(chunk_size: usize, len: usize, base: P, map: F) -> Self {
+ Self {
+ chunk_size,
+ len,
+ base,
+ map,
+ }
+ }
+}
+
+impl<P, F, T> Producer for ChunkProducer<P, F>
where
P: Producer,
+ F: Fn(P::IntoIter) -> T + Send + Clone,
{
- type Item = Vec<P::Item>;
- type IntoIter = ChunkSeq<P>;
+ type Item = T;
+ type IntoIter = std::iter::Map<ChunkSeq<P>, F>;
fn into_iter(self) -> Self::IntoIter {
- ChunkSeq {
+ let chunks = ChunkSeq {
chunk_size: self.chunk_size,
len: self.len,
inner: if self.len > 0 { Some(self.base) } else { None },
- }
+ };
+ chunks.map(self.map)
}
fn split_at(self, index: usize) -> (Self, Self) {
@@ -132,11 +140,13 @@ where
chunk_size: self.chunk_size,
len: elem_index,
base: left,
+ map: self.map.clone(),
},
ChunkProducer {
chunk_size: self.chunk_size,
len: self.len - elem_index,
base: right,
+ map: self.map,
},
)
}
@@ -150,7 +160,7 @@ where
}
}
-struct ChunkSeq<P> {
+pub(super) struct ChunkSeq<P> {
chunk_size: usize,
len: usize,
inner: Option<P>,
@@ -160,7 +170,7 @@ impl<P> Iterator for ChunkSeq<P>
where
P: Producer,
{
- type Item = Vec<P::Item>;
+ type Item = P::IntoIter;
fn next(&mut self) -> Option<Self::Item> {
let producer = self.inner.take()?;
@@ -168,11 +178,11 @@ where
let (left, right) = producer.split_at(self.chunk_size);
self.inner = Some(right);
self.len -= self.chunk_size;
- Some(left.into_iter().collect())
+ Some(left.into_iter())
} else {
debug_assert!(self.len > 0);
self.len = 0;
- Some(producer.into_iter().collect())
+ Some(producer.into_iter())
}
}
@@ -206,11 +216,11 @@ where
let (left, right) = producer.split_at(self.len - size);
self.inner = Some(left);
self.len -= size;
- Some(right.into_iter().collect())
+ Some(right.into_iter())
} else {
debug_assert!(self.len > 0);
self.len = 0;
- Some(producer.into_iter().collect())
+ Some(producer.into_iter())
}
}
}
diff --git a/vendor/rustc-rayon/src/iter/collect/test.rs b/vendor/rustc-rayon/src/iter/collect/test.rs
index b5f676f5d..97bec3f4e 100644
--- a/vendor/rustc-rayon/src/iter/collect/test.rs
+++ b/vendor/rustc-rayon/src/iter/collect/test.rs
@@ -76,6 +76,7 @@ fn right_produces_items_with_no_complete() {
// Complete is not called by the consumer. Hence,the collection vector is not fully initialized.
#[test]
+#[cfg_attr(not(panic = "unwind"), ignore)]
fn produces_items_with_no_complete() {
let counter = DropCounter::default();
let mut v = vec![];
@@ -273,6 +274,7 @@ fn right_panics() {
// The left consumer produces fewer items while the right
// consumer produces correct number; check that created elements are dropped
#[test]
+#[cfg_attr(not(panic = "unwind"), ignore)]
fn left_produces_fewer_items_drops() {
let counter = DropCounter::default();
let mut v = vec![];
diff --git a/vendor/rustc-rayon/src/iter/filter.rs b/vendor/rustc-rayon/src/iter/filter.rs
index 38627f7c0..e1b74ba52 100644
--- a/vendor/rustc-rayon/src/iter/filter.rs
+++ b/vendor/rustc-rayon/src/iter/filter.rs
@@ -97,7 +97,7 @@ where
P: Fn(&T) -> bool + Sync,
{
fn split_off_left(&self) -> Self {
- FilterConsumer::new(self.base.split_off_left(), &self.filter_op)
+ FilterConsumer::new(self.base.split_off_left(), self.filter_op)
}
fn to_reducer(&self) -> Self::Reducer {
diff --git a/vendor/rustc-rayon/src/iter/filter_map.rs b/vendor/rustc-rayon/src/iter/filter_map.rs
index f19c38563..db1c7e3f0 100644
--- a/vendor/rustc-rayon/src/iter/filter_map.rs
+++ b/vendor/rustc-rayon/src/iter/filter_map.rs
@@ -98,7 +98,7 @@ where
P: Fn(T) -> Option<U> + Sync + 'p,
{
fn split_off_left(&self) -> Self {
- FilterMapConsumer::new(self.base.split_off_left(), &self.filter_op)
+ FilterMapConsumer::new(self.base.split_off_left(), self.filter_op)
}
fn to_reducer(&self) -> Self::Reducer {
diff --git a/vendor/rustc-rayon/src/iter/find.rs b/vendor/rustc-rayon/src/iter/find.rs
index 971db2b3c..b16ee8446 100644
--- a/vendor/rustc-rayon/src/iter/find.rs
+++ b/vendor/rustc-rayon/src/iter/find.rs
@@ -94,7 +94,7 @@ where
self.item = iter
.into_iter()
// stop iterating if another thread has found something
- .take_while(not_full(&self.found))
+ .take_while(not_full(self.found))
.find(self.find_op);
if self.item.is_some() {
self.found.store(true, Ordering::Relaxed)
diff --git a/vendor/rustc-rayon/src/iter/fold_chunks.rs b/vendor/rustc-rayon/src/iter/fold_chunks.rs
new file mode 100644
index 000000000..185fb1a65
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/fold_chunks.rs
@@ -0,0 +1,236 @@
+use std::fmt::{self, Debug};
+
+use super::chunks::ChunkProducer;
+use super::plumbing::*;
+use super::*;
+use crate::math::div_round_up;
+
+/// `FoldChunks` is an iterator that groups elements of an underlying iterator and applies a
+/// function over them, producing a single value for each group.
+///
+/// This struct is created by the [`fold_chunks()`] method on [`IndexedParallelIterator`]
+///
+/// [`fold_chunks()`]: trait.IndexedParallelIterator.html#method.fold_chunks
+/// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FoldChunks<I, ID, F>
+where
+ I: IndexedParallelIterator,
+{
+ base: I,
+ chunk_size: usize,
+ fold_op: F,
+ identity: ID,
+}
+
+impl<I: IndexedParallelIterator + Debug, ID, F> Debug for FoldChunks<I, ID, F> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Fold")
+ .field("base", &self.base)
+ .field("chunk_size", &self.chunk_size)
+ .finish()
+ }
+}
+
+impl<I, ID, U, F> FoldChunks<I, ID, F>
+where
+ I: IndexedParallelIterator,
+ ID: Fn() -> U + Send + Sync,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+ U: Send,
+{
+ /// Creates a new `FoldChunks` iterator
+ pub(super) fn new(base: I, chunk_size: usize, identity: ID, fold_op: F) -> Self {
+ FoldChunks {
+ base,
+ chunk_size,
+ identity,
+ fold_op,
+ }
+ }
+}
+
+impl<I, ID, U, F> ParallelIterator for FoldChunks<I, ID, F>
+where
+ I: IndexedParallelIterator,
+ ID: Fn() -> U + Send + Sync,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+ U: Send,
+{
+ type Item = U;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: Consumer<U>,
+ {
+ bridge(self, consumer)
+ }
+
+ fn opt_len(&self) -> Option<usize> {
+ Some(self.len())
+ }
+}
+
+impl<I, ID, U, F> IndexedParallelIterator for FoldChunks<I, ID, F>
+where
+ I: IndexedParallelIterator,
+ ID: Fn() -> U + Send + Sync,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+ U: Send,
+{
+ fn len(&self) -> usize {
+ div_round_up(self.base.len(), self.chunk_size)
+ }
+
+ fn drive<C>(self, consumer: C) -> C::Result
+ where
+ C: Consumer<Self::Item>,
+ {
+ bridge(self, consumer)
+ }
+
+ fn with_producer<CB>(self, callback: CB) -> CB::Output
+ where
+ CB: ProducerCallback<Self::Item>,
+ {
+ let len = self.base.len();
+ return self.base.with_producer(Callback {
+ chunk_size: self.chunk_size,
+ len,
+ identity: self.identity,
+ fold_op: self.fold_op,
+ callback,
+ });
+
+ struct Callback<CB, ID, F> {
+ chunk_size: usize,
+ len: usize,
+ identity: ID,
+ fold_op: F,
+ callback: CB,
+ }
+
+ impl<T, CB, ID, U, F> ProducerCallback<T> for Callback<CB, ID, F>
+ where
+ CB: ProducerCallback<U>,
+ ID: Fn() -> U + Send + Sync,
+ F: Fn(U, T) -> U + Send + Sync,
+ {
+ type Output = CB::Output;
+
+ fn callback<P>(self, base: P) -> CB::Output
+ where
+ P: Producer<Item = T>,
+ {
+ let identity = &self.identity;
+ let fold_op = &self.fold_op;
+ let fold_iter = move |iter: P::IntoIter| iter.fold(identity(), fold_op);
+ let producer = ChunkProducer::new(self.chunk_size, self.len, base, fold_iter);
+ self.callback.callback(producer)
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use std::ops::Add;
+
+ #[test]
+ fn check_fold_chunks() {
+ let words = "bishbashbosh!"
+ .chars()
+ .collect::<Vec<_>>()
+ .into_par_iter()
+ .fold_chunks(4, String::new, |mut s, c| {
+ s.push(c);
+ s
+ })
+ .collect::<Vec<_>>();
+
+ assert_eq!(words, vec!["bish", "bash", "bosh", "!"]);
+ }
+
+ // 'closure' values for tests below
+ fn id() -> i32 {
+ 0
+ }
+ fn sum<T, U>(x: T, y: U) -> T
+ where
+ T: Add<U, Output = T>,
+ {
+ x + y
+ }
+
+ #[test]
+ #[should_panic(expected = "chunk_size must not be zero")]
+ fn check_fold_chunks_zero_size() {
+ let _: Vec<i32> = vec![1, 2, 3]
+ .into_par_iter()
+ .fold_chunks(0, id, sum)
+ .collect();
+ }
+
+ #[test]
+ fn check_fold_chunks_even_size() {
+ assert_eq!(
+ vec![1 + 2 + 3, 4 + 5 + 6, 7 + 8 + 9],
+ (1..10)
+ .into_par_iter()
+ .fold_chunks(3, id, sum)
+ .collect::<Vec<i32>>()
+ );
+ }
+
+ #[test]
+ fn check_fold_chunks_empty() {
+ let v: Vec<i32> = vec![];
+ let expected: Vec<i32> = vec![];
+ assert_eq!(
+ expected,
+ v.into_par_iter()
+ .fold_chunks(2, id, sum)
+ .collect::<Vec<i32>>()
+ );
+ }
+
+ #[test]
+ fn check_fold_chunks_len() {
+ assert_eq!(4, (0..8).into_par_iter().fold_chunks(2, id, sum).len());
+ assert_eq!(3, (0..9).into_par_iter().fold_chunks(3, id, sum).len());
+ assert_eq!(3, (0..8).into_par_iter().fold_chunks(3, id, sum).len());
+ assert_eq!(1, (&[1]).par_iter().fold_chunks(3, id, sum).len());
+ assert_eq!(0, (0..0).into_par_iter().fold_chunks(3, id, sum).len());
+ }
+
+ #[test]
+ fn check_fold_chunks_uneven() {
+ let cases: Vec<(Vec<u32>, usize, Vec<u32>)> = vec![
+ ((0..5).collect(), 3, vec![0 + 1 + 2, 3 + 4]),
+ (vec![1], 5, vec![1]),
+ ((0..4).collect(), 3, vec![0 + 1 + 2, 3]),
+ ];
+
+ for (i, (v, n, expected)) in cases.into_iter().enumerate() {
+ let mut res: Vec<u32> = vec![];
+ v.par_iter()
+ .fold_chunks(n, || 0, sum)
+ .collect_into_vec(&mut res);
+ assert_eq!(expected, res, "Case {} failed", i);
+
+ res.truncate(0);
+ v.into_par_iter()
+ .fold_chunks(n, || 0, sum)
+ .rev()
+ .collect_into_vec(&mut res);
+ assert_eq!(
+ expected.into_iter().rev().collect::<Vec<u32>>(),
+ res,
+ "Case {} reversed failed",
+ i
+ );
+ }
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/fold_chunks_with.rs b/vendor/rustc-rayon/src/iter/fold_chunks_with.rs
new file mode 100644
index 000000000..af120aec4
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/fold_chunks_with.rs
@@ -0,0 +1,231 @@
+use std::fmt::{self, Debug};
+
+use super::chunks::ChunkProducer;
+use super::plumbing::*;
+use super::*;
+use crate::math::div_round_up;
+
+/// `FoldChunksWith` is an iterator that groups elements of an underlying iterator and applies a
+/// function over them, producing a single value for each group.
+///
+/// This struct is created by the [`fold_chunks_with()`] method on [`IndexedParallelIterator`]
+///
+/// [`fold_chunks_with()`]: trait.IndexedParallelIterator.html#method.fold_chunks
+/// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FoldChunksWith<I, U, F>
+where
+ I: IndexedParallelIterator,
+{
+ base: I,
+ chunk_size: usize,
+ item: U,
+ fold_op: F,
+}
+
+impl<I: IndexedParallelIterator + Debug, U: Debug, F> Debug for FoldChunksWith<I, U, F> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Fold")
+ .field("base", &self.base)
+ .field("chunk_size", &self.chunk_size)
+ .field("item", &self.item)
+ .finish()
+ }
+}
+
+impl<I, U, F> FoldChunksWith<I, U, F>
+where
+ I: IndexedParallelIterator,
+ U: Send + Clone,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+{
+ /// Creates a new `FoldChunksWith` iterator
+ pub(super) fn new(base: I, chunk_size: usize, item: U, fold_op: F) -> Self {
+ FoldChunksWith {
+ base,
+ chunk_size,
+ item,
+ fold_op,
+ }
+ }
+}
+
+impl<I, U, F> ParallelIterator for FoldChunksWith<I, U, F>
+where
+ I: IndexedParallelIterator,
+ U: Send + Clone,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+{
+ type Item = U;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: Consumer<U>,
+ {
+ bridge(self, consumer)
+ }
+
+ fn opt_len(&self) -> Option<usize> {
+ Some(self.len())
+ }
+}
+
+impl<I, U, F> IndexedParallelIterator for FoldChunksWith<I, U, F>
+where
+ I: IndexedParallelIterator,
+ U: Send + Clone,
+ F: Fn(U, I::Item) -> U + Send + Sync,
+{
+ fn len(&self) -> usize {
+ div_round_up(self.base.len(), self.chunk_size)
+ }
+
+ fn drive<C>(self, consumer: C) -> C::Result
+ where
+ C: Consumer<Self::Item>,
+ {
+ bridge(self, consumer)
+ }
+
+ fn with_producer<CB>(self, callback: CB) -> CB::Output
+ where
+ CB: ProducerCallback<Self::Item>,
+ {
+ let len = self.base.len();
+ return self.base.with_producer(Callback {
+ chunk_size: self.chunk_size,
+ len,
+ item: self.item,
+ fold_op: self.fold_op,
+ callback,
+ });
+
+ struct Callback<CB, T, F> {
+ chunk_size: usize,
+ len: usize,
+ item: T,
+ fold_op: F,
+ callback: CB,
+ }
+
+ impl<T, U, F, CB> ProducerCallback<T> for Callback<CB, U, F>
+ where
+ CB: ProducerCallback<U>,
+ U: Send + Clone,
+ F: Fn(U, T) -> U + Send + Sync,
+ {
+ type Output = CB::Output;
+
+ fn callback<P>(self, base: P) -> CB::Output
+ where
+ P: Producer<Item = T>,
+ {
+ let item = self.item;
+ let fold_op = &self.fold_op;
+ let fold_iter = move |iter: P::IntoIter| iter.fold(item.clone(), fold_op);
+ let producer = ChunkProducer::new(self.chunk_size, self.len, base, fold_iter);
+ self.callback.callback(producer)
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use std::ops::Add;
+
+ #[test]
+ fn check_fold_chunks_with() {
+ let words = "bishbashbosh!"
+ .chars()
+ .collect::<Vec<_>>()
+ .into_par_iter()
+ .fold_chunks_with(4, String::new(), |mut s, c| {
+ s.push(c);
+ s
+ })
+ .collect::<Vec<_>>();
+
+ assert_eq!(words, vec!["bish", "bash", "bosh", "!"]);
+ }
+
+ // 'closure' value for tests below
+ fn sum<T, U>(x: T, y: U) -> T
+ where
+ T: Add<U, Output = T>,
+ {
+ x + y
+ }
+
+ #[test]
+ #[should_panic(expected = "chunk_size must not be zero")]
+ fn check_fold_chunks_zero_size() {
+ let _: Vec<i32> = vec![1, 2, 3]
+ .into_par_iter()
+ .fold_chunks_with(0, 0, sum)
+ .collect();
+ }
+
+ #[test]
+ fn check_fold_chunks_even_size() {
+ assert_eq!(
+ vec![1 + 2 + 3, 4 + 5 + 6, 7 + 8 + 9],
+ (1..10)
+ .into_par_iter()
+ .fold_chunks_with(3, 0, sum)
+ .collect::<Vec<i32>>()
+ );
+ }
+
+ #[test]
+ fn check_fold_chunks_with_empty() {
+ let v: Vec<i32> = vec![];
+ let expected: Vec<i32> = vec![];
+ assert_eq!(
+ expected,
+ v.into_par_iter()
+ .fold_chunks_with(2, 0, sum)
+ .collect::<Vec<i32>>()
+ );
+ }
+
+ #[test]
+ fn check_fold_chunks_len() {
+ assert_eq!(4, (0..8).into_par_iter().fold_chunks_with(2, 0, sum).len());
+ assert_eq!(3, (0..9).into_par_iter().fold_chunks_with(3, 0, sum).len());
+ assert_eq!(3, (0..8).into_par_iter().fold_chunks_with(3, 0, sum).len());
+ assert_eq!(1, (&[1]).par_iter().fold_chunks_with(3, 0, sum).len());
+ assert_eq!(0, (0..0).into_par_iter().fold_chunks_with(3, 0, sum).len());
+ }
+
+ #[test]
+ fn check_fold_chunks_uneven() {
+ let cases: Vec<(Vec<u32>, usize, Vec<u32>)> = vec![
+ ((0..5).collect(), 3, vec![0 + 1 + 2, 3 + 4]),
+ (vec![1], 5, vec![1]),
+ ((0..4).collect(), 3, vec![0 + 1 + 2, 3]),
+ ];
+
+ for (i, (v, n, expected)) in cases.into_iter().enumerate() {
+ let mut res: Vec<u32> = vec![];
+ v.par_iter()
+ .fold_chunks_with(n, 0, sum)
+ .collect_into_vec(&mut res);
+ assert_eq!(expected, res, "Case {} failed", i);
+
+ res.truncate(0);
+ v.into_par_iter()
+ .fold_chunks_with(n, 0, sum)
+ .rev()
+ .collect_into_vec(&mut res);
+ assert_eq!(
+ expected.into_iter().rev().collect::<Vec<u32>>(),
+ res,
+ "Case {} reversed failed",
+ i
+ );
+ }
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/inspect.rs b/vendor/rustc-rayon/src/iter/inspect.rs
index 9b1cd094d..c50ca022d 100644
--- a/vendor/rustc-rayon/src/iter/inspect.rs
+++ b/vendor/rustc-rayon/src/iter/inspect.rs
@@ -209,7 +209,7 @@ where
F: Fn(&T) + Sync,
{
fn split_off_left(&self) -> Self {
- InspectConsumer::new(self.base.split_off_left(), &self.inspect_op)
+ InspectConsumer::new(self.base.split_off_left(), self.inspect_op)
}
fn to_reducer(&self) -> Self::Reducer {
diff --git a/vendor/rustc-rayon/src/iter/interleave.rs b/vendor/rustc-rayon/src/iter/interleave.rs
index b5d43d53d..3cacc49f9 100644
--- a/vendor/rustc-rayon/src/iter/interleave.rs
+++ b/vendor/rustc-rayon/src/iter/interleave.rs
@@ -310,16 +310,16 @@ where
{
#[inline]
fn next_back(&mut self) -> Option<I::Item> {
- if self.i.len() == self.j.len() {
- if self.i_next {
- self.i.next_back()
- } else {
- self.j.next_back()
+ match self.i.len().cmp(&self.j.len()) {
+ Ordering::Less => self.j.next_back(),
+ Ordering::Equal => {
+ if self.i_next {
+ self.i.next_back()
+ } else {
+ self.j.next_back()
+ }
}
- } else if self.i.len() < self.j.len() {
- self.j.next_back()
- } else {
- self.i.next_back()
+ Ordering::Greater => self.i.next_back(),
}
}
}
diff --git a/vendor/rustc-rayon/src/iter/map.rs b/vendor/rustc-rayon/src/iter/map.rs
index f2a35ff8c..da14d4082 100644
--- a/vendor/rustc-rayon/src/iter/map.rs
+++ b/vendor/rustc-rayon/src/iter/map.rs
@@ -213,7 +213,7 @@ where
R: Send,
{
fn split_off_left(&self) -> Self {
- MapConsumer::new(self.base.split_off_left(), &self.map_op)
+ MapConsumer::new(self.base.split_off_left(), self.map_op)
}
fn to_reducer(&self) -> Self::Reducer {
diff --git a/vendor/rustc-rayon/src/iter/mod.rs b/vendor/rustc-rayon/src/iter/mod.rs
index 89e96fcef..e60ea1633 100644
--- a/vendor/rustc-rayon/src/iter/mod.rs
+++ b/vendor/rustc-rayon/src/iter/mod.rs
@@ -119,6 +119,8 @@ mod flat_map_iter;
mod flatten;
mod flatten_iter;
mod fold;
+mod fold_chunks;
+mod fold_chunks_with;
mod for_each;
mod from_par_iter;
mod inspect;
@@ -139,9 +141,14 @@ mod reduce;
mod repeat;
mod rev;
mod skip;
+mod skip_any;
+mod skip_any_while;
mod splitter;
+mod step_by;
mod sum;
mod take;
+mod take_any;
+mod take_any_while;
mod try_fold;
mod try_reduce;
mod try_reduce_with;
@@ -165,6 +172,8 @@ pub use self::{
flatten::Flatten,
flatten_iter::FlattenIter,
fold::{Fold, FoldWith},
+ fold_chunks::FoldChunks,
+ fold_chunks_with::FoldChunksWith,
inspect::Inspect,
interleave::Interleave,
interleave_shortest::InterleaveShortest,
@@ -180,8 +189,13 @@ pub use self::{
repeat::{repeat, repeatn, Repeat, RepeatN},
rev::Rev,
skip::Skip,
+ skip_any::SkipAny,
+ skip_any_while::SkipAnyWhile,
splitter::{split, Split},
+ step_by::StepBy,
take::Take,
+ take_any::TakeAny,
+ take_any_while::TakeAnyWhile,
try_fold::{TryFold, TryFoldWith},
update::Update,
while_some::WhileSome,
@@ -189,10 +203,6 @@ pub use self::{
zip_eq::ZipEq,
};
-mod step_by;
-#[cfg(has_step_by_rev)]
-pub use self::step_by::StepBy;
-
/// `IntoParallelIterator` implements the conversion to a [`ParallelIterator`].
///
/// By implementing `IntoParallelIterator` for a type, you define how it will
@@ -1124,7 +1134,7 @@ pub trait ParallelIterator: Sized + Send {
/// multiple sums. The number of results is nondeterministic, as
/// is the point where the breaks occur.
///
- /// So if did the same parallel fold (`fold(0, |a,b| a+b)`) on
+ /// So if we did the same parallel fold (`fold(0, |a,b| a+b)`) on
/// our example list, we might wind up with a sequence of two numbers,
/// like so:
///
@@ -2192,6 +2202,143 @@ pub trait ParallelIterator: Sized + Send {
Intersperse::new(self, element)
}
+ /// Creates an iterator that yields `n` elements from *anywhere* in the original iterator.
+ ///
+ /// This is similar to [`IndexedParallelIterator::take`] without being
+ /// constrained to the "first" `n` of the original iterator order. The
+ /// taken items will still maintain their relative order where that is
+ /// visible in `collect`, `reduce`, and similar outputs.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ ///
+ /// let result: Vec<_> = (0..100)
+ /// .into_par_iter()
+ /// .filter(|&x| x % 2 == 0)
+ /// .take_any(5)
+ /// .collect();
+ ///
+ /// assert_eq!(result.len(), 5);
+ /// assert!(result.windows(2).all(|w| w[0] < w[1]));
+ /// ```
+ fn take_any(self, n: usize) -> TakeAny<Self> {
+ TakeAny::new(self, n)
+ }
+
+ /// Creates an iterator that skips `n` elements from *anywhere* in the original iterator.
+ ///
+ /// This is similar to [`IndexedParallelIterator::skip`] without being
+ /// constrained to the "first" `n` of the original iterator order. The
+ /// remaining items will still maintain their relative order where that is
+ /// visible in `collect`, `reduce`, and similar outputs.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ ///
+ /// let result: Vec<_> = (0..100)
+ /// .into_par_iter()
+ /// .filter(|&x| x % 2 == 0)
+ /// .skip_any(5)
+ /// .collect();
+ ///
+ /// assert_eq!(result.len(), 45);
+ /// assert!(result.windows(2).all(|w| w[0] < w[1]));
+ /// ```
+ fn skip_any(self, n: usize) -> SkipAny<Self> {
+ SkipAny::new(self, n)
+ }
+
+ /// Creates an iterator that takes elements from *anywhere* in the original iterator
+ /// until the given `predicate` returns `false`.
+ ///
+ /// The `predicate` may be anything -- e.g. it could be checking a fact about the item, a
+ /// global condition unrelated to the item itself, or some combination thereof.
+ ///
+ /// If parallel calls to the `predicate` race and give different results, then the
+ /// `true` results will still take those particular items, while respecting the `false`
+ /// result from elsewhere to skip any further items.
+ ///
+ /// This is similar to [`Iterator::take_while`] without being constrained to the original
+ /// iterator order. The taken items will still maintain their relative order where that is
+ /// visible in `collect`, `reduce`, and similar outputs.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ ///
+ /// let result: Vec<_> = (0..100)
+ /// .into_par_iter()
+ /// .take_any_while(|x| *x < 50)
+ /// .collect();
+ ///
+ /// assert!(result.len() <= 50);
+ /// assert!(result.windows(2).all(|w| w[0] < w[1]));
+ /// ```
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ /// use std::sync::atomic::AtomicUsize;
+ /// use std::sync::atomic::Ordering::Relaxed;
+ ///
+ /// // Collect any group of items that sum <= 1000
+ /// let quota = AtomicUsize::new(1000);
+ /// let result: Vec<_> = (0_usize..100)
+ /// .into_par_iter()
+ /// .take_any_while(|&x| {
+ /// quota.fetch_update(Relaxed, Relaxed, |q| q.checked_sub(x))
+ /// .is_ok()
+ /// })
+ /// .collect();
+ ///
+ /// let sum = result.iter().sum::<usize>();
+ /// assert!(matches!(sum, 902..=1000));
+ /// ```
+ fn take_any_while<P>(self, predicate: P) -> TakeAnyWhile<Self, P>
+ where
+ P: Fn(&Self::Item) -> bool + Sync + Send,
+ {
+ TakeAnyWhile::new(self, predicate)
+ }
+
+ /// Creates an iterator that skips elements from *anywhere* in the original iterator
+ /// until the given `predicate` returns `false`.
+ ///
+ /// The `predicate` may be anything -- e.g. it could be checking a fact about the item, a
+ /// global condition unrelated to the item itself, or some combination thereof.
+ ///
+ /// If parallel calls to the `predicate` race and give different results, then the
+ /// `true` results will still skip those particular items, while respecting the `false`
+ /// result from elsewhere to skip any further items.
+ ///
+ /// This is similar to [`Iterator::skip_while`] without being constrained to the original
+ /// iterator order. The remaining items will still maintain their relative order where that is
+ /// visible in `collect`, `reduce`, and similar outputs.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ ///
+ /// let result: Vec<_> = (0..100)
+ /// .into_par_iter()
+ /// .skip_any_while(|x| *x < 50)
+ /// .collect();
+ ///
+ /// assert!(result.len() >= 50);
+ /// assert!(result.windows(2).all(|w| w[0] < w[1]));
+ /// ```
+ fn skip_any_while<P>(self, predicate: P) -> SkipAnyWhile<Self, P>
+ where
+ P: Fn(&Self::Item) -> bool + Sync + Send,
+ {
+ SkipAnyWhile::new(self, predicate)
+ }
+
/// Internal method used to define the behavior of this parallel
/// iterator. You should not need to call this directly.
///
@@ -2241,6 +2388,8 @@ impl<T: ParallelIterator> IntoParallelIterator for T {
/// those points.
///
/// **Note:** Not implemented for `u64`, `i64`, `u128`, or `i128` ranges
+// Waiting for `ExactSizeIterator::is_empty` to be stabilized. See rust-lang/rust#35428
+#[allow(clippy::len_without_is_empty)]
pub trait IndexedParallelIterator: ParallelIterator {
/// Collects the results of the iterator into the specified
/// vector. The vector is always truncated before execution
@@ -2339,13 +2488,18 @@ pub trait IndexedParallelIterator: ParallelIterator {
/// // we should never get here
/// assert_eq!(1, zipped.len());
/// ```
+ #[track_caller]
fn zip_eq<Z>(self, zip_op: Z) -> ZipEq<Self, Z::Iter>
where
Z: IntoParallelIterator,
Z::Iter: IndexedParallelIterator,
{
let zip_op_iter = zip_op.into_par_iter();
- assert_eq!(self.len(), zip_op_iter.len());
+ assert_eq!(
+ self.len(),
+ zip_op_iter.len(),
+ "iterators must have the same length"
+ );
ZipEq::new(self, zip_op_iter)
}
@@ -2410,11 +2564,95 @@ pub trait IndexedParallelIterator: ParallelIterator {
/// let r: Vec<Vec<i32>> = a.into_par_iter().chunks(3).collect();
/// assert_eq!(r, vec![vec![1,2,3], vec![4,5,6], vec![7,8,9], vec![10]]);
/// ```
+ #[track_caller]
fn chunks(self, chunk_size: usize) -> Chunks<Self> {
assert!(chunk_size != 0, "chunk_size must not be zero");
Chunks::new(self, chunk_size)
}
+ /// Splits an iterator into fixed-size chunks, performing a sequential [`fold()`] on
+ /// each chunk.
+ ///
+ /// Returns an iterator that produces a folded result for each chunk of items
+ /// produced by this iterator.
+ ///
+ /// This works essentially like:
+ ///
+ /// ```text
+ /// iter.chunks(chunk_size)
+ /// .map(|chunk|
+ /// chunk.into_iter()
+ /// .fold(identity, fold_op)
+ /// )
+ /// ```
+ ///
+ /// except there is no per-chunk allocation overhead.
+ ///
+ /// [`fold()`]: std::iter::Iterator#method.fold
+ ///
+ /// **Panics** if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ /// let nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ /// let chunk_sums = nums.into_par_iter().fold_chunks(2, || 0, |a, n| a + n).collect::<Vec<_>>();
+ /// assert_eq!(chunk_sums, vec![3, 7, 11, 15, 19]);
+ /// ```
+ #[track_caller]
+ fn fold_chunks<T, ID, F>(
+ self,
+ chunk_size: usize,
+ identity: ID,
+ fold_op: F,
+ ) -> FoldChunks<Self, ID, F>
+ where
+ ID: Fn() -> T + Send + Sync,
+ F: Fn(T, Self::Item) -> T + Send + Sync,
+ T: Send,
+ {
+ assert!(chunk_size != 0, "chunk_size must not be zero");
+ FoldChunks::new(self, chunk_size, identity, fold_op)
+ }
+
+ /// Splits an iterator into fixed-size chunks, performing a sequential [`fold()`] on
+ /// each chunk.
+ ///
+ /// Returns an iterator that produces a folded result for each chunk of items
+ /// produced by this iterator.
+ ///
+ /// This works essentially like `fold_chunks(chunk_size, || init.clone(), fold_op)`,
+ /// except it doesn't require the `init` type to be `Sync`, nor any other form of
+ /// added synchronization.
+ ///
+ /// [`fold()`]: std::iter::Iterator#method.fold
+ ///
+ /// **Panics** if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use rayon::prelude::*;
+ /// let nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ /// let chunk_sums = nums.into_par_iter().fold_chunks_with(2, 0, |a, n| a + n).collect::<Vec<_>>();
+ /// assert_eq!(chunk_sums, vec![3, 7, 11, 15, 19]);
+ /// ```
+ #[track_caller]
+ fn fold_chunks_with<T, F>(
+ self,
+ chunk_size: usize,
+ init: T,
+ fold_op: F,
+ ) -> FoldChunksWith<Self, T, F>
+ where
+ T: Send + Clone,
+ F: Fn(T, Self::Item) -> T + Send + Sync,
+ {
+ assert!(chunk_size != 0, "chunk_size must not be zero");
+ FoldChunksWith::new(self, chunk_size, init, fold_op)
+ }
+
/// Lexicographically compares the elements of this `ParallelIterator` with those of
/// another.
///
@@ -2601,11 +2839,6 @@ pub trait IndexedParallelIterator: ParallelIterator {
///
/// assert_eq!(result, [3, 6, 9])
/// ```
- ///
- /// # Compatibility
- ///
- /// This method is only available on Rust 1.38 or greater.
- #[cfg(has_step_by_rev)]
fn step_by(self, step: usize) -> StepBy<Self> {
StepBy::new(self, step)
}
@@ -3146,20 +3379,9 @@ pub trait ParallelDrainRange<Idx = usize> {
/// stable clone of the standard library's `Try` trait, as yet unstable.
mod private {
use std::convert::Infallible;
+ use std::ops::ControlFlow::{self, Break, Continue};
use std::task::Poll;
- #[cfg(has_control_flow)]
- pub(crate) use std::ops::ControlFlow;
-
- #[cfg(not(has_control_flow))]
- #[allow(missing_debug_implementations)]
- pub enum ControlFlow<B, C = ()> {
- Continue(C),
- Break(B),
- }
-
- use self::ControlFlow::{Break, Continue};
-
/// Clone of `std::ops::Try`.
///
/// Implementing this trait is not permitted outside of `rayon`.
@@ -3176,7 +3398,6 @@ mod private {
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
}
- #[cfg(has_control_flow)]
impl<B, C> Try for ControlFlow<B, C> {
private_impl! {}
diff --git a/vendor/rustc-rayon/src/iter/par_bridge.rs b/vendor/rustc-rayon/src/iter/par_bridge.rs
index 339ac1a32..8398274b3 100644
--- a/vendor/rustc-rayon/src/iter/par_bridge.rs
+++ b/vendor/rustc-rayon/src/iter/par_bridge.rs
@@ -1,12 +1,9 @@
-use crossbeam_deque::{Steal, Stealer, Worker};
-
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
-use std::sync::{Mutex, TryLockError};
-use std::thread::yield_now;
+use std::sync::Mutex;
-use crate::current_num_threads;
use crate::iter::plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer};
use crate::iter::ParallelIterator;
+use crate::{current_num_threads, current_thread_index};
/// Conversion trait to convert an `Iterator` to a `ParallelIterator`.
///
@@ -78,71 +75,46 @@ where
where
C: UnindexedConsumer<Self::Item>,
{
- let split_count = AtomicUsize::new(current_num_threads());
- let worker = Worker::new_fifo();
- let stealer = worker.stealer();
- let done = AtomicBool::new(false);
- let iter = Mutex::new((self.iter, worker));
+ let num_threads = current_num_threads();
+ let threads_started: Vec<_> = (0..num_threads).map(|_| AtomicBool::new(false)).collect();
bridge_unindexed(
- IterParallelProducer {
- split_count: &split_count,
- done: &done,
- iter: &iter,
- items: stealer,
+ &IterParallelProducer {
+ split_count: AtomicUsize::new(num_threads),
+ iter: Mutex::new(self.iter.fuse()),
+ threads_started: &threads_started,
},
consumer,
)
}
}
-struct IterParallelProducer<'a, Iter: Iterator> {
- split_count: &'a AtomicUsize,
- done: &'a AtomicBool,
- iter: &'a Mutex<(Iter, Worker<Iter::Item>)>,
- items: Stealer<Iter::Item>,
-}
-
-// manual clone because T doesn't need to be Clone, but the derive assumes it should be
-impl<'a, Iter: Iterator + 'a> Clone for IterParallelProducer<'a, Iter> {
- fn clone(&self) -> Self {
- IterParallelProducer {
- split_count: self.split_count,
- done: self.done,
- iter: self.iter,
- items: self.items.clone(),
- }
- }
+struct IterParallelProducer<'a, Iter> {
+ split_count: AtomicUsize,
+ iter: Mutex<std::iter::Fuse<Iter>>,
+ threads_started: &'a [AtomicBool],
}
-impl<'a, Iter: Iterator + Send + 'a> UnindexedProducer for IterParallelProducer<'a, Iter>
-where
- Iter::Item: Send,
-{
+impl<Iter: Iterator + Send> UnindexedProducer for &IterParallelProducer<'_, Iter> {
type Item = Iter::Item;
fn split(self) -> (Self, Option<Self>) {
let mut count = self.split_count.load(Ordering::SeqCst);
loop {
- // Check if the iterator is exhausted *and* we've consumed every item from it.
- let done = self.done.load(Ordering::SeqCst) && self.items.is_empty();
-
- match count.checked_sub(1) {
- Some(new_count) if !done => {
- match self.split_count.compare_exchange_weak(
- count,
- new_count,
- Ordering::SeqCst,
- Ordering::SeqCst,
- ) {
- Ok(_) => return (self.clone(), Some(self)),
- Err(last_count) => count = last_count,
- }
- }
- _ => {
- return (self, None);
+ // Check if the iterator is exhausted
+ if let Some(new_count) = count.checked_sub(1) {
+ match self.split_count.compare_exchange_weak(
+ count,
+ new_count,
+ Ordering::SeqCst,
+ Ordering::SeqCst,
+ ) {
+ Ok(_) => return (self, Some(self)),
+ Err(last_count) => count = last_count,
}
+ } else {
+ return (self, None);
}
}
}
@@ -151,66 +123,39 @@ where
where
F: Folder<Self::Item>,
{
+ // Guard against work-stealing-induced recursion, in case `Iter::next()`
+ // calls rayon internally, so we don't deadlock our mutex. We might also
+ // be recursing via `folder` methods, which doesn't present a mutex hazard,
+ // but it's lower overhead for us to just check this once, rather than
+ // updating additional shared state on every mutex lock/unlock.
+ // (If this isn't a rayon thread, then there's no work-stealing anyway...)
+ if let Some(i) = current_thread_index() {
+ // Note: If the number of threads in the pool ever grows dynamically, then
+ // we'll end up sharing flags and may falsely detect recursion -- that's
+ // still fine for overall correctness, just not optimal for parallelism.
+ let thread_started = &self.threads_started[i % self.threads_started.len()];
+ if thread_started.swap(true, Ordering::Relaxed) {
+ // We can't make progress with a nested mutex, so just return and let
+ // the outermost loop continue with the rest of the iterator items.
+ return folder;
+ }
+ }
+
loop {
- match self.items.steal() {
- Steal::Success(it) => {
+ if let Ok(mut iter) = self.iter.lock() {
+ if let Some(it) = iter.next() {
+ drop(iter);
folder = folder.consume(it);
if folder.full() {
return folder;
}
+ } else {
+ return folder;
}
- Steal::Empty => {
- // Don't storm the mutex if we're already done.
- if self.done.load(Ordering::SeqCst) {
- // Someone might have pushed more between our `steal()` and `done.load()`
- if self.items.is_empty() {
- // The iterator is out of items, no use in continuing
- return folder;
- }
- } else {
- // our cache is out of items, time to load more from the iterator
- match self.iter.try_lock() {
- Ok(mut guard) => {
- // Check `done` again in case we raced with the previous lock
- // holder on its way out.
- if self.done.load(Ordering::SeqCst) {
- if self.items.is_empty() {
- return folder;
- }
- continue;
- }
-
- let count = current_num_threads();
- let count = (count * count) * 2;
-
- let (ref mut iter, ref worker) = *guard;
-
- // while worker.len() < count {
- // FIXME the new deque doesn't let us count items. We can just
- // push a number of items, but that doesn't consider active
- // stealers elsewhere.
- for _ in 0..count {
- if let Some(it) = iter.next() {
- worker.push(it);
- } else {
- self.done.store(true, Ordering::SeqCst);
- break;
- }
- }
- }
- Err(TryLockError::WouldBlock) => {
- // someone else has the mutex, just sit tight until it's ready
- yield_now(); //TODO: use a thread-pool-aware yield? (#548)
- }
- Err(TryLockError::Poisoned(_)) => {
- // any panics from other threads will have been caught by the pool,
- // and will be re-thrown when joined - just exit
- return folder;
- }
- }
- }
- }
- Steal::Retry => (),
+ } else {
+ // any panics from other threads will have been caught by the pool,
+ // and will be re-thrown when joined - just exit
+ return folder;
}
}
}
diff --git a/vendor/rustc-rayon/src/iter/plumbing/README.md b/vendor/rustc-rayon/src/iter/plumbing/README.md
index cd94eae30..42d22effe 100644
--- a/vendor/rustc-rayon/src/iter/plumbing/README.md
+++ b/vendor/rustc-rayon/src/iter/plumbing/README.md
@@ -35,8 +35,8 @@ modes (which is why there are two):
more like a `for_each` call: each time a new item is produced, the
`consume` method is called with that item. (The traits themselves are
a bit more complex, as they support state that can be threaded
- through and ultimately reduced.) Unlike producers, there are two
- variants of consumers. The difference is how the split is performed:
+ through and ultimately reduced.) Like producers, there are two
+ variants of consumers which differ in how the split is performed:
- in the `Consumer` trait, splitting is done with `split_at`, which
accepts an index where the split should be performed. All
iterators can work in this mode. The resulting halves thus have an
@@ -124,7 +124,7 @@ implement `IndexedParallelIterator`.
The `bridge` function will then connect the consumer, which is
handling the `flat_map` and `for_each`, with the producer, which is
-handling the `zip` and its preecessors. It will split down until the
+handling the `zip` and its predecessors. It will split down until the
chunks seem reasonably small, then pull items from the producer and
feed them to the consumer.
diff --git a/vendor/rustc-rayon/src/iter/skip_any.rs b/vendor/rustc-rayon/src/iter/skip_any.rs
new file mode 100644
index 000000000..0660a564a
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/skip_any.rs
@@ -0,0 +1,144 @@
+use super::plumbing::*;
+use super::*;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+/// `SkipAny` is an iterator that skips over `n` elements from anywhere in `I`.
+/// This struct is created by the [`skip_any()`] method on [`ParallelIterator`]
+///
+/// [`skip_any()`]: trait.ParallelIterator.html#method.skip_any
+/// [`ParallelIterator`]: trait.ParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone, Debug)]
+pub struct SkipAny<I: ParallelIterator> {
+ base: I,
+ count: usize,
+}
+
+impl<I> SkipAny<I>
+where
+ I: ParallelIterator,
+{
+ /// Creates a new `SkipAny` iterator.
+ pub(super) fn new(base: I, count: usize) -> Self {
+ SkipAny { base, count }
+ }
+}
+
+impl<I> ParallelIterator for SkipAny<I>
+where
+ I: ParallelIterator,
+{
+ type Item = I::Item;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: UnindexedConsumer<Self::Item>,
+ {
+ let consumer1 = SkipAnyConsumer {
+ base: consumer,
+ count: &AtomicUsize::new(self.count),
+ };
+ self.base.drive_unindexed(consumer1)
+ }
+}
+
+/// ////////////////////////////////////////////////////////////////////////
+/// Consumer implementation
+
+struct SkipAnyConsumer<'f, C> {
+ base: C,
+ count: &'f AtomicUsize,
+}
+
+impl<'f, T, C> Consumer<T> for SkipAnyConsumer<'f, C>
+where
+ C: Consumer<T>,
+ T: Send,
+{
+ type Folder = SkipAnyFolder<'f, C::Folder>;
+ type Reducer = C::Reducer;
+ type Result = C::Result;
+
+ fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
+ let (left, right, reducer) = self.base.split_at(index);
+ (
+ SkipAnyConsumer { base: left, ..self },
+ SkipAnyConsumer {
+ base: right,
+ ..self
+ },
+ reducer,
+ )
+ }
+
+ fn into_folder(self) -> Self::Folder {
+ SkipAnyFolder {
+ base: self.base.into_folder(),
+ count: self.count,
+ }
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}
+
+impl<'f, T, C> UnindexedConsumer<T> for SkipAnyConsumer<'f, C>
+where
+ C: UnindexedConsumer<T>,
+ T: Send,
+{
+ fn split_off_left(&self) -> Self {
+ SkipAnyConsumer {
+ base: self.base.split_off_left(),
+ ..*self
+ }
+ }
+
+ fn to_reducer(&self) -> Self::Reducer {
+ self.base.to_reducer()
+ }
+}
+
+struct SkipAnyFolder<'f, C> {
+ base: C,
+ count: &'f AtomicUsize,
+}
+
+fn checked_decrement(u: &AtomicUsize) -> bool {
+ u.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |u| u.checked_sub(1))
+ .is_ok()
+}
+
+impl<'f, T, C> Folder<T> for SkipAnyFolder<'f, C>
+where
+ C: Folder<T>,
+{
+ type Result = C::Result;
+
+ fn consume(mut self, item: T) -> Self {
+ if !checked_decrement(self.count) {
+ self.base = self.base.consume(item);
+ }
+ self
+ }
+
+ fn consume_iter<I>(mut self, iter: I) -> Self
+ where
+ I: IntoIterator<Item = T>,
+ {
+ self.base = self.base.consume_iter(
+ iter.into_iter()
+ .skip_while(move |_| checked_decrement(self.count)),
+ );
+ self
+ }
+
+ fn complete(self) -> C::Result {
+ self.base.complete()
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/skip_any_while.rs b/vendor/rustc-rayon/src/iter/skip_any_while.rs
new file mode 100644
index 000000000..28b9e5973
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/skip_any_while.rs
@@ -0,0 +1,166 @@
+use super::plumbing::*;
+use super::*;
+use std::fmt;
+use std::sync::atomic::{AtomicBool, Ordering};
+
+/// `SkipAnyWhile` is an iterator that skips over elements from anywhere in `I`
+/// until the callback returns `false`.
+/// This struct is created by the [`skip_any_while()`] method on [`ParallelIterator`]
+///
+/// [`skip_any_while()`]: trait.ParallelIterator.html#method.skip_any_while
+/// [`ParallelIterator`]: trait.ParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct SkipAnyWhile<I: ParallelIterator, P> {
+ base: I,
+ predicate: P,
+}
+
+impl<I: ParallelIterator + fmt::Debug, P> fmt::Debug for SkipAnyWhile<I, P> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("SkipAnyWhile")
+ .field("base", &self.base)
+ .finish()
+ }
+}
+
+impl<I, P> SkipAnyWhile<I, P>
+where
+ I: ParallelIterator,
+{
+ /// Creates a new `SkipAnyWhile` iterator.
+ pub(super) fn new(base: I, predicate: P) -> Self {
+ SkipAnyWhile { base, predicate }
+ }
+}
+
+impl<I, P> ParallelIterator for SkipAnyWhile<I, P>
+where
+ I: ParallelIterator,
+ P: Fn(&I::Item) -> bool + Sync + Send,
+{
+ type Item = I::Item;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: UnindexedConsumer<Self::Item>,
+ {
+ let consumer1 = SkipAnyWhileConsumer {
+ base: consumer,
+ predicate: &self.predicate,
+ skipping: &AtomicBool::new(true),
+ };
+ self.base.drive_unindexed(consumer1)
+ }
+}
+
+/// ////////////////////////////////////////////////////////////////////////
+/// Consumer implementation
+
+struct SkipAnyWhileConsumer<'p, C, P> {
+ base: C,
+ predicate: &'p P,
+ skipping: &'p AtomicBool,
+}
+
+impl<'p, T, C, P> Consumer<T> for SkipAnyWhileConsumer<'p, C, P>
+where
+ C: Consumer<T>,
+ P: Fn(&T) -> bool + Sync,
+{
+ type Folder = SkipAnyWhileFolder<'p, C::Folder, P>;
+ type Reducer = C::Reducer;
+ type Result = C::Result;
+
+ fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
+ let (left, right, reducer) = self.base.split_at(index);
+ (
+ SkipAnyWhileConsumer { base: left, ..self },
+ SkipAnyWhileConsumer {
+ base: right,
+ ..self
+ },
+ reducer,
+ )
+ }
+
+ fn into_folder(self) -> Self::Folder {
+ SkipAnyWhileFolder {
+ base: self.base.into_folder(),
+ predicate: self.predicate,
+ skipping: self.skipping,
+ }
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}
+
+impl<'p, T, C, P> UnindexedConsumer<T> for SkipAnyWhileConsumer<'p, C, P>
+where
+ C: UnindexedConsumer<T>,
+ P: Fn(&T) -> bool + Sync,
+{
+ fn split_off_left(&self) -> Self {
+ SkipAnyWhileConsumer {
+ base: self.base.split_off_left(),
+ ..*self
+ }
+ }
+
+ fn to_reducer(&self) -> Self::Reducer {
+ self.base.to_reducer()
+ }
+}
+
+struct SkipAnyWhileFolder<'p, C, P> {
+ base: C,
+ predicate: &'p P,
+ skipping: &'p AtomicBool,
+}
+
+fn skip<T>(item: &T, skipping: &AtomicBool, predicate: &impl Fn(&T) -> bool) -> bool {
+ if !skipping.load(Ordering::Relaxed) {
+ return false;
+ }
+ if predicate(item) {
+ return true;
+ }
+ skipping.store(false, Ordering::Relaxed);
+ false
+}
+
+impl<'p, T, C, P> Folder<T> for SkipAnyWhileFolder<'p, C, P>
+where
+ C: Folder<T>,
+ P: Fn(&T) -> bool + 'p,
+{
+ type Result = C::Result;
+
+ fn consume(mut self, item: T) -> Self {
+ if !skip(&item, self.skipping, self.predicate) {
+ self.base = self.base.consume(item);
+ }
+ self
+ }
+
+ fn consume_iter<I>(mut self, iter: I) -> Self
+ where
+ I: IntoIterator<Item = T>,
+ {
+ self.base = self.base.consume_iter(
+ iter.into_iter()
+ .skip_while(move |x| skip(x, self.skipping, self.predicate)),
+ );
+ self
+ }
+
+ fn complete(self) -> C::Result {
+ self.base.complete()
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/step_by.rs b/vendor/rustc-rayon/src/iter/step_by.rs
index 863de3cf0..94b8334e8 100644
--- a/vendor/rustc-rayon/src/iter/step_by.rs
+++ b/vendor/rustc-rayon/src/iter/step_by.rs
@@ -1,4 +1,3 @@
-#![cfg(has_step_by_rev)]
use std::cmp::min;
use super::plumbing::*;
diff --git a/vendor/rustc-rayon/src/iter/take_any.rs b/vendor/rustc-rayon/src/iter/take_any.rs
new file mode 100644
index 000000000..e3992b383
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/take_any.rs
@@ -0,0 +1,144 @@
+use super::plumbing::*;
+use super::*;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+/// `TakeAny` is an iterator that iterates over `n` elements from anywhere in `I`.
+/// This struct is created by the [`take_any()`] method on [`ParallelIterator`]
+///
+/// [`take_any()`]: trait.ParallelIterator.html#method.take_any
+/// [`ParallelIterator`]: trait.ParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone, Debug)]
+pub struct TakeAny<I: ParallelIterator> {
+ base: I,
+ count: usize,
+}
+
+impl<I> TakeAny<I>
+where
+ I: ParallelIterator,
+{
+ /// Creates a new `TakeAny` iterator.
+ pub(super) fn new(base: I, count: usize) -> Self {
+ TakeAny { base, count }
+ }
+}
+
+impl<I> ParallelIterator for TakeAny<I>
+where
+ I: ParallelIterator,
+{
+ type Item = I::Item;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: UnindexedConsumer<Self::Item>,
+ {
+ let consumer1 = TakeAnyConsumer {
+ base: consumer,
+ count: &AtomicUsize::new(self.count),
+ };
+ self.base.drive_unindexed(consumer1)
+ }
+}
+
+/// ////////////////////////////////////////////////////////////////////////
+/// Consumer implementation
+
+struct TakeAnyConsumer<'f, C> {
+ base: C,
+ count: &'f AtomicUsize,
+}
+
+impl<'f, T, C> Consumer<T> for TakeAnyConsumer<'f, C>
+where
+ C: Consumer<T>,
+ T: Send,
+{
+ type Folder = TakeAnyFolder<'f, C::Folder>;
+ type Reducer = C::Reducer;
+ type Result = C::Result;
+
+ fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
+ let (left, right, reducer) = self.base.split_at(index);
+ (
+ TakeAnyConsumer { base: left, ..self },
+ TakeAnyConsumer {
+ base: right,
+ ..self
+ },
+ reducer,
+ )
+ }
+
+ fn into_folder(self) -> Self::Folder {
+ TakeAnyFolder {
+ base: self.base.into_folder(),
+ count: self.count,
+ }
+ }
+
+ fn full(&self) -> bool {
+ self.count.load(Ordering::Relaxed) == 0 || self.base.full()
+ }
+}
+
+impl<'f, T, C> UnindexedConsumer<T> for TakeAnyConsumer<'f, C>
+where
+ C: UnindexedConsumer<T>,
+ T: Send,
+{
+ fn split_off_left(&self) -> Self {
+ TakeAnyConsumer {
+ base: self.base.split_off_left(),
+ ..*self
+ }
+ }
+
+ fn to_reducer(&self) -> Self::Reducer {
+ self.base.to_reducer()
+ }
+}
+
+struct TakeAnyFolder<'f, C> {
+ base: C,
+ count: &'f AtomicUsize,
+}
+
+fn checked_decrement(u: &AtomicUsize) -> bool {
+ u.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |u| u.checked_sub(1))
+ .is_ok()
+}
+
+impl<'f, T, C> Folder<T> for TakeAnyFolder<'f, C>
+where
+ C: Folder<T>,
+{
+ type Result = C::Result;
+
+ fn consume(mut self, item: T) -> Self {
+ if checked_decrement(self.count) {
+ self.base = self.base.consume(item);
+ }
+ self
+ }
+
+ fn consume_iter<I>(mut self, iter: I) -> Self
+ where
+ I: IntoIterator<Item = T>,
+ {
+ self.base = self.base.consume_iter(
+ iter.into_iter()
+ .take_while(move |_| checked_decrement(self.count)),
+ );
+ self
+ }
+
+ fn complete(self) -> C::Result {
+ self.base.complete()
+ }
+
+ fn full(&self) -> bool {
+ self.count.load(Ordering::Relaxed) == 0 || self.base.full()
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/take_any_while.rs b/vendor/rustc-rayon/src/iter/take_any_while.rs
new file mode 100644
index 000000000..e6a91afab
--- /dev/null
+++ b/vendor/rustc-rayon/src/iter/take_any_while.rs
@@ -0,0 +1,166 @@
+use super::plumbing::*;
+use super::*;
+use std::fmt;
+use std::sync::atomic::{AtomicBool, Ordering};
+
+/// `TakeAnyWhile` is an iterator that iterates over elements from anywhere in `I`
+/// until the callback returns `false`.
+/// This struct is created by the [`take_any_while()`] method on [`ParallelIterator`]
+///
+/// [`take_any_while()`]: trait.ParallelIterator.html#method.take_any_while
+/// [`ParallelIterator`]: trait.ParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct TakeAnyWhile<I: ParallelIterator, P> {
+ base: I,
+ predicate: P,
+}
+
+impl<I: ParallelIterator + fmt::Debug, P> fmt::Debug for TakeAnyWhile<I, P> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TakeAnyWhile")
+ .field("base", &self.base)
+ .finish()
+ }
+}
+
+impl<I, P> TakeAnyWhile<I, P>
+where
+ I: ParallelIterator,
+{
+ /// Creates a new `TakeAnyWhile` iterator.
+ pub(super) fn new(base: I, predicate: P) -> Self {
+ TakeAnyWhile { base, predicate }
+ }
+}
+
+impl<I, P> ParallelIterator for TakeAnyWhile<I, P>
+where
+ I: ParallelIterator,
+ P: Fn(&I::Item) -> bool + Sync + Send,
+{
+ type Item = I::Item;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: UnindexedConsumer<Self::Item>,
+ {
+ let consumer1 = TakeAnyWhileConsumer {
+ base: consumer,
+ predicate: &self.predicate,
+ taking: &AtomicBool::new(true),
+ };
+ self.base.drive_unindexed(consumer1)
+ }
+}
+
+/// ////////////////////////////////////////////////////////////////////////
+/// Consumer implementation
+
+struct TakeAnyWhileConsumer<'p, C, P> {
+ base: C,
+ predicate: &'p P,
+ taking: &'p AtomicBool,
+}
+
+impl<'p, T, C, P> Consumer<T> for TakeAnyWhileConsumer<'p, C, P>
+where
+ C: Consumer<T>,
+ P: Fn(&T) -> bool + Sync,
+{
+ type Folder = TakeAnyWhileFolder<'p, C::Folder, P>;
+ type Reducer = C::Reducer;
+ type Result = C::Result;
+
+ fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
+ let (left, right, reducer) = self.base.split_at(index);
+ (
+ TakeAnyWhileConsumer { base: left, ..self },
+ TakeAnyWhileConsumer {
+ base: right,
+ ..self
+ },
+ reducer,
+ )
+ }
+
+ fn into_folder(self) -> Self::Folder {
+ TakeAnyWhileFolder {
+ base: self.base.into_folder(),
+ predicate: self.predicate,
+ taking: self.taking,
+ }
+ }
+
+ fn full(&self) -> bool {
+ !self.taking.load(Ordering::Relaxed) || self.base.full()
+ }
+}
+
+impl<'p, T, C, P> UnindexedConsumer<T> for TakeAnyWhileConsumer<'p, C, P>
+where
+ C: UnindexedConsumer<T>,
+ P: Fn(&T) -> bool + Sync,
+{
+ fn split_off_left(&self) -> Self {
+ TakeAnyWhileConsumer {
+ base: self.base.split_off_left(),
+ ..*self
+ }
+ }
+
+ fn to_reducer(&self) -> Self::Reducer {
+ self.base.to_reducer()
+ }
+}
+
+struct TakeAnyWhileFolder<'p, C, P> {
+ base: C,
+ predicate: &'p P,
+ taking: &'p AtomicBool,
+}
+
+fn take<T>(item: &T, taking: &AtomicBool, predicate: &impl Fn(&T) -> bool) -> bool {
+ if !taking.load(Ordering::Relaxed) {
+ return false;
+ }
+ if predicate(item) {
+ return true;
+ }
+ taking.store(false, Ordering::Relaxed);
+ false
+}
+
+impl<'p, T, C, P> Folder<T> for TakeAnyWhileFolder<'p, C, P>
+where
+ C: Folder<T>,
+ P: Fn(&T) -> bool + 'p,
+{
+ type Result = C::Result;
+
+ fn consume(mut self, item: T) -> Self {
+ if take(&item, self.taking, self.predicate) {
+ self.base = self.base.consume(item);
+ }
+ self
+ }
+
+ fn consume_iter<I>(mut self, iter: I) -> Self
+ where
+ I: IntoIterator<Item = T>,
+ {
+ self.base = self.base.consume_iter(
+ iter.into_iter()
+ .take_while(move |x| take(x, self.taking, self.predicate)),
+ );
+ self
+ }
+
+ fn complete(self) -> C::Result {
+ self.base.complete()
+ }
+
+ fn full(&self) -> bool {
+ !self.taking.load(Ordering::Relaxed) || self.base.full()
+ }
+}
diff --git a/vendor/rustc-rayon/src/iter/test.rs b/vendor/rustc-rayon/src/iter/test.rs
index bc5106bec..c72068df7 100644
--- a/vendor/rustc-rayon/src/iter/test.rs
+++ b/vendor/rustc-rayon/src/iter/test.rs
@@ -117,13 +117,10 @@ fn fold_map_reduce() {
let r1 = (0_i32..32)
.into_par_iter()
.with_max_len(1)
- .fold(
- || vec![],
- |mut v, e| {
- v.push(e);
- v
- },
- )
+ .fold(Vec::new, |mut v, e| {
+ v.push(e);
+ v
+ })
.map(|v| vec![v])
.reduce_with(|mut v_a, v_b| {
v_a.extend(v_b);
@@ -394,7 +391,7 @@ fn check_slice_mut_indexed() {
#[test]
fn check_vec_indexed() {
let a = vec![1, 2, 3];
- is_indexed(a.clone().into_par_iter());
+ is_indexed(a.into_par_iter());
}
#[test]
@@ -471,6 +468,7 @@ fn check_cmp_gt_to_seq() {
}
#[test]
+#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn check_cmp_short_circuit() {
// We only use a single thread in order to make the short-circuit behavior deterministic.
let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
@@ -500,6 +498,7 @@ fn check_cmp_short_circuit() {
}
#[test]
+#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn check_partial_cmp_short_circuit() {
// We only use a single thread to make the short-circuit behavior deterministic.
let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
@@ -529,6 +528,7 @@ fn check_partial_cmp_short_circuit() {
}
#[test]
+#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn check_partial_cmp_nan_short_circuit() {
// We only use a single thread to make the short-circuit behavior deterministic.
let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
@@ -1371,10 +1371,10 @@ fn check_find_is_present() {
let counter = AtomicUsize::new(0);
let value: Option<i32> = (0_i32..2048).into_par_iter().find_any(|&p| {
counter.fetch_add(1, Ordering::SeqCst);
- p >= 1024 && p < 1096
+ (1024..1096).contains(&p)
});
let q = value.unwrap();
- assert!(q >= 1024 && q < 1096);
+ assert!((1024..1096).contains(&q));
assert!(counter.load(Ordering::SeqCst) < 2048); // should not have visited every single one
}
@@ -1892,7 +1892,7 @@ fn check_either() {
// try an indexed iterator
let left: E = Either::Left(v.clone().into_par_iter());
- assert!(left.enumerate().eq(v.clone().into_par_iter().enumerate()));
+ assert!(left.enumerate().eq(v.into_par_iter().enumerate()));
}
#[test]
@@ -2063,7 +2063,7 @@ fn check_chunks_len() {
assert_eq!(4, (0..8).into_par_iter().chunks(2).len());
assert_eq!(3, (0..9).into_par_iter().chunks(3).len());
assert_eq!(3, (0..8).into_par_iter().chunks(3).len());
- assert_eq!(1, (&[1]).par_iter().chunks(3).len());
+ assert_eq!(1, [1].par_iter().chunks(3).len());
assert_eq!(0, (0..0).into_par_iter().chunks(3).len());
}
diff --git a/vendor/rustc-rayon/src/iter/try_fold.rs b/vendor/rustc-rayon/src/iter/try_fold.rs
index c1881d10f..6d1048d75 100644
--- a/vendor/rustc-rayon/src/iter/try_fold.rs
+++ b/vendor/rustc-rayon/src/iter/try_fold.rs
@@ -2,10 +2,9 @@ use super::plumbing::*;
use super::ParallelIterator;
use super::Try;
-use super::private::ControlFlow::{self, Break, Continue};
-
use std::fmt::{self, Debug};
use std::marker::PhantomData;
+use std::ops::ControlFlow::{self, Break, Continue};
impl<U, I, ID, F> TryFold<I, U, ID, F>
where
diff --git a/vendor/rustc-rayon/src/iter/try_reduce.rs b/vendor/rustc-rayon/src/iter/try_reduce.rs
index 7bf6cc4a5..35a724c94 100644
--- a/vendor/rustc-rayon/src/iter/try_reduce.rs
+++ b/vendor/rustc-rayon/src/iter/try_reduce.rs
@@ -2,8 +2,7 @@ use super::plumbing::*;
use super::ParallelIterator;
use super::Try;
-use super::private::ControlFlow::{self, Break, Continue};
-
+use std::ops::ControlFlow::{self, Break, Continue};
use std::sync::atomic::{AtomicBool, Ordering};
pub(super) fn try_reduce<PI, R, ID, T>(pi: PI, identity: ID, reduce_op: R) -> T
diff --git a/vendor/rustc-rayon/src/iter/try_reduce_with.rs b/vendor/rustc-rayon/src/iter/try_reduce_with.rs
index 9fe34d61d..cd7c83e27 100644
--- a/vendor/rustc-rayon/src/iter/try_reduce_with.rs
+++ b/vendor/rustc-rayon/src/iter/try_reduce_with.rs
@@ -2,8 +2,7 @@ use super::plumbing::*;
use super::ParallelIterator;
use super::Try;
-use super::private::ControlFlow::{self, Break, Continue};
-
+use std::ops::ControlFlow::{self, Break, Continue};
use std::sync::atomic::{AtomicBool, Ordering};
pub(super) fn try_reduce_with<PI, R, T>(pi: PI, reduce_op: R) -> Option<T>
diff --git a/vendor/rustc-rayon/src/iter/update.rs b/vendor/rustc-rayon/src/iter/update.rs
index 373a4d772..c693ac8d6 100644
--- a/vendor/rustc-rayon/src/iter/update.rs
+++ b/vendor/rustc-rayon/src/iter/update.rs
@@ -210,7 +210,7 @@ where
F: Fn(&mut T) + Send + Sync,
{
fn split_off_left(&self) -> Self {
- UpdateConsumer::new(self.base.split_off_left(), &self.update_op)
+ UpdateConsumer::new(self.base.split_off_left(), self.update_op)
}
fn to_reducer(&self) -> Self::Reducer {
diff --git a/vendor/rustc-rayon/src/lib.rs b/vendor/rustc-rayon/src/lib.rs
index b59f3fa2b..86f997b36 100644
--- a/vendor/rustc-rayon/src/lib.rs
+++ b/vendor/rustc-rayon/src/lib.rs
@@ -1,4 +1,3 @@
-#![doc(html_root_url = "https://docs.rs/rayon/1.5")]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(unreachable_pub)]
@@ -77,6 +76,11 @@
//! [the `collections` from `std`]: https://doc.rust-lang.org/std/collections/index.html
//! [`std`]: https://doc.rust-lang.org/std/
//!
+//! # Targets without threading
+//!
+//! Rayon has limited support for targets without `std` threading implementations.
+//! See the [`rayon_core`] documentation for more information about its global fallback.
+//!
//! # Other questions?
//!
//! See [the Rayon FAQ][faq].
@@ -114,11 +118,13 @@ pub use rayon_core::ThreadBuilder;
pub use rayon_core::ThreadPool;
pub use rayon_core::ThreadPoolBuildError;
pub use rayon_core::ThreadPoolBuilder;
+pub use rayon_core::{broadcast, spawn_broadcast, BroadcastContext};
pub use rayon_core::{current_num_threads, current_thread_index, max_num_threads};
pub use rayon_core::{in_place_scope, scope, Scope};
pub use rayon_core::{in_place_scope_fifo, scope_fifo, ScopeFifo};
pub use rayon_core::{join, join_context};
pub use rayon_core::{spawn, spawn_fifo};
+pub use rayon_core::{yield_local, yield_now, Yield};
/// We need to transmit raw pointers across threads. It is possible to do this
/// without any unsafe code by converting pointers to usize or to AtomicPtr<T>
@@ -136,6 +142,13 @@ unsafe impl<T: Send> Send for SendPtr<T> {}
// SAFETY: !Sync for raw pointers is not for safety, just as a lint
unsafe impl<T: Send> Sync for SendPtr<T> {}
+impl<T> SendPtr<T> {
+ // Helper to avoid disjoint captures of `send_ptr.0`
+ fn get(self) -> *mut T {
+ self.0
+ }
+}
+
// Implement Clone without the T: Clone bound from the derive
impl<T> Clone for SendPtr<T> {
fn clone(&self) -> Self {
diff --git a/vendor/rustc-rayon/src/math.rs b/vendor/rustc-rayon/src/math.rs
index 9de588965..3a630be14 100644
--- a/vendor/rustc-rayon/src/math.rs
+++ b/vendor/rustc-rayon/src/math.rs
@@ -1,7 +1,7 @@
use std::ops::{Bound, Range, RangeBounds};
/// Divide `n` by `divisor`, and round up to the nearest integer
-/// if not evenly divisable.
+/// if not evenly divisible.
#[inline]
pub(super) fn div_round_up(n: usize, divisor: usize) -> usize {
debug_assert!(divisor != 0, "Division by zero!");
diff --git a/vendor/rustc-rayon/src/slice/chunks.rs b/vendor/rustc-rayon/src/slice/chunks.rs
index 5b145bdd8..a3cc70953 100644
--- a/vendor/rustc-rayon/src/slice/chunks.rs
+++ b/vendor/rustc-rayon/src/slice/chunks.rs
@@ -317,7 +317,7 @@ impl<'data, T: Send> ChunksExactMut<'data, T> {
/// returned by the iterator. The returned slice has at most `chunk_size-1`
/// elements. Subsequent calls will return an empty slice.
pub fn take_remainder(&mut self) -> &'data mut [T] {
- std::mem::replace(&mut self.rem, &mut [])
+ std::mem::take(&mut self.rem)
}
}
diff --git a/vendor/rustc-rayon/src/slice/mergesort.rs b/vendor/rustc-rayon/src/slice/mergesort.rs
index a24ba65b7..fec309d27 100644
--- a/vendor/rustc-rayon/src/slice/mergesort.rs
+++ b/vendor/rustc-rayon/src/slice/mergesort.rs
@@ -205,9 +205,8 @@ where
impl<T> Drop for MergeHole<T> {
fn drop(&mut self) {
// `T` is not a zero-sized type, so it's okay to divide by its size.
- let len = (self.end as usize - self.start as usize) / size_of::<T>();
unsafe {
- // TODO 1.47: let len = self.end.offset_from(self.start) as usize;
+ let len = self.end.offset_from(self.start) as usize;
ptr::copy_nonoverlapping(self.start, self.dest, len);
}
}
@@ -491,8 +490,8 @@ where
let dest_l = SendPtr(dest);
let dest_r = SendPtr(dest.add(left_l.len() + right_l.len()));
rayon_core::join(
- || par_merge(left_l, right_l, dest_l.0, is_less),
- || par_merge(left_r, right_r, dest_r.0, is_less),
+ move || par_merge(left_l, right_l, dest_l.get(), is_less),
+ move || par_merge(left_r, right_r, dest_r.get(), is_less),
);
}
// Finally, `s` gets dropped if we used sequential merge, thus copying the remaining elements
@@ -570,7 +569,7 @@ unsafe fn recurse<T, F>(
// After recursive calls finish we'll have to merge chunks `(start, mid)` and `(mid, end)` from
// `src` into `dest`. If the current invocation has to store the result into `buf`, we'll
- // merge chunks from `v` into `buf`, and viceversa.
+ // merge chunks from `v` into `buf`, and vice versa.
//
// Recursive calls flip `into_buf` at each level of recursion. More concretely, `par_merge`
// merges chunks from `buf` into `v` at the first level, from `v` into `buf` at the second
@@ -593,8 +592,8 @@ unsafe fn recurse<T, F>(
let v = SendPtr(v);
let buf = SendPtr(buf);
rayon_core::join(
- || recurse(v.0, buf.0, left, !into_buf, is_less),
- || recurse(v.0, buf.0, right, !into_buf, is_less),
+ move || recurse(v.get(), buf.get(), left, !into_buf, is_less),
+ move || recurse(v.get(), buf.get(), right, !into_buf, is_less),
);
// Everything went all right - recursive calls didn't panic.
@@ -661,16 +660,17 @@ where
// Wrap pointer in SendPtr so that it can be sent to another thread
// See the documentation of SendPtr for a full explanation
let buf = SendPtr(buf);
+ let is_less = &is_less;
v.par_chunks_mut(CHUNK_LENGTH)
.with_max_len(1)
.enumerate()
- .map(|(i, chunk)| {
+ .map(move |(i, chunk)| {
let l = CHUNK_LENGTH * i;
let r = l + chunk.len();
unsafe {
- let buf = buf.0.add(l);
- (l, r, mergesort(chunk, buf, &is_less))
+ let buf = buf.get().add(l);
+ (l, r, mergesort(chunk, buf, is_less))
}
})
.collect::<Vec<_>>()
@@ -731,7 +731,7 @@ mod tests {
check(&[1, 2, 2, 2, 2, 3], &[]);
check(&[], &[1, 2, 2, 2, 2, 3]);
- let ref mut rng = thread_rng();
+ let rng = &mut thread_rng();
for _ in 0..100 {
let limit: u32 = rng.gen_range(1..21);
diff --git a/vendor/rustc-rayon/src/slice/mod.rs b/vendor/rustc-rayon/src/slice/mod.rs
index a4e777374..dab56deb3 100644
--- a/vendor/rustc-rayon/src/slice/mod.rs
+++ b/vendor/rustc-rayon/src/slice/mod.rs
@@ -85,6 +85,7 @@ pub trait ParallelSlice<T: Sync> {
/// let chunks: Vec<_> = [1, 2, 3, 4, 5].par_chunks(2).collect();
/// assert_eq!(chunks, vec![&[1, 2][..], &[3, 4], &[5]]);
/// ```
+ #[track_caller]
fn par_chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
Chunks::new(chunk_size, self.as_parallel_slice())
@@ -104,6 +105,7 @@ pub trait ParallelSlice<T: Sync> {
/// let chunks: Vec<_> = [1, 2, 3, 4, 5].par_chunks_exact(2).collect();
/// assert_eq!(chunks, vec![&[1, 2][..], &[3, 4]]);
/// ```
+ #[track_caller]
fn par_chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
ChunksExact::new(chunk_size, self.as_parallel_slice())
@@ -123,6 +125,7 @@ pub trait ParallelSlice<T: Sync> {
/// let chunks: Vec<_> = [1, 2, 3, 4, 5].par_rchunks(2).collect();
/// assert_eq!(chunks, vec![&[4, 5][..], &[2, 3], &[1]]);
/// ```
+ #[track_caller]
fn par_rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
RChunks::new(chunk_size, self.as_parallel_slice())
@@ -142,6 +145,7 @@ pub trait ParallelSlice<T: Sync> {
/// let chunks: Vec<_> = [1, 2, 3, 4, 5].par_rchunks_exact(2).collect();
/// assert_eq!(chunks, vec![&[4, 5][..], &[2, 3]]);
/// ```
+ #[track_caller]
fn par_rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
RChunksExact::new(chunk_size, self.as_parallel_slice())
@@ -199,6 +203,7 @@ pub trait ParallelSliceMut<T: Send> {
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [2, 1, 4, 3, 5]);
/// ```
+ #[track_caller]
fn par_chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
ChunksMut::new(chunk_size, self.as_parallel_slice_mut())
@@ -220,6 +225,7 @@ pub trait ParallelSliceMut<T: Send> {
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [3, 2, 1, 4, 5]);
/// ```
+ #[track_caller]
fn par_chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
ChunksExactMut::new(chunk_size, self.as_parallel_slice_mut())
@@ -241,6 +247,7 @@ pub trait ParallelSliceMut<T: Send> {
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [1, 3, 2, 5, 4]);
/// ```
+ #[track_caller]
fn par_rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
RChunksMut::new(chunk_size, self.as_parallel_slice_mut())
@@ -262,6 +269,7 @@ pub trait ParallelSliceMut<T: Send> {
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [1, 2, 5, 4, 3]);
/// ```
+ #[track_caller]
fn par_rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
RChunksExactMut::new(chunk_size, self.as_parallel_slice_mut())
diff --git a/vendor/rustc-rayon/src/slice/quicksort.rs b/vendor/rustc-rayon/src/slice/quicksort.rs
index 87fb724c3..2bfc35032 100644
--- a/vendor/rustc-rayon/src/slice/quicksort.rs
+++ b/vendor/rustc-rayon/src/slice/quicksort.rs
@@ -5,16 +5,34 @@
//! `rayon_core::join`.
use std::cmp;
+use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::ptr;
/// When dropped, copies from `src` into `dest`.
-struct CopyOnDrop<T> {
+#[must_use]
+struct CopyOnDrop<'a, T> {
src: *const T,
dest: *mut T,
+ /// `src` is often a local pointer here, make sure we have appropriate
+ /// PhantomData so that dropck can protect us.
+ marker: PhantomData<&'a mut T>,
}
-impl<T> Drop for CopyOnDrop<T> {
+impl<'a, T> CopyOnDrop<'a, T> {
+ /// Construct from a source pointer and a destination
+ /// Assumes dest lives longer than src, since there is no easy way to
+ /// copy down lifetime information from another pointer
+ unsafe fn new(src: &'a T, dest: *mut T) -> Self {
+ CopyOnDrop {
+ src,
+ dest,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<T> Drop for CopyOnDrop<'_, T> {
fn drop(&mut self) {
// SAFETY: This is a helper class.
// Please refer to its usage for correctness.
@@ -54,10 +72,7 @@ where
// into the slice.
let tmp = mem::ManuallyDrop::new(ptr::read(v.get_unchecked(0)));
let v = v.as_mut_ptr();
- let mut hole = CopyOnDrop {
- src: &*tmp,
- dest: v.add(1),
- };
+ let mut hole = CopyOnDrop::new(&*tmp, v.add(1));
ptr::copy_nonoverlapping(v.add(1), v.add(0), 1);
for i in 2..len {
@@ -103,10 +118,7 @@ where
// into the slice.
let tmp = mem::ManuallyDrop::new(ptr::read(v.get_unchecked(len - 1)));
let v = v.as_mut_ptr();
- let mut hole = CopyOnDrop {
- src: &*tmp,
- dest: v.add(len - 2),
- };
+ let mut hole = CopyOnDrop::new(&*tmp, v.add(len - 2));
ptr::copy_nonoverlapping(v.add(len - 2), v.add(len - 1), 1);
for i in (0..len - 2).rev() {
@@ -191,25 +203,25 @@ where
// This binary heap respects the invariant `parent >= child`.
let sift_down = |v: &mut [T], mut node| {
loop {
- // Children of `node`:
- let left = 2 * node + 1;
- let right = 2 * node + 2;
+ // Children of `node`.
+ let mut child = 2 * node + 1;
+ if child >= v.len() {
+ break;
+ }
// Choose the greater child.
- let greater = if right < v.len() && is_less(&v[left], &v[right]) {
- right
- } else {
- left
- };
+ if child + 1 < v.len() && is_less(&v[child], &v[child + 1]) {
+ child += 1;
+ }
// Stop if the invariant holds at `node`.
- if greater >= v.len() || !is_less(&v[node], &v[greater]) {
+ if !is_less(&v[node], &v[child]) {
break;
}
// Swap `node` with the greater child, move one step down, and continue sifting.
- v.swap(node, greater);
- node = greater;
+ v.swap(node, child);
+ node = child;
}
};
@@ -426,7 +438,7 @@ where
// safe. Otherwise, the debug assertions in the `is_done` case guarantee that
// `width(l, r) == block_l + block_r`, namely, that the block sizes have been adjusted to account
// for the smaller number of remaining elements.
- l = unsafe { l.offset(block_l as isize) };
+ l = unsafe { l.add(block_l) };
}
if start_r == end_r {
@@ -510,10 +522,7 @@ where
// SAFETY: `pivot` is a reference to the first element of `v`, so `ptr::read` is safe.
let tmp = mem::ManuallyDrop::new(unsafe { ptr::read(pivot) });
- let _pivot_guard = CopyOnDrop {
- src: &*tmp,
- dest: pivot,
- };
+ let _pivot_guard = unsafe { CopyOnDrop::new(&*tmp, pivot) };
let pivot = &*tmp;
// Find the first pair of out-of-order elements.
@@ -569,10 +578,7 @@ where
// operation panics, the pivot will be automatically written back into the slice.
// SAFETY: The pointer here is valid because it is obtained from a reference to a slice.
let tmp = mem::ManuallyDrop::new(unsafe { ptr::read(pivot) });
- let _pivot_guard = CopyOnDrop {
- src: &*tmp,
- dest: pivot,
- };
+ let _pivot_guard = unsafe { CopyOnDrop::new(&*tmp, pivot) };
let pivot = &*tmp;
// Now partition the slice.
@@ -629,8 +635,7 @@ fn break_patterns<T>(v: &mut [T]) {
random
};
let mut gen_usize = || {
- // TODO 1.53: if usize::BITS <= 32 {
- if mem::size_of::<usize>() <= 4 {
+ if usize::BITS <= 32 {
gen_u32() as usize
} else {
(((gen_u32() as u64) << 32) | (gen_u32() as u64)) as usize
@@ -676,6 +681,7 @@ where
let len = v.len();
// Three indices near which we are going to choose a pivot.
+ #[allow(clippy::identity_op)]
let mut a = len / 4 * 1;
let mut b = len / 4 * 2;
let mut c = len / 4 * 3;
@@ -850,8 +856,7 @@ where
}
// Limit the number of imbalanced partitions to `floor(log2(len)) + 1`.
- // TODO 1.53: let limit = usize::BITS - v.len().leading_zeros();
- let limit = mem::size_of::<usize>() as u32 * 8 - v.len().leading_zeros();
+ let limit = usize::BITS - v.len().leading_zeros();
recurse(v, &is_less, None, limit);
}
@@ -864,7 +869,7 @@ mod tests {
#[test]
fn test_heapsort() {
- let ref mut rng = thread_rng();
+ let rng = &mut thread_rng();
for len in (0..25).chain(500..501) {
for &modulus in &[5, 10, 100] {
@@ -891,8 +896,8 @@ mod tests {
heapsort(&mut v, &|_, _| thread_rng().gen());
heapsort(&mut v, &|a, b| a < b);
- for i in 0..v.len() {
- assert_eq!(v[i], i);
+ for (i, &entry) in v.iter().enumerate() {
+ assert_eq!(entry, i);
}
}
}
diff --git a/vendor/rustc-rayon/src/slice/rchunks.rs b/vendor/rustc-rayon/src/slice/rchunks.rs
index 4ff712548..63e765d77 100644
--- a/vendor/rustc-rayon/src/slice/rchunks.rs
+++ b/vendor/rustc-rayon/src/slice/rchunks.rs
@@ -314,7 +314,7 @@ impl<'data, T: Send> RChunksExactMut<'data, T> {
/// returned by the iterator. The returned slice has at most `chunk_size-1`
/// elements. Subsequent calls will return an empty slice.
pub fn take_remainder(&mut self) -> &'data mut [T] {
- std::mem::replace(&mut self.rem, &mut [])
+ std::mem::take(&mut self.rem)
}
}
diff --git a/vendor/rustc-rayon/src/slice/test.rs b/vendor/rustc-rayon/src/slice/test.rs
index 4325aa88d..f74ca0f74 100644
--- a/vendor/rustc-rayon/src/slice/test.rs
+++ b/vendor/rustc-rayon/src/slice/test.rs
@@ -10,7 +10,7 @@ macro_rules! sort {
($f:ident, $name:ident) => {
#[test]
fn $name() {
- let ref mut rng = thread_rng();
+ let rng = &mut thread_rng();
for len in (0..25).chain(500..501) {
for &modulus in &[5, 10, 100] {
diff --git a/vendor/rustc-rayon/src/str.rs b/vendor/rustc-rayon/src/str.rs
index 7afda3063..c85754e3d 100644
--- a/vendor/rustc-rayon/src/str.rs
+++ b/vendor/rustc-rayon/src/str.rs
@@ -689,11 +689,7 @@ pub struct Lines<'ch>(&'ch str);
#[inline]
fn no_carriage_return(line: &str) -> &str {
- if line.ends_with('\r') {
- &line[..line.len() - 1]
- } else {
- line
- }
+ line.strip_suffix('\r').unwrap_or(line)
}
impl<'ch> ParallelIterator for Lines<'ch> {
diff --git a/vendor/rustc-rayon/src/vec.rs b/vendor/rustc-rayon/src/vec.rs
index e07963922..7892f53fa 100644
--- a/vendor/rustc-rayon/src/vec.rs
+++ b/vendor/rustc-rayon/src/vec.rs
@@ -132,7 +132,7 @@ impl<'data, T: Send> IndexedParallelIterator for Drain<'data, T> {
self.range.len()
}
- fn with_producer<CB>(mut self, callback: CB) -> CB::Output
+ fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<Self::Item>,
{
@@ -141,7 +141,7 @@ impl<'data, T: Send> IndexedParallelIterator for Drain<'data, T> {
self.vec.set_len(self.range.start);
// Create the producer as the exclusive "owner" of the slice.
- let producer = DrainProducer::from_vec(&mut self.vec, self.range.len());
+ let producer = DrainProducer::from_vec(self.vec, self.range.len());
// The producer will move or drop each item from the drained range.
callback.callback(producer)
@@ -151,22 +151,24 @@ impl<'data, T: Send> IndexedParallelIterator for Drain<'data, T> {
impl<'data, T: Send> Drop for Drain<'data, T> {
fn drop(&mut self) {
- if self.range.len() > 0 {
- let Range { start, end } = self.range;
- if self.vec.len() != start {
- // We must not have produced, so just call a normal drain to remove the items.
- assert_eq!(self.vec.len(), self.orig_len);
- self.vec.drain(start..end);
- } else if end < self.orig_len {
- // The producer was responsible for consuming the drained items.
- // Move the tail items to their new place, then set the length to include them.
- unsafe {
- let ptr = self.vec.as_mut_ptr().add(start);
- let tail_ptr = self.vec.as_ptr().add(end);
- let tail_len = self.orig_len - end;
- ptr::copy(tail_ptr, ptr, tail_len);
- self.vec.set_len(start + tail_len);
- }
+ let Range { start, end } = self.range;
+ if self.vec.len() == self.orig_len {
+ // We must not have produced, so just call a normal drain to remove the items.
+ self.vec.drain(start..end);
+ } else if start == end {
+ // Empty range, so just restore the length to its original state
+ unsafe {
+ self.vec.set_len(self.orig_len);
+ }
+ } else if end < self.orig_len {
+ // The producer was responsible for consuming the drained items.
+ // Move the tail items to their new place, then set the length to include them.
+ unsafe {
+ let ptr = self.vec.as_mut_ptr().add(start);
+ let tail_ptr = self.vec.as_ptr().add(end);
+ let tail_len = self.orig_len - end;
+ ptr::copy(tail_ptr, ptr, tail_len);
+ self.vec.set_len(start + tail_len);
}
}
}
@@ -181,7 +183,7 @@ pub(crate) struct DrainProducer<'data, T: Send> {
impl<T: Send> DrainProducer<'_, T> {
/// Creates a draining producer, which *moves* items from the slice.
///
- /// Unsafe bacause `!Copy` data must not be read after the borrow is released.
+ /// Unsafe because `!Copy` data must not be read after the borrow is released.
pub(crate) unsafe fn new(slice: &mut [T]) -> DrainProducer<'_, T> {
DrainProducer { slice }
}
@@ -207,7 +209,7 @@ impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
fn into_iter(mut self) -> Self::IntoIter {
// replace the slice so we don't drop it twice
- let slice = mem::replace(&mut self.slice, &mut []);
+ let slice = mem::take(&mut self.slice);
SliceDrain {
iter: slice.iter_mut(),
}
@@ -215,7 +217,7 @@ impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
fn split_at(mut self, index: usize) -> (Self, Self) {
// replace the slice so we don't drop it twice
- let slice = mem::replace(&mut self.slice, &mut []);
+ let slice = mem::take(&mut self.slice);
let (left, right) = slice.split_at_mut(index);
unsafe { (DrainProducer::new(left), DrainProducer::new(right)) }
}
@@ -223,8 +225,9 @@ impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
fn drop(&mut self) {
- // use `Drop for [T]`
- unsafe { ptr::drop_in_place(self.slice) };
+ // extract the slice so we can use `Drop for [T]`
+ let slice_ptr: *mut [T] = mem::take::<&'data mut [T]>(&mut self.slice);
+ unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
}
}
@@ -274,7 +277,7 @@ impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}
impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
fn drop(&mut self) {
// extract the iterator so we can use `Drop for [T]`
- let iter = mem::replace(&mut self.iter, [].iter_mut());
- unsafe { ptr::drop_in_place(iter.into_slice()) };
+ let slice_ptr: *mut [T] = mem::replace(&mut self.iter, [].iter_mut()).into_slice();
+ unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
}
}