rayon optional feature

This commit is contained in:
Breval Ferrari 2025-04-19 12:22:19 -04:00
parent 066d89d250
commit d2ede29165
Signed by: breval
GPG key ID: A2EEBF62257FF960
4 changed files with 36 additions and 4 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "bingus" name = "bingus"
version = "0.3.0" version = "0.3.1"
edition.workspace = true edition.workspace = true
license.workspace = true license.workspace = true
description.workspace = true description.workspace = true
@ -10,9 +10,9 @@ keywords.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
image = { version = "0.25", features = ["rayon"] } image = { version = "0.25" }
num = "0" num = "0"
rayon = "1" rayon = { version = "1", optional = true }
infer = "0" infer = "0"
thiserror = "2" thiserror = "2"
derive-new = "0" derive-new = "0"
@ -28,7 +28,6 @@ printpdf = { version = "0.8.2", features = [
"jpeg", "jpeg",
"png", "png",
"pnm", "pnm",
"rayon",
"tga", "tga",
"tiff", "tiff",
"js-sys", "js-sys",
@ -41,3 +40,7 @@ font-kit = { version = "0.14.2", features = ["loader-freetype-default"] }
[dev-dependencies] [dev-dependencies]
project-root = "0" project-root = "0"
pretty_assertions = "1" pretty_assertions = "1"
[features]
default = []
rayon = ["image/rayon", "printpdf/rayon", "dep:rayon"]

View file

@ -1,5 +1,6 @@
use std::{borrow::Cow, convert::Infallible}; use std::{borrow::Cow, convert::Infallible};
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use crate::{Bendable, Bytes, IntoDataBytes, TryFromDataBytes}; use crate::{Bendable, Bytes, IntoDataBytes, TryFromDataBytes};
@ -27,10 +28,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))); self.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e)));
self 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)));
self
}
fn format() -> crate::Format { fn format() -> crate::Format {
crate::Format::Binary crate::Format::Binary
} }

View file

@ -10,6 +10,7 @@ use num::{
traits::{FromBytes, ToBytes}, traits::{FromBytes, ToBytes},
Zero, Zero,
}; };
#[cfg(feature = "rayon")]
use rayon::iter::ParallelIterator; use rayon::iter::ParallelIterator;
use thiserror::Error; use thiserror::Error;
@ -178,10 +179,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))); self.par_pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p)));
self 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)));
self
}
fn format() -> crate::Format { fn format() -> crate::Format {
Format::Image Format::Image
} }

View file

@ -6,6 +6,7 @@ use num::{
traits::{FromBytes, ToBytes}, traits::{FromBytes, ToBytes},
Zero, Zero,
}; };
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
@ -66,6 +67,7 @@ where
} }
} }
#[cfg(feature = "rayon")]
impl<T> Bendable for RawSamples<T> impl<T> Bendable for RawSamples<T>
where where
T: Sample + FromBytes + ToBytes + Zero + Send, T: Sample + FromBytes + ToBytes + Zero + Send,
@ -81,3 +83,18 @@ where
crate::Format::Sound 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
}
}