Skip to content

Commit

Permalink
maybe-rayon package
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Aug 22, 2024
1 parent ad43b7c commit 6fc91aa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
16 changes: 16 additions & 0 deletions maybe_rayon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "crate_crypto_internal_eth_kzg_maybe_rayon"
description = "This crate provides an implementation of a wrapper around the rayon crate"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
rust-version = { workspace = true }
repository = { workspace = true }

[dependencies]
rayon = { workspace = true, optional = true }

[features]
default = []
multithreading = ["rayon"]
17 changes: 17 additions & 0 deletions maybe_rayon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[cfg(feature = "multithreading")]
mod multi_threaded;
#[cfg(not(feature = "multithreading"))]
mod single_threaded;

#[cfg(feature = "multithreading")]
pub use multi_threaded::*;
#[cfg(not(feature = "multithreading"))]
pub use single_threaded::*;

pub mod prelude {
pub use crate::MaybeParallelRefExt;
pub use crate::MaybeParallelRefMutExt;
pub use crate::*;
#[cfg(feature = "multithreading")]
pub use rayon::prelude::*;
}
29 changes: 29 additions & 0 deletions maybe_rayon/src/multi_threaded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub use rayon::iter::IntoParallelIterator;
pub use rayon::iter::IntoParallelRefIterator;
pub use rayon::iter::IntoParallelRefMutIterator;
pub use rayon::iter::ParallelIterator;

pub trait MaybeParallelExt: IntoParallelIterator {
fn maybe_into_par_iter(self) -> <Self as IntoParallelIterator>::Iter
where
Self: Sized,
{
self.into_par_iter()
}
}

pub trait MaybeParallelRefExt: for<'a> IntoParallelRefIterator<'a> {
fn maybe_par_iter(&self) -> <Self as IntoParallelRefIterator>::Iter {
self.par_iter()
}
}

pub trait MaybeParallelRefMutExt: for<'a> IntoParallelRefMutIterator<'a> {
fn maybe_par_iter_mut(&mut self) -> <Self as IntoParallelRefMutIterator>::Iter {
self.par_iter_mut()
}
}

impl<T: IntoParallelIterator> MaybeParallelExt for T {}
impl<T: for<'a> IntoParallelRefIterator<'a>> MaybeParallelRefExt for T {}
impl<T: for<'a> IntoParallelRefMutIterator<'a>> MaybeParallelRefMutExt for T {}
53 changes: 53 additions & 0 deletions maybe_rayon/src/single_threaded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pub use std::iter::IntoIterator;
pub use std::iter::Iterator;

pub trait MaybeParallelExt: IntoIterator {
fn maybe_into_par_iter(self) -> <Self as IntoIterator>::IntoIter
where
Self: Sized,
{
self.into_iter()
}
}

pub trait MaybeParallelRefExt {
type Item;
type Iter<'a>: Iterator<Item = &'a Self::Item>
where
Self: 'a;
fn maybe_par_iter(&self) -> Self::Iter<'_>;
}

pub trait MaybeParallelRefMutExt {
type Item;
type Iter<'a>: Iterator<Item = &'a mut Self::Item>
where
Self: 'a;
fn maybe_par_iter_mut(&mut self) -> Self::Iter<'_>;
}

impl<T: IntoIterator> MaybeParallelExt for T {}

impl<T: IntoIterator> MaybeParallelRefExt for T
where
for<'a> &'a T: IntoIterator<Item = &'a <T as IntoIterator>::Item>,
{
type Item = <T as IntoIterator>::Item;
type Iter<'a> = <&'a T as IntoIterator>::IntoIter where Self: 'a;

fn maybe_par_iter(&self) -> Self::Iter<'_> {
self.into_iter()
}
}

impl<T: IntoIterator> MaybeParallelRefMutExt for T
where
for<'a> &'a mut T: IntoIterator<Item = &'a mut <T as IntoIterator>::Item>,
{
type Item = <T as IntoIterator>::Item;
type Iter<'a> = <&'a mut T as IntoIterator>::IntoIter where Self: 'a;

fn maybe_par_iter_mut(&mut self) -> Self::Iter<'_> {
self.into_iter()
}
}

0 comments on commit 6fc91aa

Please sign in to comment.