From 553103125266db6827f6a1f42f8e0e90087107c3 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 5 Dec 2016 23:11:40 -0800 Subject: [PATCH] complex: derive Eq Fixes #245. --- complex/src/lib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/complex/src/lib.rs b/complex/src/lib.rs index 8a3718f..62f74cc 100644 --- a/complex/src/lib.rs +++ b/complex/src/lib.rs @@ -33,7 +33,7 @@ use traits::{Zero, One, Num, Float}; // probably doesn't map to C's _Complex correctly. /// A complex number in Cartesian form. -#[derive(PartialEq, Copy, Clone, Hash, Debug, Default)] +#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default)] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] pub struct Complex { /// Real portion of the complex number @@ -1220,6 +1220,20 @@ mod test { assert!(::hash(&c) != ::hash(&a)); } + #[test] + fn test_hashset() { + use std::collections::HashSet; + let a = Complex::new(0i32, 0i32); + let b = Complex::new(1i32, 0i32); + let c = Complex::new(0i32, 1i32); + + let set: HashSet<_> = [a, b, c].iter().cloned().collect(); + assert!(set.contains(&a)); + assert!(set.contains(&b)); + assert!(set.contains(&c)); + assert!(!set.contains(&(a + b + c))); + } + #[test] fn test_is_nan() { assert!(!_1_1i.is_nan());