replace &Unit with Cow<Unit> in map F generics
This commit is contained in:
parent
bff2b660dc
commit
4ab4d33e0c
8 changed files with 34 additions and 18 deletions
|
@ -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<F: Fn(&Self::Unit) -> Self::Unit + Sync>(mut self, f: F) -> Self {
|
||||
self.par_iter_mut().for_each(|e| *e = f(e));
|
||||
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 format() -> crate::Format {
|
||||
|
|
|
@ -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<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 {
|
||||
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::<Vec<Op>>(),
|
||||
ops: page
|
||||
.ops
|
||||
.into_iter()
|
||||
.map(|op| f(Cow::Owned(op)))
|
||||
.collect::<Vec<Op>>(),
|
||||
..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()))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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())
|
||||
.expect("coudn't get font back from bytes after map")
|
||||
}
|
||||
|
|
|
@ -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<F: Fn(&Self::Unit) -> Self::Unit + Sync>(mut self, f: F) -> Self {
|
||||
self.par_pixels_mut().for_each(|p| *p = f(p));
|
||||
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 format() -> crate::Format {
|
||||
|
|
|
@ -9,6 +9,7 @@ pub type Bytes = Vec<u8>;
|
|||
|
||||
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<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();
|
||||
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<Self, <Self as TryFromDataBytes>::Error> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<T>: IntoParallelRefMutIterator<'a, Item = &'a mut T>,
|
||||
{
|
||||
type Unit = T;
|
||||
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(mut self, f: F) -> Self {
|
||||
self.0.par_iter_mut().for_each(|e| *e = f(e));
|
||||
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 {
|
||||
|
|
|
@ -28,8 +28,11 @@ impl IntoDataBytes for Text<'_> {
|
|||
impl Bendable for Text<'_> {
|
||||
type Unit = char;
|
||||
|
||||
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self {
|
||||
self.chars().map(|c| f(&c)).collect::<String>().into()
|
||||
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
|
||||
self.chars()
|
||||
.map(|c| f(Cow::Owned(c)))
|
||||
.collect::<String>()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn format() -> crate::Format {
|
||||
|
|
|
@ -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<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(
|
||||
Document::new(
|
||||
self.document
|
||||
.get_all_elements()
|
||||
.into_iter()
|
||||
.map(Cow::Borrowed)
|
||||
.map(f)
|
||||
.collect(),
|
||||
),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue