equivalence

This crate provides traits for comparing and hashing values modulo an equivalence relation specified by a context of user-defined type C.
The Equivalence derive macro allows the user to easily implement Equivalence for custom types.
Example
# use equivalence::*;
# use std::cmp::Ordering;
# use std::hash::{Hash, Hasher};
struct ModN(u64);
impl PartialEqWith<ModN> for u64 {
fn eq_with(&self, other: &u64, ctx: &ModN) -> bool {
(*self % ctx.0) == (*other % ctx.0)
}
}
impl EqWith<ModN> for u64 {}
impl OrdWith<ModN> for u64 {
fn cmp_with(&self, other: &u64, ctx: &ModN) -> Ordering {
(*self % ctx.0).cmp(&(*other % ctx.0))
}
}
impl PartialOrdWith<ModN> for u64 {
fn partial_cmp_with(&self, other: &u64, ctx: &ModN) -> Option<Ordering> {
Some(self.cmp_with(other, ctx))
}
}
impl HashWith<ModN> for u64 {
fn hash_with<H: Hasher>(&self, hasher: &mut H, ctx: &ModN) {
(*self % ctx.0).hash(hasher)
}
}
assert!([1, 2, 3].eq_with(&[4, 5, 6], &ModN(3)));
assert!([1, 2, 3].ne_with(&[4, 5, 6], &ModN(2)));
#[derive(Equivalence)]
struct MyPair<T> {
#[equiv(fwd)]
left: T, right: u32 }
assert!(MyPair { left: 5u64, right: 7 }.eq_with(&MyPair { left: 6u64, right: 7 }, &ModN(1)));
assert!(MyPair { left: 5u64, right: 7 }.ne_with(&MyPair { left: 5u64, right: 8 }, &ModN(1)));
#[derive(Equivalence)]
#[equiv(rel = "ModN")]
struct U32Pair {
#[equiv(fwd = "map_rec |x: &u32, _| *x as u64")]
left: u32, #[equiv(fwd = "map |x: &u32, ctx: &ModN| (*x as u64) % ctx.0")]
right: u32, }
assert!(U32Pair { left: 3, right: 5 }.eq_with(&U32Pair { left: 5, right: 7 }, &ModN(2)));
assert!(U32Pair { left: 3, right: 5 }.ne_with(&U32Pair { left: 5, right: 7 }, &ModN(3)));