add remaining rayon, better feature switches

This commit is contained in:
Breval Ferrari 2025-04-19 15:00:32 -04:00
parent d2ede29165
commit 84a3b182ab
Signed by: breval
GPG key ID: A2EEBF62257FF960
5 changed files with 65 additions and 46 deletions

View file

@ -36,6 +36,7 @@ printpdf = { version = "0.8.2", features = [
shiva = "1.4.9" shiva = "1.4.9"
anyhow = "1.0" anyhow = "1.0"
font-kit = { version = "0.14.2", features = ["loader-freetype-default"] } font-kit = { version = "0.14.2", features = ["loader-freetype-default"] }
cfg-if = "1.0.0"
[dev-dependencies] [dev-dependencies]
project-root = "0" project-root = "0"

View file

@ -1,5 +1,6 @@
use std::{borrow::Cow, convert::Infallible}; use std::{borrow::Cow, convert::Infallible};
use cfg_if::cfg_if;
#[cfg(feature = "rayon")] #[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
@ -28,13 +29,15 @@ impl TryFromDataBytes for Bytes {
impl Bendable for Bytes { impl Bendable for Bytes {
type Unit = u8; type Unit = u8;
#[cfg(feature = "rayon")]
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self {
self.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); cfg_if! {
self if #[cfg(feature = "rayon")] {
} let iter = self.par_iter_mut();
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self { } else {
self.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); let iter = self.iter_mut();
}
}
iter.for_each(|e| *e = f(Cow::Borrowed(e)));
self self
} }
fn format() -> crate::Format { fn format() -> crate::Format {

View file

@ -1,8 +1,11 @@
use std::borrow::Cow; use std::borrow::Cow;
use cfg_if::cfg_if;
use printpdf::{ use printpdf::{
Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg, Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg,
}; };
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
@ -73,11 +76,17 @@ impl Bendable for PdfDocument {
pages: pages pages: pages
.into_iter() .into_iter()
.map(|page| PdfPage { .map(|page| PdfPage {
ops: page ops: {
.ops cfg_if! {
.into_iter() if #[cfg(feature = "rayon")] {
.map(|op| f(Cow::Owned(op))) page.ops.into_par_iter()
.collect::<Vec<Op>>(), } else {
page.ops.into_iter()
}
}
}
.map(|op| f(Cow::Owned(op)))
.collect::<Vec<Op>>(),
..page ..page
}) })
.collect(), .collect(),

View file

@ -5,6 +5,7 @@ use std::{
ops::{Add, Deref, DerefMut, Div, Mul, Sub}, ops::{Add, Deref, DerefMut, Div, Mul, Sub},
}; };
use cfg_if::cfg_if;
pub use image::*; pub use image::*;
use num::{ use num::{
traits::{FromBytes, ToBytes}, traits::{FromBytes, ToBytes},
@ -179,13 +180,15 @@ where
P: Send + Sync, P: Send + Sync,
{ {
type Unit = P; type Unit = P;
#[cfg(feature = "rayon")]
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self {
self.par_pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p))); cfg_if! {
self if #[cfg(feature = "rayon")] {
} let iter = self.par_pixels_mut();
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self { } else {
self.pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p))); let iter = self.pixels_mut();
}
}
iter.for_each(|p| *p = f(Cow::Borrowed(p)));
self self
} }
fn format() -> crate::Format { fn format() -> crate::Format {

View file

@ -1,6 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use super::Sample; use super::Sample;
use cfg_if::cfg_if;
use derive_wrapper::{AsRef, From}; use derive_wrapper::{AsRef, From};
use num::{ use num::{
traits::{FromBytes, ToBytes}, traits::{FromBytes, ToBytes},
@ -67,34 +68,36 @@ where
} }
} }
#[cfg(feature = "rayon")] cfg_if! {
impl<T> Bendable for RawSamples<T> if #[cfg(feature = "rayon")] {
where impl<T> Bendable for RawSamples<T>
T: Sample + FromBytes + ToBytes + Zero + Send, where
<T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>, T: Sample + FromBytes + ToBytes + Zero + Send,
for<'a> Vec<T>: IntoParallelRefMutIterator<'a, Item = &'a mut T>, <T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>,
{ for<'a> Vec<T>: IntoParallelRefMutIterator<'a, Item = &'a mut T>,
type Unit = T; {
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self { type Unit = T;
self.0.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(mut self, f: F) -> Self {
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 { } else {
crate::Format::Sound impl<T> Bendable for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero + Send,
<T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>,
{
type Unit = T;
fn map<F: Fn(Cow<Self::Unit>) -> 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<T> Bendable for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero + Send,
<T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>,
{
type Unit = T;
fn map<F: Fn(Cow<Self::Unit>) -> 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
}
}