Skip to content

Commit b4bdc3f

Browse files
committed
auto merge of #6127 : gifnksm/rust/impl-clone-for-bigint, r=graydon
implement `Clone` using `deriving(Clone)`.
2 parents d3e7c74 + 046a285 commit b4bdc3f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/libstd/num/bigint.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ A big unsigned integer type.
8080
A BigUint-typed value BigUint { data: @[a, b, c] } represents a number
8181
(a + b * BigDigit::base + c * BigDigit::base^2).
8282
*/
83+
#[deriving(Clone)]
8384
pub struct BigUint {
8485
priv data: ~[BigDigit]
8586
}
@@ -680,7 +681,7 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) {
680681
}
681682

682683
/// A Sign is a BigInt's composing element.
683-
#[deriving(Eq)]
684+
#[deriving(Eq, Clone)]
684685
pub enum Sign { Minus, Zero, Plus }
685686

686687
impl Ord for Sign {
@@ -726,6 +727,7 @@ impl Neg<Sign> for Sign {
726727
}
727728

728729
/// A big signed integer type.
730+
#[deriving(Clone)]
729731
pub struct BigInt {
730732
priv sign: Sign,
731733
priv data: BigUint
@@ -825,8 +827,8 @@ impl Signed for BigInt {
825827
#[inline(always)]
826828
fn abs(&self) -> BigInt {
827829
match self.sign {
828-
Plus | Zero => copy *self,
829-
Minus => BigInt::from_biguint(Plus, copy self.data)
830+
Plus | Zero => self.clone(),
831+
Minus => BigInt::from_biguint(Plus, self.data.clone())
830832
}
831833
}
832834

@@ -850,8 +852,8 @@ impl Add<BigInt, BigInt> for BigInt {
850852
#[inline(always)]
851853
fn add(&self, other: &BigInt) -> BigInt {
852854
match (self.sign, other.sign) {
853-
(Zero, _) => copy *other,
854-
(_, Zero) => copy *self,
855+
(Zero, _) => other.clone(),
856+
(_, Zero) => self.clone(),
855857
(Plus, Plus) => BigInt::from_biguint(Plus,
856858
self.data + other.data),
857859
(Plus, Minus) => self - (-*other),
@@ -866,7 +868,7 @@ impl Sub<BigInt, BigInt> for BigInt {
866868
fn sub(&self, other: &BigInt) -> BigInt {
867869
match (self.sign, other.sign) {
868870
(Zero, _) => -other,
869-
(_, Zero) => copy *self,
871+
(_, Zero) => self.clone(),
870872
(Plus, Plus) => match self.data.cmp(&other.data) {
871873
Less => BigInt::from_biguint(Minus, other.data - self.data),
872874
Greater => BigInt::from_biguint(Plus, self.data - other.data),
@@ -913,7 +915,7 @@ impl Rem<BigInt, BigInt> for BigInt {
913915
impl Neg<BigInt> for BigInt {
914916
#[inline(always)]
915917
fn neg(&self) -> BigInt {
916-
BigInt::from_biguint(self.sign.neg(), copy self.data)
918+
BigInt::from_biguint(self.sign.neg(), self.data.clone())
917919
}
918920
}
919921

@@ -1100,9 +1102,9 @@ pub impl BigInt {
11001102
11011103
#[cfg(test)]
11021104
mod biguint_tests {
1105+
use super::*;
11031106
use core::num::{IntConvertible, Zero, One, FromStrRadix};
11041107
use core::cmp::{Less, Equal, Greater};
1105-
use super::{BigUint, BigDigit};
11061108
11071109
#[test]
11081110
fn test_from_slice() {
@@ -1390,10 +1392,10 @@ mod biguint_tests {
13901392
let c = BigUint::from_slice(cVec);
13911393

13921394
if !a.is_zero() {
1393-
assert!(c.div_rem(&a) == (copy b, Zero::zero()));
1395+
assert!(c.div_rem(&a) == (b.clone(), Zero::zero()));
13941396
}
13951397
if !b.is_zero() {
1396-
assert!(c.div_rem(&b) == (copy a, Zero::zero()));
1398+
assert!(c.div_rem(&b) == (a.clone(), Zero::zero()));
13971399
}
13981400
}
13991401

@@ -1555,7 +1557,7 @@ mod biguint_tests {
15551557
15561558
#[cfg(test)]
15571559
mod bigint_tests {
1558-
use super::{BigInt, BigUint, BigDigit, Sign, Minus, Zero, Plus};
1560+
use super::*;
15591561
use core::cmp::{Less, Equal, Greater};
15601562
use core::num::{IntConvertible, Zero, One, FromStrRadix};
15611563

0 commit comments

Comments
 (0)