use std::fmt::Debug;
pub trait Merge: Clone + Debug {
fn merge(&mut self, other: &Self);
fn merged(&self, other: &Self) -> Self
where
Self: Sized,
{
let mut copy = self.clone();
copy.merge(other);
copy
}
fn subsumes(&self, other: &Self) -> bool
where
Self: PartialEq,
{
self.merged(other) == *self
}
}
#[cfg(test)]
pub mod laws {
use super::*;
pub fn check_commutative<T: Merge + PartialEq>(a: &T, b: &T) -> bool {
a.merged(b) == b.merged(a)
}
pub fn check_associative<T: Merge + PartialEq>(a: &T, b: &T, c: &T) -> bool {
a.merged(b).merged(c) == a.merged(&b.merged(c))
}
pub fn check_idempotent<T: Merge + PartialEq>(a: &T) -> bool {
a.merged(a) == *a
}
}