summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2016-11-26 12:20:56 +0200
committerJustin Worthe <justin.worthe@gmail.com>2016-11-26 12:20:56 +0200
commit253500a2088718957831dbec5922aa53e2fe113e (patch)
tree274232482883e6cc60bb1076283d2360b1595139
parent732d394f1f456202576e6d2a6326b1e57e61b078 (diff)
Moved complex to its own file
-rw-r--r--src/complex.rs35
-rw-r--r--src/lib.rs34
2 files changed, 36 insertions, 33 deletions
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<T> {
+ real: T,
+ imag: T
+}
+
+impl<T: Clone> Complex<T> {
+ fn new(real: T, imag: T) -> Complex<T> {
+ Complex{real: real, imag: imag}
+ }
+}
+
+impl<T> Add for Complex<T> where T: Add<Output=T> + Copy {
+ type Output = Complex<T>;
+
+ 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<T> {
- real: T,
- imag: T
-}
-
-impl<T: Clone> Complex<T> {
- fn new(real: T, imag: T) -> Complex<T> {
- Complex{real: real, imag: imag}
- }
-}
-
-impl<T> std::ops::Add for Complex<T> where T: std::ops::Add<Output=T> + Copy {
- type Output = Complex<T>;
-
- 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;