Skip to main content

diff

Function diff 

Source
pub fn diff<C, E>(
    old: C,
    new: C,
) -> MapDelta<E::Key, E::Value, <E::Value as Delta>::Output>
where C: IntoIterator<Item = E> + TryIndex<E::Key, Output = E::Value>, E: MapEntry, E::Value: Delta,
Expand description

Pairs the entries of old and new by key and diffs the values that survived.

A key present on both sides with an equal value produces nothing at all, so the MapDelta is empty when the collections agree.

Each key of old is looked up in new exactly once, through the collection’s own TryIndex implementation, so the cost is that of n lookups: O(n) for a HashMap, O(n log n) for a BTreeMap.

use delta_struct::{map, Delta, ScalarDelta};
use std::collections::BTreeMap;

#[derive(Delta)]
#[delta_struct(delta_leader = "#[derive(Debug, PartialEq)]")]
struct Service {
    port: u16,
    healthy: bool,
}

let services = |port| {
    vec![("web", Service { port, healthy: true })]
        .into_iter()
        .collect::<BTreeMap<&str, Service>>()
};

let delta = map::diff(services(80), services(8080));
assert!(delta.add.is_empty() && delta.remove.is_empty());
assert_eq!(delta.change[0].key, "web");
assert_eq!(delta.change[0].delta.port, ScalarDelta::Changed(8080));
assert_eq!(delta.change[0].delta.healthy, ScalarDelta::Unchanged);