Skip to content

Implement PartialEq and Eq #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,28 @@ impl<K, V, S> Default for OrderMap<K, V, S>
}
}

impl<K, V1, S1, V2, S2> PartialEq<OrderMap<K, V2, S2>> for OrderMap<K, V1, S1>
where K: Hash + Eq,
V1: PartialEq<V2>,
S1: BuildHasher,
S2: BuildHasher
{
fn eq(&self, other: &OrderMap<K, V2, S2>) -> bool {
if self.len() != other.len() {
return false;
}

self.iter().all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
}
}

impl<K, V, S> Eq for OrderMap<K, V, S>
where K: Eq + Hash,
V: Eq,
S: BuildHasher
{
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1642,4 +1664,19 @@ mod tests {
assert_eq!(a, b);
}
}

#[test]
fn partial_eq_and_eq() {
let mut map_a = OrderMap::new();
map_a.insert(1, "1");
map_a.insert(2, "2");
let mut map_b = map_a.clone();
assert_eq!(map_a, map_b);
map_b.remove(&1);
assert_ne!(map_a, map_b);

let map_c: OrderMap<_, String> = map_b.into_iter().map(|(k, v)| (k, v.to_owned())).collect();
assert_ne!(map_a, map_c);
assert_ne!(map_c, map_a);
}
}