From 046a285a4b6ae54aa8f8a1d4b0e1e8d22478b4f6 Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 30 Apr 2013 20:01:26 +0900 Subject: [PATCH] libstd: impl Clone for BigUint/BigInt and replace `copy` with `.clone()` --- src/libstd/num/bigint.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index fa9d6318cc9..e2a336027fd 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -80,6 +80,7 @@ pub fn to_uint(hi: BigDigit, lo: BigDigit) -> uint { A BigUint-typed value BigUint { data: @[a, b, c] } represents a number (a + b * BigDigit::base + c * BigDigit::base^2). */ +#[deriving(Clone)] pub struct BigUint { priv data: ~[BigDigit] } @@ -680,7 +681,7 @@ pub fn to_uint(&self) -> uint { } /// A Sign is a BigInt's composing element. -#[deriving(Eq)] +#[deriving(Eq, Clone)] pub enum Sign { Minus, Zero, Plus } impl Ord for Sign { @@ -726,6 +727,7 @@ fn neg(&self) -> Sign { } /// A big signed integer type. +#[deriving(Clone)] pub struct BigInt { priv sign: Sign, priv data: BigUint @@ -825,8 +827,8 @@ impl Signed for BigInt { #[inline(always)] fn abs(&self) -> BigInt { match self.sign { - Plus | Zero => copy *self, - Minus => BigInt::from_biguint(Plus, copy self.data) + Plus | Zero => self.clone(), + Minus => BigInt::from_biguint(Plus, self.data.clone()) } } @@ -850,8 +852,8 @@ impl Add for BigInt { #[inline(always)] fn add(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { - (Zero, _) => copy *other, - (_, Zero) => copy *self, + (Zero, _) => other.clone(), + (_, Zero) => self.clone(), (Plus, Plus) => BigInt::from_biguint(Plus, self.data + other.data), (Plus, Minus) => self - (-*other), @@ -866,7 +868,7 @@ impl Sub for BigInt { fn sub(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { (Zero, _) => -other, - (_, Zero) => copy *self, + (_, Zero) => self.clone(), (Plus, Plus) => match self.data.cmp(&other.data) { Less => BigInt::from_biguint(Minus, other.data - self.data), Greater => BigInt::from_biguint(Plus, self.data - other.data), @@ -913,7 +915,7 @@ fn rem(&self, other: &BigInt) -> BigInt { impl Neg for BigInt { #[inline(always)] fn neg(&self) -> BigInt { - BigInt::from_biguint(self.sign.neg(), copy self.data) + BigInt::from_biguint(self.sign.neg(), self.data.clone()) } } @@ -1100,9 +1102,9 @@ fn to_uint(&self) -> uint { #[cfg(test)] mod biguint_tests { + use super::*; use core::num::{IntConvertible, Zero, One, FromStrRadix}; use core::cmp::{Less, Equal, Greater}; - use super::{BigUint, BigDigit}; #[test] fn test_from_slice() { @@ -1390,10 +1392,10 @@ fn test_div_rem() { let c = BigUint::from_slice(cVec); if !a.is_zero() { - assert!(c.div_rem(&a) == (copy b, Zero::zero())); + assert!(c.div_rem(&a) == (b.clone(), Zero::zero())); } if !b.is_zero() { - assert!(c.div_rem(&b) == (copy a, Zero::zero())); + assert!(c.div_rem(&b) == (a.clone(), Zero::zero())); } } @@ -1555,7 +1557,7 @@ fn check(n: uint, s: &str) { #[cfg(test)] mod bigint_tests { - use super::{BigInt, BigUint, BigDigit, Sign, Minus, Zero, Plus}; + use super::*; use core::cmp::{Less, Equal, Greater}; use core::num::{IntConvertible, Zero, One, FromStrRadix}; -- 2.44.0