pub fn apply<C, E>(
target: &mut C,
delta: MapDelta<E::Key, E::Value, <E::Value as Delta>::Output>,
) -> Result<(), Mismatch>where
C: IntoIterator<Item = E> + Extend<E> + TryIndexMut<E::Key, Output = E::Value>,
E: MapEntry,
E::Value: Delta,Expand description
Applies a keyed diff to target in place.
Removals and changes are single lookups rather than scans, so this costs
the same as diff does, and a changed value is updated where it sits
rather than taken out and put back. As with an unordered field,
membership is preserved but position is not.
A key in remove or change that target does not have is ignored.
This is the one collection helper that can fail, because it is the one that
recurses into Delta::apply_delta: a map of enums can be handed a change
whose value has since moved to another variant. The error propagates rather
than being swallowed, and leaves the map partly updated.
use delta_struct::{map, Delta};
use std::collections::BTreeMap;
#[derive(Delta)]
struct Service {
port: u16,
}
let services = |port| {
vec![("web", Service { port })]
.into_iter()
.collect::<BTreeMap<&str, Service>>()
};
let delta = map::diff(services(80), services(8080));
let mut target = services(80);
map::apply(&mut target, delta)?;
assert_eq!(target["web"].port, 8080);