mitsuhiko / similar

A high level diffing library for rust based on diffs
https://insta.rs/similar
Apache License 2.0
949 stars 31 forks source link

Restricting Trait Bounds in `ChangesIter` #29

Closed alidn closed 2 years ago

alidn commented 3 years ago

First of all, thank you for this crate.

The iter_changes method in DiffOp requires old and new to be Index<usize, Output = &'x T>, which makes it unusable in many situations; the doc's example won't compile if I change the type of vector from Vec<&str> to Vec<usize>:

use similar::{ChangeTag, Algorithm};
use similar::capture_diff_slices;

let old = vec![1, 2, 3]; // changed from let old = vec!["foo", "bar", "baz"];
let new = vec![1, 2, 4]; // changed from let new = vec!["foo", "bar", "blah"];
let ops = capture_diff_slices(Algorithm::Myers, &old, &new);
let changes: Vec<_> = ops
    .iter()
    .flat_map(|x| x.iter_changes(&old, &new))
    .map(|x| (x.tag(), x.value()))
    .collect();
assert_eq!(changes, vec![
    (ChangeTag::Equal, "foo"),
    (ChangeTag::Equal, "bar"),
    (ChangeTag::Delete, "baz"),
    (ChangeTag::Insert, "blah"),
]);

I know the Rust compiler make this almost impossible, but is there any workaround for this?

mitsuhiko commented 2 years ago

I agree this is not great. I will have a look what can be done here without breaking too much stuff.

mitsuhiko commented 2 years ago

I was able to change this, but had to make a major release due to the type changes.

alidn commented 2 years ago

Thank you so much!