#[derive(Debug, Clone)]
pub struct IsEq<T> {
pub lhs: T,
pub rhs: T,
}
impl<T> IsEq<T> {
pub fn new(lhs: T, rhs: T) -> Self {
IsEq { lhs, rhs }
}
pub fn equal_under_law(lhs: T, rhs: T) -> Self {
Self::new(lhs, rhs)
}
}
impl<T: Eq> IsEq<T> {
pub fn holds(self) -> bool {
self.lhs == self.rhs
}
}
impl<T: PartialEq> IsEq<T> {
pub fn holds_partial(self) -> bool {
self.lhs == self.rhs
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_eq_holds() {
assert!(IsEq::new(42, 42).holds());
assert!(!IsEq::new(42, 43).holds());
}
#[test]
fn test_is_eq_strings() {
assert!(IsEq::new(String::from("hello"), String::from("hello")).holds());
assert!(!IsEq::new(String::from("hello"), String::from("world")).holds());
}
#[test]
fn test_equal_under_law() {
let eq = IsEq::equal_under_law(vec![1, 2, 3], vec![1, 2, 3]);
assert!(eq.holds());
}
}