Skip to content

Commit 8e1c6a3

Browse files
committed
bigint: add From implementations
From all primative unsigned ints to BigUint From all primative ints to BigInt From BigUint to BigInt Closes: #117
1 parent f2ea30d commit 8e1c6a3

File tree

1 file changed

+94
-21
lines changed

1 file changed

+94
-21
lines changed

src/bigint.rs

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,27 +1105,47 @@ impl ToPrimitive for BigUint {
11051105
impl FromPrimitive for BigUint {
11061106
#[inline]
11071107
fn from_i64(n: i64) -> Option<BigUint> {
1108-
if n > 0 {
1109-
FromPrimitive::from_u64(n as u64)
1110-
} else if n == 0 {
1111-
Some(Zero::zero())
1108+
if n >= 0 {
1109+
Some(BigUint::from(n as u64))
11121110
} else {
11131111
None
11141112
}
11151113
}
11161114

1117-
// `DoubleBigDigit` size dependent
11181115
#[inline]
11191116
fn from_u64(n: u64) -> Option<BigUint> {
1120-
let n = match big_digit::from_doublebigdigit(n) {
1121-
(0, 0) => Zero::zero(),
1122-
(0, n0) => BigUint::new(vec!(n0)),
1123-
(n1, n0) => BigUint::new(vec!(n0, n1))
1124-
};
1125-
Some(n)
1117+
Some(BigUint::from(n))
11261118
}
11271119
}
11281120

1121+
impl From<u64> for BigUint {
1122+
// `DoubleBigDigit` size dependent
1123+
#[inline]
1124+
fn from(n: u64) -> Self {
1125+
match big_digit::from_doublebigdigit(n) {
1126+
(0, 0) => BigUint::zero(),
1127+
(0, n0) => BigUint { data: vec![n0] },
1128+
(n1, n0) => BigUint { data: vec![n0, n1] },
1129+
}
1130+
}
1131+
}
1132+
1133+
macro_rules! impl_biguint_from_uint {
1134+
($T:ty) => {
1135+
impl From<$T> for BigUint {
1136+
#[inline]
1137+
fn from(n: $T) -> Self {
1138+
BigUint::from(n as u64)
1139+
}
1140+
}
1141+
}
1142+
}
1143+
1144+
impl_biguint_from_uint!(u8);
1145+
impl_biguint_from_uint!(u16);
1146+
impl_biguint_from_uint!(u32);
1147+
impl_biguint_from_uint!(usize);
1148+
11291149
/// A generic trait for converting a value to a `BigUint`.
11301150
pub trait ToBigUint {
11311151
/// Converts the value of `self` to a `BigUint`.
@@ -1973,24 +1993,77 @@ impl ToPrimitive for BigInt {
19731993
impl FromPrimitive for BigInt {
19741994
#[inline]
19751995
fn from_i64(n: i64) -> Option<BigInt> {
1996+
Some(BigInt::from(n))
1997+
}
1998+
1999+
#[inline]
2000+
fn from_u64(n: u64) -> Option<BigInt> {
2001+
Some(BigInt::from(n))
2002+
}
2003+
}
2004+
2005+
impl From<i64> for BigInt {
2006+
#[inline]
2007+
fn from(n: i64) -> Self {
19762008
if n >= 0 {
1977-
FromPrimitive::from_u64(n as u64)
2009+
BigInt::from(n as u64)
19782010
} else {
19792011
let u = u64::MAX - (n as u64) + 1;
1980-
FromPrimitive::from_u64(u).map(|n| {
1981-
BigInt::from_biguint(Minus, n)
1982-
})
2012+
BigInt { sign: Minus, data: BigUint::from(u) }
19832013
}
19842014
}
2015+
}
19852016

2017+
macro_rules! impl_bigint_from_int {
2018+
($T:ty) => {
2019+
impl From<$T> for BigInt {
2020+
#[inline]
2021+
fn from(n: $T) -> Self {
2022+
BigInt::from(n as i64)
2023+
}
2024+
}
2025+
}
2026+
}
2027+
2028+
impl_bigint_from_int!(i8);
2029+
impl_bigint_from_int!(i16);
2030+
impl_bigint_from_int!(i32);
2031+
impl_bigint_from_int!(isize);
2032+
2033+
impl From<u64> for BigInt {
19862034
#[inline]
1987-
fn from_u64(n: u64) -> Option<BigInt> {
1988-
if n == 0 {
1989-
Some(Zero::zero())
2035+
fn from(n: u64) -> Self {
2036+
if n > 0 {
2037+
BigInt { sign: Plus, data: BigUint::from(n) }
2038+
} else {
2039+
BigInt::zero()
2040+
}
2041+
}
2042+
}
2043+
2044+
macro_rules! impl_bigint_from_uint {
2045+
($T:ty) => {
2046+
impl From<$T> for BigInt {
2047+
#[inline]
2048+
fn from(n: $T) -> Self {
2049+
BigInt::from(n as u64)
2050+
}
2051+
}
2052+
}
2053+
}
2054+
2055+
impl_bigint_from_uint!(u8);
2056+
impl_bigint_from_uint!(u16);
2057+
impl_bigint_from_uint!(u32);
2058+
impl_bigint_from_uint!(usize);
2059+
2060+
impl From<BigUint> for BigInt {
2061+
#[inline]
2062+
fn from(n: BigUint) -> Self {
2063+
if n.is_zero() {
2064+
BigInt::zero()
19902065
} else {
1991-
FromPrimitive::from_u64(n).map(|n| {
1992-
BigInt::from_biguint(Plus, n)
1993-
})
2066+
BigInt { sign: Plus, data: n }
19942067
}
19952068
}
19962069
}

0 commit comments

Comments
 (0)