diff --git a/bingus/src/bin_/bytes.rs b/bingus/src/bin_/bytes.rs index bdcc719..0b77a2c 100644 --- a/bingus/src/bin_/bytes.rs +++ b/bingus/src/bin_/bytes.rs @@ -1,4 +1,4 @@ -use std::convert::Infallible; +use std::{borrow::Cow, convert::Infallible}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; @@ -27,8 +27,8 @@ impl TryFromDataBytes for Bytes { impl Bendable for Bytes { type Unit = u8; - fn map Self::Unit + Sync>(mut self, f: F) -> Self { - self.par_iter_mut().for_each(|e| *e = f(e)); + fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { + self.par_iter_mut().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 81de293..dca00ab 100644 --- a/bingus/src/doc/pdf.rs +++ b/bingus/src/doc/pdf.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use printpdf::{ Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg, }; @@ -60,7 +62,7 @@ impl IntoDataBytes for PdfDocument { impl Bendable for PdfDocument { type Unit = Op; - fn map Self::Unit + Sync>(self, f: F) -> Self { + fn map) -> Self::Unit + Sync>(self, f: F) -> Self { let PdfDocument { metadata, resources, @@ -71,7 +73,11 @@ impl Bendable for PdfDocument { pages: pages .into_iter() .map(|page| PdfPage { - ops: page.ops.into_iter().map(|op| f(&op)).collect::>(), + ops: page + .ops + .into_iter() + .map(|op| f(Cow::Owned(op))) + .collect::>(), ..page }) .collect(), @@ -108,7 +114,7 @@ mod tests { } assert_eq!( format!("{:?}", original), - format!("{:?}", original.clone().map(|u| u.clone())) + format!("{:?}", original.clone().map(|u| u.into_owned())) ) } } diff --git a/bingus/src/fnt/fontkit.rs b/bingus/src/fnt/fontkit.rs index 3da11ac..caff993 100644 --- a/bingus/src/fnt/fontkit.rs +++ b/bingus/src/fnt/fontkit.rs @@ -1,6 +1,6 @@ use font_kit::error::FontLoadingError; pub use font_kit::font::Font; -use std::sync::Arc; +use std::{borrow::Cow, sync::Arc}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; @@ -31,7 +31,7 @@ impl IntoDataBytes for Font { impl Bendable for Font { type Unit = u8; - fn map Self::Unit + Sync>(self, f: F) -> Self { + fn map) -> Self::Unit + Sync>(self, f: F) -> Self { Self::try_from_data_bytes(self.into_data_bytes().map(f), (), Default::default()) .expect("coudn't get font back from bytes after map") } diff --git a/bingus/src/img/image.rs b/bingus/src/img/image.rs index 0f47b2d..c91e359 100644 --- a/bingus/src/img/image.rs +++ b/bingus/src/img/image.rs @@ -1,4 +1,5 @@ use std::{ + borrow::Cow, convert::Infallible, iter::once, ops::{Add, Deref, DerefMut, Div, Mul, Sub}, @@ -177,8 +178,8 @@ where P: Send + Sync, { type Unit = P; - fn map Self::Unit + Sync>(mut self, f: F) -> Self { - self.par_pixels_mut().for_each(|p| *p = f(p)); + fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { + self.par_pixels_mut().for_each(|p| *p = f(Cow::Borrowed(p))); self } fn format() -> crate::Format { diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index fb89b3e..d4765fb 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -9,6 +9,7 @@ pub type Bytes = Vec; mod dynamic { use std::{ + borrow::Cow, fs::File, io::{self, Read, Write}, path::Path, @@ -98,7 +99,7 @@ mod dynamic { type Unit = u8; /// /!\ may panic with io errors /!\ - fn map Self::Unit + Sync>(mut self, f: F) -> Self { + fn map) -> Self::Unit + Sync>(mut self, f: F) -> Self { let mut bytes = Vec::new(); self.read_to_end(&mut bytes).expect("couldn't read 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::*; @@ -184,7 +185,7 @@ pub trait Bendable: TryFromDataBytes + IntoDataBytes { ) -> Result::Error> { Self::try_from_data_bytes(b.into_data_bytes(), format, crop) } - fn map Self::Unit + Sync>(self, f: F) -> Self; + fn map) -> Self::Unit + Sync>(self, f: F) -> Self; fn format() -> Format; } diff --git a/bingus/src/snd/raw.rs b/bingus/src/snd/raw.rs index 6b0bfb0..35ed099 100644 --- a/bingus/src/snd/raw.rs +++ b/bingus/src/snd/raw.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use super::Sample; use derive_wrapper::{AsRef, From}; use num::{ @@ -71,8 +73,8 @@ where 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(e)); + 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 { diff --git a/bingus/src/txt/bare.rs b/bingus/src/txt/bare.rs index 92652a1..9cbb537 100644 --- a/bingus/src/txt/bare.rs +++ b/bingus/src/txt/bare.rs @@ -28,8 +28,11 @@ impl IntoDataBytes for Text<'_> { impl Bendable for Text<'_> { type Unit = char; - fn map Self::Unit + Sync>(self, f: F) -> Self { - self.chars().map(|c| f(&c)).collect::().into() + fn map) -> Self::Unit + Sync>(self, f: F) -> Self { + self.chars() + .map(|c| f(Cow::Owned(c))) + .collect::() + .into() } fn format() -> crate::Format { diff --git a/bingus/src/txt/shiva.rs b/bingus/src/txt/shiva.rs index f6748f9..5b4c7bd 100644 --- a/bingus/src/txt/shiva.rs +++ b/bingus/src/txt/shiva.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use derive_new::new; pub use shiva::core::DocumentType; use shiva::core::{bytes::Bytes, Document, Element}; @@ -47,12 +49,13 @@ impl IntoDataBytes for ShivaDocument { impl Bendable for ShivaDocument { type Unit = Element; - fn map Self::Unit + Sync>(self, f: F) -> Self { + fn map) -> Self::Unit + Sync>(self, f: F) -> Self { ShivaDocument::new( Document::new( self.document .get_all_elements() .into_iter() + .map(Cow::Borrowed) .map(f) .collect(), ),