Skip to content

Commit

Permalink
Added futile attempt to workaround type issue
Browse files Browse the repository at this point in the history
Using temporaries won't work due to the definition
of the Delegate trait, which allows them to
keep the values around for later.
  • Loading branch information
Byron committed Jan 28, 2017
1 parent a7eec83 commit 01a4b58
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,42 @@ use traitdef::{Value, Delegate};
use std::cmp::Ordering;
use std::collections::BTreeSet;

pub fn diff<'a, V, D, AnyV>(l: &'a V, r: &'a V, d: &mut D)
#[derive(PartialEq)]
enum Values<'a, V1: 'a, V2: 'a> {
V1(&'a V1),
V2(&'a V2),
}

impl<'b, V1, V2> Value for Values<'b, V1, V2>
where V1: Value<Key = V2::Key, Item = V2::Item>,
V2: Value
{
type Key = V1::Key;
type Item = V1::Item;
fn items<'a>(&'a self) -> Option<Box<Iterator<Item = (Self::Key, &'a Self::Item)> + 'a>> {
match *self {
Values::V1(ref v) => v.items(),
Values::V2(ref v) => v.items(),
}
}
}

pub fn diff<'a, V, D, T>(l: &'a V, r: &'a V, d: &'a mut D)
where V: Value,
<V as Value>::Key: Ord,
<V as Value>::Item: Value,
D: Delegate<'a, V>
D: Delegate<'a, Values<'a, V, <V as Value>::Item>>
{
match (l.items(), r.items()) {
// two scalars, equal
(None, None) if l == r => d.unchanged(l),
(None, None) if l == r => {
let v = Values::V1(l);
d.unchanged(&v)
}
// two scalars, different
(None, None) => d.modified(l, r),
(None, None) => d.modified(&Values::V1(l), &Values::V1(r)),
// two objects, equal
(Some(_), Some(_)) if l == r => d.unchanged(l),
(Some(_), Some(_)) if l == r => d.unchanged(&Values::V1(l)),
// two objects, different
(Some(li), Some(ri)) => {
let mut sl: BTreeSet<OrdByKey<_, _>> = BTreeSet::new();
Expand All @@ -25,7 +48,7 @@ pub fn diff<'a, V, D, AnyV>(l: &'a V, r: &'a V, d: &mut D)
let v1 = sl.get(k).expect("union to work");
let v2 = sr.get(k).expect("union to work");
if v1.1 == v2.1 {
d.unchanged(v1.1);
// d.unchanged(v1.1);
}
}
}
Expand Down

0 comments on commit 01a4b58

Please sign in to comment.