X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/e2374eed1c3ae8d0063138ea011e86bbd42473ab..a115b75de6e7e85f8799a77e2998ab1a24743e06:/src/part08.rs?ds=sidebyside diff --git a/src/part08.rs b/src/part08.rs index 558bd62..db8e56c 100644 --- a/src/part08.rs +++ b/src/part08.rs @@ -1,63 +1,18 @@ +// Rust-101, Part 08: Associated Types, Modules +// ============================================ + use std::cmp; use std::ops; use std::fmt; use part05::BigInt; -impl PartialEq for BigInt { - fn eq(&self, other: &BigInt) -> bool { - debug_assert!(self.test_invariant() && other.test_invariant()); - self.data == other.data - } -} - -fn call_eq() { - let b1 = BigInt::new(13); - let b2 = BigInt::new(37); - println!("b1 == b1: {} ; b1 == b2: {}; b1 != b2: {}", b1 == b1, b1 == b2, b1 != b2); -} - - -impl fmt::Debug for BigInt { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.data.fmt(f) - } -} - - - -impl BigInt { - pub fn inc(&mut self, mut by: u64) { - panic!("Not yet implemented."); - } -} - - -#[test] -fn test_inc() { - let mut b = BigInt::new(1337); - b.inc(1337); - assert!(b == BigInt::new(1337 + 1337)); - - b = BigInt::new(0); - assert_eq!(b, BigInt::from_vec(vec![0])); - b.inc(1 << 63); - assert_eq!(b, BigInt::from_vec(vec![1 << 63])); - b.inc(1 << 63); - assert_eq!(b, BigInt::from_vec(vec![0, 1])); - b.inc(1 << 63); - assert_eq!(b, BigInt::from_vec(vec![1 << 63, 1])); - b.inc(1 << 63); - assert_eq!(b, BigInt::from_vec(vec![0, 2])); -} - - // Add with carry, returning the sum and the carry fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) { let sum = u64::wrapping_add(a, b); - if sum >= a { - panic!("First addition did not overflow. Not implemented."); - } else { - panic!("First addition *did* overflow. Not implemented."); + if sum >= a { // first addition did not overflow + unimplemented!() + } else { // first addition *did* overflow + unimplemented!() } } @@ -74,6 +29,8 @@ impl ops::Add for BigInt { type Output = BigInt; fn add(self, rhs: BigInt) -> Self::Output { let mut result_vec:Vec = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len())); - panic!("Not yet implemented."); + unimplemented!() } } + +// [index](main.html) | [previous](part07.html) | [next](main.html)