Skip to content

Commit 4e5d5ba

Browse files
committed
auto merge of #16850 : vks/rust/hash-num, r=alexcrichton
Updates #15294.
2 parents 0e7e107 + 46e6e42 commit 4e5d5ba

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/libnum/complex.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
//! Complex numbers.
1313
1414
use std::fmt;
15-
use std::num::{Zero,One,ToStrRadix};
15+
use std::num::{Zero, One, ToStrRadix};
1616

1717
// FIXME #1284: handle complex NaN & infinity etc. This
1818
// probably doesn't map to C's _Complex correctly.
1919

2020
/// A complex number in Cartesian form.
21-
#[deriving(PartialEq,Clone)]
21+
#[deriving(PartialEq, Clone, Hash)]
2222
pub struct Complex<T> {
2323
/// Real portion of the complex number
2424
pub re: T,
@@ -36,10 +36,8 @@ impl<T: Clone + Num> Complex<T> {
3636
Complex { re: re, im: im }
3737
}
3838

39-
/**
40-
Returns the square of the norm (since `T` doesn't necessarily
41-
have a sqrt function), i.e. `re^2 + im^2`.
42-
*/
39+
/// Returns the square of the norm (since `T` doesn't necessarily
40+
/// have a sqrt function), i.e. `re^2 + im^2`.
4341
#[inline]
4442
pub fn norm_sqr(&self) -> T {
4543
self.re * self.re + self.im * self.im
@@ -193,7 +191,8 @@ mod test {
193191
#![allow(non_uppercase_statics)]
194192

195193
use super::{Complex64, Complex};
196-
use std::num::{Zero,One,Float};
194+
use std::num::{Zero, One, Float};
195+
use std::hash::hash;
197196

198197
pub static _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 };
199198
pub static _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 };
@@ -367,4 +366,14 @@ mod test {
367366
test(-_neg1_1i, "1-1i".to_string());
368367
test(_05_05i, "0.5+0.5i".to_string());
369368
}
369+
370+
#[test]
371+
fn test_hash() {
372+
let a = Complex::new(0i32, 0i32);
373+
let b = Complex::new(1i32, 0i32);
374+
let c = Complex::new(0i32, 1i32);
375+
assert!(hash(&a) != hash(&b));
376+
assert!(hash(&b) != hash(&c));
377+
assert!(hash(&c) != hash(&a));
378+
}
370379
}

src/libnum/rational.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::num::{Zero, One, ToStrRadix, FromStrRadix};
2121
use bigint::{BigInt, BigUint, Sign, Plus, Minus};
2222

2323
/// Represents the ratio between 2 numbers.
24-
#[deriving(Clone)]
24+
#[deriving(Clone, Hash)]
2525
#[allow(missing_doc)]
2626
pub struct Ratio<T> {
2727
numer: T,
@@ -380,6 +380,7 @@ mod test {
380380
use super::{Ratio, Rational, BigRational};
381381
use std::num::{Zero, One, FromStrRadix, FromPrimitive, ToStrRadix};
382382
use std::from_str::FromStr;
383+
use std::hash::hash;
383384
use std::num;
384385

385386
pub static _0 : Rational = Ratio { numer: 0, denom: 1};
@@ -751,4 +752,10 @@ mod test {
751752
assert!(! _neg1_2.is_positive());
752753
assert!(! _1_2.is_negative());
753754
}
755+
756+
#[test]
757+
fn test_hash() {
758+
assert!(hash(&_0) != hash(&_1));
759+
assert!(hash(&_0) != hash(&_3_2));
760+
}
754761
}

0 commit comments

Comments
 (0)