+impl<'a, 'b> ops::Sub<&'a BigInt> for &'b BigInt {
+ type Output = BigInt;
+ fn sub(self, rhs: &'a BigInt) -> Self::Output {
+ let max_len = cmp::max(self.data.len(), rhs.data.len());
+ let mut result_vec:Vec<u64> = Vec::with_capacity(max_len);
+ let mut carry:bool = false; // the carry bit
+ for i in 0..max_len {
+ // compute next digit and carry
+ let lhs_val = if i < self.data.len() { self.data[i] } else { 0 };
+ let rhs_val = if i < rhs.data.len() { rhs.data[i] } else { 0 };
+ let (sum, new_carry) = overflowing_sub(lhs_val, rhs_val, carry);
+ // store them
+ result_vec.push(sum);
+ carry = new_carry;
+ }
+ if carry {
+ panic!("Wrapping subtraction of BigInt");
+ }
+ // We may have trailing zeroes, so get rid of them
+ BigInt::from_vec(result_vec)
+ }
+}
+
+impl<'a> ops::Sub<BigInt> for &'a BigInt {
+ type Output = BigInt;
+ #[inline]
+ fn sub(self, rhs: BigInt) -> Self::Output {
+ self - &rhs
+ }
+}
+
+impl<'a> ops::Sub<&'a BigInt> for BigInt {
+ type Output = BigInt;
+ #[inline]
+ fn sub(self, rhs: &'a BigInt) -> Self::Output {
+ &self - rhs
+ }
+}
+
+impl ops::Sub<BigInt> for BigInt {
+ type Output = BigInt;
+ #[inline]
+ fn sub(self, rhs: BigInt) -> Self::Output {
+ &self - &rhs
+ }
+}
+