mitsuhiko / similar

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

internal: remove unneeded boxes #12

Closed tommilligan closed 3 years ago

tommilligan commented 3 years ago

You mentioned here that you had some Boxes you thought were unnecessary - I think you were almost there, I pretty much just shuffled around the lifetimes you already had and got something that worked.

The main hurdle was:

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
   --> src/text/mod.rs:465:54
    |
465 |     pub fn iter_all_changes<'x, 'slf>(&'slf self) -> impl Iterator<Item = Change<'x, T>> + 'slf
    |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: hidden type `FlatMap<std::slice::Iter<'slf, types::DiffOp>, impl Iterator, [closure@src/text/mod.rs:471:36: 471:68]>` captures the lifetime `'new` as defined on the impl at 383:12
   --> src/text/mod.rs:383:12
    |
383 | impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'new, 'bufs, T> {
    |            ^^^^

Which states that:

The only lifetime we use in impl Iterator is 'x. So all we need to do is add an additional bound that 'x: 'new, which then means that 'new is implicitly used in our final type signature.

The rest is just adding lifetime bounds on other functions that call this one until the compiler is happy.

mitsuhiko commented 3 years ago

This is perfect. I have to admit I puzzled about this for way too long. Thanks so much!