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"
anyhow = "1.0"
font-kit = { version = "0.14.2", features = ["loader-freetype-default"] }
cfg-if = "1.0.0"
[dev-dependencies]
project-root = "0"

View file

@ -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<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)));
self
}
fn map<F: Fn(Cow<Self::Unit>) -> 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 {

View file

@ -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::<Vec<Op>>(),
ops: {
cfg_if! {
if #[cfg(feature = "rayon")] {
page.ops.into_par_iter()
} else {
page.ops.into_iter()
}
}
}
.map(|op| f(Cow::Owned(op)))
.collect::<Vec<Op>>(),
..page
})
.collect(),

View file

@ -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<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)));
self
}
fn map<F: Fn(Cow<Self::Unit>) -> 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 {

View file

@ -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<T> Bendable for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero + Send,
<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 {
self.0.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e)));
self
cfg_if! {
if #[cfg(feature = "rayon")] {
impl<T> Bendable for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero + Send,
<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 {
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<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
}
}
}}