Remove ExactSizeIterator requirement on Transaction::change

Size hint is enough.
pull/469/head
Blaž Hrastnik 3 years ago
parent cd65a48635
commit bf43fabf65

@ -1,6 +1,4 @@
use ropey::Rope;
use crate::{Change, Transaction};
use crate::{Rope, Transaction};
/// Compares `old` and `new` to generate a [`Transaction`] describing
/// the steps required to get from `old` to `new`.
@ -25,8 +23,9 @@ pub fn compare_ropes(old: &Rope, new: &Rope) -> Transaction {
// The current position of the change needs to be tracked to
// construct the `Change`s.
let mut pos = 0;
let changes: Vec<Change> = diff
.ops()
Transaction::change(
old,
diff.ops()
.iter()
.map(|op| op.as_tag_tuple())
.filter_map(|(tag, old_range, new_range)| {
@ -50,9 +49,8 @@ pub fn compare_ropes(old: &Rope, new: &Rope) -> Transaction {
similar::DiffTag::Delete => Some((old_pos, pos, None)),
similar::DiffTag::Equal => None,
}
})
.collect();
Transaction::change(old, changes.into_iter())
}),
)
}
#[cfg(test)]

@ -473,11 +473,13 @@ impl Transaction {
/// Generate a transaction from a set of changes.
pub fn change<I>(doc: &Rope, changes: I) -> Self
where
I: IntoIterator<Item = Change> + ExactSizeIterator,
I: IntoIterator<Item = Change> + Iterator,
{
let len = doc.len_chars();
let mut changeset = ChangeSet::with_capacity(2 * changes.len() + 1); // rough estimate
let (lower, upper) = changes.size_hint();
let size = upper.unwrap_or(lower);
let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
// TODO: verify ranges are ordered and not overlapping or change will panic.

Loading…
Cancel
Save