From 84a3b182ab57ec91e3fa41820ac05c8eee5e2a52 Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Sat, 19 Apr 2025 15:00:32 -0400 Subject: [PATCH] add remaining rayon, better feature switches --- bingus/Cargo.toml | 1 + bingus/src/bin_/bytes.rs | 15 ++++++---- bingus/src/doc/pdf.rs | 19 +++++++++---- bingus/src/img/image.rs | 15 ++++++---- bingus/src/snd/raw.rs | 61 +++++++++++++++++++++------------------- 5 files changed, 65 insertions(+), 46 deletions(-) diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index 066fc46..48a6eba 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -36,6 +36,7 @@ printpdf = { version = "0.8.2", features = [ shiva = "1.4.9" anyhow = "1.0" font-kit = { version = "0.14.2", features = ["loader-freetype-default"] } +cfg-if = "1.0.0" [dev-dependencies] project-root = "0" diff --git a/bingus/src/bin_/bytes.rs b/bingus/src/bin_/bytes.rs index 6e1733e..472d0b4 100644 --- a/bingus/src/bin_/bytes.rs +++ b/bingus/src/bin_/bytes.rs @@ -1,5 +1,6 @@ use std::{borrow::Cow, convert::Infallible}; +use cfg_if::cfg_if; #[cfg(feature = "rayon")] use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; @@ -28,13 +29,15 @@ impl TryFromDataBytes for Bytes { impl Bendable for Bytes { type Unit = u8; - #[cfg(feature = "rayon")] fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); - self - } - fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); + cfg_if! { + if #[cfg(feature = "rayon")] { + let iter = self.par_iter_mut(); + } else { + let iter = self.iter_mut(); + } + } + iter.for_each(|e| *e = f(Cow::Borrowed(e))); self } fn format() -> crate::Format { diff --git a/bingus/src/doc/pdf.rs b/bingus/src/doc/pdf.rs index dca00ab..02a39f5 100644 --- a/bingus/src/doc/pdf.rs +++ b/bingus/src/doc/pdf.rs @@ -1,8 +1,11 @@ use std::borrow::Cow; +use cfg_if::cfg_if; use printpdf::{ Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg, }; +#[cfg(feature = "rayon")] +use rayon::iter::{IntoParallelIterator, ParallelIterator}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; @@ -73,11 +76,17 @@ impl Bendable for PdfDocument { pages: pages .into_iter() .map(|page| PdfPage { - ops: page - .ops - .into_iter() - .map(|op| f(Cow::Owned(op))) - .collect::>(), + ops: { + cfg_if! { + if #[cfg(feature = "rayon")] { + page.ops.into_par_iter() + } else { + page.ops.into_iter() + } + } + } + .map(|op| f(Cow::Owned(op))) + .collect::>(), ..page }) .collect(), diff --git a/bingus/src/img/image.rs b/bingus/src/img/image.rs index 6681379..b00a421 100644 --- a/bingus/src/img/image.rs +++ b/bingus/src/img/image.rs @@ -5,6 +5,7 @@ use std::{ ops::{Add, Deref, DerefMut, Div, Mul, Sub}, }; +use cfg_if::cfg_if; pub use image::*; use num::{ traits::{FromBytes, ToBytes}, @@ -179,13 +180,15 @@ where P: Send + Sync, { type Unit = P; - #[cfg(feature = "rayon")] fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.par_pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p))); - self - } - fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p))); + cfg_if! { + if #[cfg(feature = "rayon")] { + let iter = self.par_pixels_mut(); + } else { + let iter = self.pixels_mut(); + } + } + iter.for_each(|p| *p = f(Cow::Borrowed(p))); self } fn format() -> crate::Format { diff --git a/bingus/src/snd/raw.rs b/bingus/src/snd/raw.rs index 944c5c0..0557eb6 100644 --- a/bingus/src/snd/raw.rs +++ b/bingus/src/snd/raw.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use super::Sample; +use cfg_if::cfg_if; use derive_wrapper::{AsRef, From}; use num::{ traits::{FromBytes, ToBytes}, @@ -67,34 +68,36 @@ where } } -#[cfg(feature = "rayon")] -impl Bendable for RawSamples -where - T: Sample + FromBytes + ToBytes + Zero + Send, - ::Bytes: Sized + for<'a> TryFrom<&'a [u8]>, - for<'a> Vec: IntoParallelRefMutIterator<'a, Item = &'a mut T>, -{ - type Unit = T; - fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.0.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); - self +cfg_if! { +if #[cfg(feature = "rayon")] { + impl Bendable for RawSamples + where + T: Sample + FromBytes + ToBytes + Zero + Send, + ::Bytes: Sized + for<'a> TryFrom<&'a [u8]>, + for<'a> Vec: IntoParallelRefMutIterator<'a, Item = &'a mut T>, + { + type Unit = T; + fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { + self.0.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); + self + } + fn format() -> crate::dynamic::Format { + crate::Format::Sound + } } - fn format() -> crate::dynamic::Format { - crate::Format::Sound +} else { + impl Bendable for RawSamples + where + T: Sample + FromBytes + ToBytes + Zero + Send, + ::Bytes: Sized + for<'a> TryFrom<&'a [u8]>, + { + type Unit = T; + fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { + self.0.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); + self + } + fn format() -> crate::dynamic::Format { + crate::Format::Sound + } } -} - -impl Bendable for RawSamples -where - T: Sample + FromBytes + ToBytes + Zero + Send, - ::Bytes: Sized + for<'a> TryFrom<&'a [u8]>, -{ - type Unit = T; - fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { - self.0.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); - self - } - fn format() -> crate::dynamic::Format { - crate::Format::Sound - } -} +}}