replace &Unit with Cow<Unit> in map F generics

This commit is contained in:
Breval Ferrari 2025-04-19 11:32:51 -04:00
parent bff2b660dc
commit 4ab4d33e0c
Signed by: breval
GPG key ID: A2EEBF62257FF960
8 changed files with 34 additions and 18 deletions

View file

@ -1,4 +1,4 @@
use std::convert::Infallible; use std::{borrow::Cow, convert::Infallible};
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
@ -27,8 +27,8 @@ impl TryFromDataBytes for Bytes {
impl Bendable for Bytes { impl Bendable for Bytes {
type Unit = u8; type Unit = u8;
fn map<F: Fn(&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(e)); self.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e)));
self self
} }
fn format() -> crate::Format { fn format() -> crate::Format {

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use printpdf::{ use printpdf::{
Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg, Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg,
}; };
@ -60,7 +62,7 @@ impl IntoDataBytes for PdfDocument {
impl Bendable for PdfDocument { impl Bendable for PdfDocument {
type Unit = Op; type Unit = Op;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
let PdfDocument { let PdfDocument {
metadata, metadata,
resources, resources,
@ -71,7 +73,11 @@ impl Bendable for PdfDocument {
pages: pages pages: pages
.into_iter() .into_iter()
.map(|page| PdfPage { .map(|page| PdfPage {
ops: page.ops.into_iter().map(|op| f(&op)).collect::<Vec<Op>>(), ops: page
.ops
.into_iter()
.map(|op| f(Cow::Owned(op)))
.collect::<Vec<Op>>(),
..page ..page
}) })
.collect(), .collect(),
@ -108,7 +114,7 @@ mod tests {
} }
assert_eq!( assert_eq!(
format!("{:?}", original), format!("{:?}", original),
format!("{:?}", original.clone().map(|u| u.clone())) format!("{:?}", original.clone().map(|u| u.into_owned()))
) )
} }
} }

View file

@ -1,6 +1,6 @@
use font_kit::error::FontLoadingError; use font_kit::error::FontLoadingError;
pub use font_kit::font::Font; pub use font_kit::font::Font;
use std::sync::Arc; use std::{borrow::Cow, sync::Arc};
use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
@ -31,7 +31,7 @@ impl IntoDataBytes for Font {
impl Bendable for Font { impl Bendable for Font {
type Unit = u8; type Unit = u8;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
Self::try_from_data_bytes(self.into_data_bytes().map(f), (), Default::default()) Self::try_from_data_bytes(self.into_data_bytes().map(f), (), Default::default())
.expect("coudn't get font back from bytes after map") .expect("coudn't get font back from bytes after map")
} }

View file

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Cow,
convert::Infallible, convert::Infallible,
iter::once, iter::once,
ops::{Add, Deref, DerefMut, Div, Mul, Sub}, ops::{Add, Deref, DerefMut, Div, Mul, Sub},
@ -177,8 +178,8 @@ where
P: Send + Sync, P: Send + Sync,
{ {
type Unit = P; type Unit = P;
fn map<F: Fn(&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(p)); self.par_pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p)));
self self
} }
fn format() -> crate::Format { fn format() -> crate::Format {

View file

@ -9,6 +9,7 @@ pub type Bytes = Vec<u8>;
mod dynamic { mod dynamic {
use std::{ use std::{
borrow::Cow,
fs::File, fs::File,
io::{self, Read, Write}, io::{self, Read, Write},
path::Path, path::Path,
@ -98,7 +99,7 @@ mod dynamic {
type Unit = u8; type Unit = u8;
/// /!\ may panic with io errors /!\ /// /!\ may panic with io errors /!\
fn map<F: Fn(&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 {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
self.read_to_end(&mut bytes).expect("couldn't read file"); self.read_to_end(&mut bytes).expect("couldn't read file");
self.write_all(&bytes.map(f)).expect("couldn't write file"); self.write_all(&bytes.map(f)).expect("couldn't write file");
@ -164,7 +165,7 @@ mod dynamic {
} }
} }
use std::convert::Infallible; use std::{borrow::Cow, convert::Infallible};
pub use dynamic::*; pub use dynamic::*;
@ -184,7 +185,7 @@ pub trait Bendable: TryFromDataBytes + IntoDataBytes {
) -> Result<Self, <Self as TryFromDataBytes>::Error> { ) -> Result<Self, <Self as TryFromDataBytes>::Error> {
Self::try_from_data_bytes(b.into_data_bytes(), format, crop) Self::try_from_data_bytes(b.into_data_bytes(), format, crop)
} }
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self; fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self;
fn format() -> Format; fn format() -> Format;
} }

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use super::Sample; use super::Sample;
use derive_wrapper::{AsRef, From}; use derive_wrapper::{AsRef, From};
use num::{ use num::{
@ -71,8 +73,8 @@ where
for<'a> Vec<T>: IntoParallelRefMutIterator<'a, Item = &'a mut T>, for<'a> Vec<T>: IntoParallelRefMutIterator<'a, Item = &'a mut T>,
{ {
type Unit = T; type Unit = T;
fn map<F: Fn(&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.0.par_iter_mut().for_each(|e| *e = f(e)); self.0.par_iter_mut().for_each(|e| *e = f(Cow::Borrowed(e)));
self self
} }
fn format() -> crate::dynamic::Format { fn format() -> crate::dynamic::Format {

View file

@ -28,8 +28,11 @@ impl IntoDataBytes for Text<'_> {
impl Bendable for Text<'_> { impl Bendable for Text<'_> {
type Unit = char; type Unit = char;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
self.chars().map(|c| f(&c)).collect::<String>().into() self.chars()
.map(|c| f(Cow::Owned(c)))
.collect::<String>()
.into()
} }
fn format() -> crate::Format { fn format() -> crate::Format {

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use derive_new::new; use derive_new::new;
pub use shiva::core::DocumentType; pub use shiva::core::DocumentType;
use shiva::core::{bytes::Bytes, Document, Element}; use shiva::core::{bytes::Bytes, Document, Element};
@ -47,12 +49,13 @@ impl IntoDataBytes for ShivaDocument {
impl Bendable for ShivaDocument { impl Bendable for ShivaDocument {
type Unit = Element; type Unit = Element;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self { fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
ShivaDocument::new( ShivaDocument::new(
Document::new( Document::new(
self.document self.document
.get_all_elements() .get_all_elements()
.into_iter() .into_iter()
.map(Cow::Borrowed)
.map(f) .map(f)
.collect(), .collect(),
), ),