From 253500a2088718957831dbec5922aa53e2fe113e Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 26 Nov 2016 12:20:56 +0200 Subject: Moved complex to its own file --- src/complex.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 34 +--------------------------------- 2 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 src/complex.rs diff --git a/src/complex.rs b/src/complex.rs new file mode 100644 index 0000000..0919564 --- /dev/null +++ b/src/complex.rs @@ -0,0 +1,35 @@ +use std::ops::Add; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Complex { + real: T, + imag: T +} + +impl Complex { + fn new(real: T, imag: T) -> Complex { + Complex{real: real, imag: imag} + } +} + +impl Add for Complex where T: Add + Copy { + type Output = Complex; + + fn add(self, other: Self) -> Self { + let real = self.real + other.real; + let imag = self.imag + other.imag; + Complex::new(real, imag) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn addition() { + let a = Complex::new(1, 5); + let b = Complex::new(-3, 2); + assert_eq!(a+b, Complex::new(-2, 7)); + } +} + diff --git a/src/lib.rs b/src/lib.rs index 1f29a24..3987f3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,33 +1 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Complex { - real: T, - imag: T -} - -impl Complex { - fn new(real: T, imag: T) -> Complex { - Complex{real: real, imag: imag} - } -} - -impl std::ops::Add for Complex where T: std::ops::Add + Copy { - type Output = Complex; - - fn add(self, other: Self) -> Self { - let real = self.real + other.real; - let imag = self.imag + other.imag; - Complex::new(real, imag) - } -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn addition() { - let a = Complex::new(1, 5); - let b = Complex::new(-3, 2); - assert_eq!(a+b, Complex::new(-2, 7)); - } -} - +pub mod complex; -- cgit v1.2.3