mitsuhiko / similar

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

Support custom comparison with capture_diff_slices_by #27

Open WiSaGaN opened 3 years ago

WiSaGaN commented 3 years ago

I should first say, thank you for the crate, this is very useful for even comparing binary data.

Similar to capture_diff_slices but user can supply a F: FnMut(&T, &T) -> Ordering. This is more of a wishlist since I don't have a concrete design in mind. Please feel free to close if this is not possible or not on the roadmap.

mitsuhiko commented 3 years ago

Do you have a specific usecase in mind for this?

WiSaGaN commented 3 years ago

I am currently using this to align two data series from different sources while they both describes the same underlying event stream, e1....en, which we don't have other ways to observe:

So I now use capture_diff_slices with success to form a combined and sorted data series. However, this is based on the assumption that the data points from different series are equal if they describe the same event. This is not the case for some kind of data. Data points from different data sources may have fields that have different values due to observation defects, which we know beforehand, and thus would like to ignore when we do the capture_diff_slices. In rust, I want to have a way to supply the compare function so that I can change it dynamically instead of implementing new Ord/Eq.

Dushistov commented 2 years ago

I need this feature too. I compare two slices with Foo defined like this:

struct Foo {
   uuid: Uuid,
   others: Data,
}

In particular algorithm I compare slices only by uuid field, but in other algorithms I need PartialEq that take others also into account. So I have to create two Vec<Wrapper<'a>>, where Wrapper hold reference to Foo and its PartialEq compare only uuid.