Skip to content

libstd: impl Clone for BigUint/BigInt and replace copy with .clone() #6127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/libstd/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ A big unsigned integer type.
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]
}
Expand Down Expand Up @@ -680,7 +681,7 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) {
}

/// A Sign is a BigInt's composing element.
#[deriving(Eq)]
#[deriving(Eq, Clone)]
pub enum Sign { Minus, Zero, Plus }

impl Ord for Sign {
Expand Down Expand Up @@ -726,6 +727,7 @@ impl Neg<Sign> for Sign {
}

/// A big signed integer type.
#[deriving(Clone)]
pub struct BigInt {
priv sign: Sign,
priv data: BigUint
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -850,8 +852,8 @@ impl Add<BigInt, BigInt> 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),
Expand All @@ -866,7 +868,7 @@ impl Sub<BigInt, BigInt> 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),
Expand Down Expand Up @@ -913,7 +915,7 @@ impl Rem<BigInt, BigInt> for BigInt {
impl Neg<BigInt> 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())
}
}

Expand Down Expand Up @@ -1100,9 +1102,9 @@ pub impl BigInt {

#[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() {
Expand Down Expand Up @@ -1390,10 +1392,10 @@ mod biguint_tests {
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()));
}
}

Expand Down Expand Up @@ -1555,7 +1557,7 @@ mod biguint_tests {

#[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};

Expand Down