From 0427acd18cc06adec49783818107d1d5e2afc2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 13 Sep 2020 23:12:14 +0900 Subject: [PATCH] Avoid collect() by accepting iterators into Transaction::change. --- helix-core/src/commands.rs | 2 +- helix-core/src/transaction.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/helix-core/src/commands.rs b/helix-core/src/commands.rs index 3bebd7b46..8dc817da8 100644 --- a/helix-core/src/commands.rs +++ b/helix-core/src/commands.rs @@ -155,7 +155,7 @@ pub fn open_below(state: &mut State, _count: usize) { 0, ); - let transaction = Transaction::change(state, changes.collect()).with_selection(selection); + let transaction = Transaction::change(state, changes).with_selection(selection); transaction.apply(state); // TODO: need to store into history if successful diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 12ed523e4..4ecc575a8 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -353,8 +353,10 @@ impl Transaction { } /// Generate a transaction from a set of changes. - // TODO: take an owned iter instead of Vec - pub fn change(state: &State, changes: Vec) -> Self { + pub fn change(state: &State, changes: I) -> Self + where + I: IntoIterator + ExactSizeIterator, + { let len = state.doc.len_chars(); let mut acc = Vec::with_capacity(2 * changes.len() + 1); @@ -381,7 +383,7 @@ impl Transaction { where F: Fn(&SelectionRange) -> Change, { - Self::change(state, state.selection.ranges.iter().map(f).collect()) + Self::change(state, state.selection.ranges.iter().map(f)) } /// Insert text at each selection head.