Skip to content

Commit

Permalink
Add interface to rayon's par_bridge adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyBF committed Feb 23, 2024
1 parent c4dd314 commit 16e6927
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ext/crates/maybe-rayon/src/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub mod prelude {
fn maybe_par_iter_mut(&'data mut self) -> Self::Iter;
}

pub type MaybeIterBridge<I> = rayon::iter::IterBridge<I>;

pub trait MaybeParallelBridge: ParallelBridge {
fn maybe_par_bridge(self) -> MaybeIterBridge<Self> {
self.par_bridge()
}
}

// Implementations

impl<I: ParallelIterator> MaybeParallelIterator for I {}
Expand All @@ -33,6 +41,8 @@ pub mod prelude {
self.par_iter_mut()
}
}

impl<I: ParallelBridge> MaybeParallelBridge for I {}
}

pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
Expand Down
18 changes: 18 additions & 0 deletions ext/crates/maybe-rayon/src/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub mod prelude {
fn maybe_par_iter_mut(&'data mut self) -> Self::Iter;
}

pub struct MaybeIterBridge<Iter>(Iter);

pub trait MaybeParallelBridge: Sized {
fn maybe_par_bridge(self) -> MaybeIterBridge<Self> {
MaybeIterBridge(self)
}
}

// Implementations

impl<I: Iterator> MaybeParallelIterator for I {}
Expand All @@ -39,6 +47,16 @@ pub mod prelude {
self.into_iter()
}
}

impl<Iter: Iterator> Iterator for MaybeIterBridge<Iter> {
type Item = Iter::Item;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

impl<T: Iterator> MaybeParallelBridge for T {}
}

#[allow(dead_code)]
Expand Down

0 comments on commit 16e6927

Please sign in to comment.