From 1d6b080132a4cd78d6dc5410534fafcf5a4e3264 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sat, 4 Jun 2022 11:28:36 +0200 Subject: [PATCH] Avoid clones in CSV export --- rslib/src/import_export/text/csv/export.rs | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/rslib/src/import_export/text/csv/export.rs b/rslib/src/import_export/text/csv/export.rs index f11b12e2c..c175049c2 100644 --- a/rslib/src/import_export/text/csv/export.rs +++ b/rslib/src/import_export/text/csv/export.rs @@ -142,12 +142,12 @@ fn rendered_nodes_to_str(nodes: &[RenderedNode]) -> String { .join("") } -fn field_to_record_field(field: &str, with_html: bool) -> String { +fn field_to_record_field(field: &str, with_html: bool) -> Cow { let mut text = strip_redundant_sections(field); if !with_html { text = text.map_cow(|t| html_to_text_line(t, false)); } - text.into() + text } fn strip_redundant_sections(text: &str) -> Cow { @@ -220,37 +220,46 @@ impl NoteContext { .then(|| 1 + self.deck_column().unwrap_or_default() + self.field_columns) } - fn record<'i, 's: 'i, 'n: 'i>(&'s self, note: &'n Note) -> impl Iterator + 'i { + fn record<'c, 's: 'c, 'n: 'c>(&'s self, note: &'n Note) -> impl Iterator> { self.notetype_name(note) .into_iter() .chain(self.deck_name(note).into_iter()) - .chain(self.note_fields(note).into_iter()) - .chain(self.with_tags.then(|| note.tags.join(" ")).into_iter()) + .chain(self.note_fields(note)) + .chain(self.tags(note).into_iter()) } - fn notetype_name(&self, note: &Note) -> Option { + fn notetype_name(&self, note: &Note) -> Option> { self.with_notetype.then(|| { self.notetypes .get(¬e.notetype_id) - .map_or(String::new(), |nt| nt.name.clone()) + .map_or(Cow::from(vec![]), |nt| Cow::from(nt.name.as_bytes())) }) } - fn deck_name(&self, note: &Note) -> Option { + fn deck_name(&self, note: &Note) -> Option> { self.with_deck.then(|| { self.deck_ids .get(¬e.id) .and_then(|did| self.deck_names.get(did)) - .map_or(String::new(), |name| name.clone()) + .map_or(Cow::from(vec![]), |name| Cow::from(name.as_bytes())) }) } - fn note_fields<'n>(&self, note: &'n Note) -> impl Iterator + 'n { + fn tags(&self, note: &Note) -> Option> { + self.with_tags + .then(|| Cow::from(note.tags.join(" ").into_bytes())) + } + + fn note_fields<'n>(&self, note: &'n Note) -> impl Iterator> { let with_html = self.with_html; note.fields() .iter() .map(move |f| field_to_record_field(f, with_html)) - .pad_using(self.field_columns, |_| String::new()) + .pad_using(self.field_columns, |_| Cow::from("")) + .map(|cow| match cow { + Cow::Borrowed(s) => Cow::from(s.as_bytes()), + Cow::Owned(s) => Cow::from(s.into_bytes()), + }) } }