diff --git a/src/iter/empty.rs b/src/iter/empty.rs new file mode 100644 index 000000000..f4ca74482 --- /dev/null +++ b/src/iter/empty.rs @@ -0,0 +1,93 @@ +use iter::internal::*; +use iter::*; + +use std; +use std::fmt; +use std::marker::PhantomData; + +/// Creates a parallel iterator that produces nothing. +/// +/// This admits no parallelism on its own, but it could be used for code that +/// deals with generic parallel iterators. +/// +/// # Examples +/// +/// ``` +/// use rayon::prelude::*; +/// use rayon::iter::empty; +/// +/// let pi = (0..1234).into_par_iter() +/// .chain(empty()) +/// .chain(1234..10_000); +/// +/// assert_eq!(pi.count(), 10_000); +/// ``` +pub fn empty() -> Empty { + Empty { marker: PhantomData } +} + +/// Iterator adaptor for [the `empty()` function](fn.empty.html). +pub struct Empty { + marker: PhantomData, +} + +impl Clone for Empty { + fn clone(&self) -> Self { + empty() + } +} + +impl fmt::Debug for Empty { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("Empty") + } +} + +impl ParallelIterator for Empty { + type Item = T; + + fn drive_unindexed(self, consumer: C) -> C::Result + where C: UnindexedConsumer + { + self.drive(consumer) + } + + fn opt_len(&mut self) -> Option { + Some(0) + } +} + +impl IndexedParallelIterator for Empty { + fn drive(self, consumer: C) -> C::Result + where C: Consumer + { + consumer.into_folder().complete() + } + + fn len(&mut self) -> usize { + 0 + } + + fn with_producer(self, callback: CB) -> CB::Output + where CB: ProducerCallback + { + callback.callback(EmptyProducer(PhantomData)) + } +} + +/// Private empty producer +struct EmptyProducer(PhantomData); + +impl Producer for EmptyProducer { + type Item = T; + type IntoIter = std::iter::Empty; + + fn into_iter(self) -> Self::IntoIter { + std::iter::empty() + } + + fn split_at(self, index: usize) -> (Self, Self) { + debug_assert_eq!(index, 0); + (self, EmptyProducer(PhantomData)) + } +} diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 30ed113cb..ba9f4bce4 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -87,6 +87,11 @@ mod repeat; pub use self::repeat::{Repeat, repeat}; pub use self::repeat::{RepeatN, repeatn}; +mod empty; +pub use self::empty::{Empty, empty}; +mod once; +pub use self::once::{Once, once}; + #[cfg(test)] mod test; diff --git a/src/iter/once.rs b/src/iter/once.rs new file mode 100644 index 000000000..87160cc33 --- /dev/null +++ b/src/iter/once.rs @@ -0,0 +1,65 @@ +use iter::internal::*; +use iter::*; + +/// Creates a parallel iterator that produces an element exactly once. +/// +/// This admits no parallelism on its own, but it could be chained to existing +/// parallel iterators to extend their contents, or otherwise used for any code +/// that deals with generic parallel iterators. +/// +/// # Examples +/// +/// ``` +/// use rayon::prelude::*; +/// use rayon::iter::once; +/// +/// let pi = (0..1234).into_par_iter() +/// .chain(once(-1)) +/// .chain(1234..10_000); +/// +/// assert_eq!(pi.clone().count(), 10_001); +/// assert_eq!(pi.clone().filter(|&x| x < 0).count(), 1); +/// assert_eq!(pi.position_any(|x| x < 0), Some(1234)); +/// ``` +pub fn once(item: T) -> Once { + Once { item: item } +} + +/// Iterator adaptor for [the `once()` function](fn.once.html). +#[derive(Clone, Debug)] +pub struct Once { + item: T, +} + +impl ParallelIterator for Once { + type Item = T; + + fn drive_unindexed(self, consumer: C) -> C::Result + where C: UnindexedConsumer + { + self.drive(consumer) + } + + fn opt_len(&mut self) -> Option { + Some(1) + } +} + +impl IndexedParallelIterator for Once { + fn drive(self, consumer: C) -> C::Result + where C: Consumer + { + consumer.into_folder().consume(self.item).complete() + } + + fn len(&mut self) -> usize { + 1 + } + + fn with_producer(self, callback: CB) -> CB::Output + where CB: ProducerCallback + { + // Let `OptionProducer` handle it. + Some(self.item).into_par_iter().with_producer(callback) + } +} diff --git a/src/iter/test.rs b/src/iter/test.rs index a41b72155..d5c596b4d 100644 --- a/src/iter/test.rs +++ b/src/iter/test.rs @@ -1888,3 +1888,33 @@ fn check_repeatn_zip_right() { assert_eq!(item, (4, 4)); } } + +#[test] +fn check_empty() { + // drive_unindexed + let mut v: Vec = empty().filter(|_| unreachable!()).collect(); + assert!(v.is_empty()); + + // drive (indexed) + empty().collect_into(&mut v); + assert!(v.is_empty()); + + // with_producer + let v: Vec<(i32, i32)> = empty().zip(1..10).collect(); + assert!(v.is_empty()); +} + +#[test] +fn check_once() { + // drive_unindexed + let mut v: Vec = once(42).filter(|_| true).collect(); + assert_eq!(v, &[42]); + + // drive (indexed) + once(42).collect_into(&mut v); + assert_eq!(v, &[42]); + + // with_producer + let v: Vec<(i32, i32)> = once(42).zip(1..10).collect(); + assert_eq!(v, &[(42, 1)]); +} diff --git a/src/option.rs b/src/option.rs index a7efd7417..33f2879f4 100644 --- a/src/option.rs +++ b/src/option.rs @@ -29,7 +29,7 @@ impl ParallelIterator for IntoIter { fn drive_unindexed(self, consumer: C) -> C::Result where C: UnindexedConsumer { - bridge(self, consumer) + self.drive(consumer) } fn opt_len(&mut self) -> Option { @@ -41,7 +41,11 @@ impl IndexedParallelIterator for IntoIter { fn drive(self, consumer: C) -> C::Result where C: Consumer { - bridge(self, consumer) + let mut folder = consumer.into_folder(); + if let Some(item) = self.opt { + folder = folder.consume(item); + } + folder.complete() } fn len(&mut self) -> usize { @@ -121,6 +125,7 @@ impl Producer for OptionProducer { } fn split_at(self, index: usize) -> (Self, Self) { + debug_assert!(index <= 1); let none = OptionProducer { opt: None }; if index == 0 { (none, self) diff --git a/tests/clones.rs b/tests/clones.rs index 9acecf5d2..e387ba6f1 100644 --- a/tests/clones.rs +++ b/tests/clones.rs @@ -132,6 +132,16 @@ fn clone_adaptors() { check(v.par_iter().zip_eq(&v)); } +#[test] +fn clone_empty() { + check(rayon::iter::empty::()); +} + +#[test] +fn clone_once() { + check(rayon::iter::once(10)); +} + #[test] fn clone_repeat() { let x: Option = None; diff --git a/tests/debug.rs b/tests/debug.rs index efdd4183c..0a6a215c1 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -141,6 +141,16 @@ fn debug_adaptors() { check(v.par_iter().zip_eq(&v)); } +#[test] +fn debug_empty() { + check(rayon::iter::empty::()); +} + +#[test] +fn debug_once() { + check(rayon::iter::once(10)); +} + #[test] fn debug_repeat() { let x: Option = None; diff --git a/tests/producer_split_at.rs b/tests/producer_split_at.rs index 53be6a023..29d2c913a 100644 --- a/tests/producer_split_at.rs +++ b/tests/producer_split_at.rs @@ -96,6 +96,18 @@ fn check_len(iter: &I, len: usize) { // **** Base Producers **** +#[test] +fn empty() { + let v = vec![42]; + check(&v[..0], || rayon::iter::empty()); +} + +#[test] +fn once() { + let v = vec![42]; + check(&v, || rayon::iter::once(42)); +} + #[test] fn option() { let v = vec![42];