Skip to content

Commit 01be9e9

Browse files
committed
extra: Add ToBigInt and ToBigUint traits
1 parent 9de7ad2 commit 01be9e9

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/libextra/num/bigint.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,52 @@ impl FromPrimitive for BigUint {
547547
}
548548
}
549549

550+
pub trait ToBigUint {
551+
fn to_biguint(&self) -> Option<BigUint>;
552+
}
553+
554+
impl ToBigUint for BigInt {
555+
#[inline]
556+
fn to_biguint(&self) -> Option<BigUint> {
557+
if self.sign == Plus {
558+
Some(self.data.clone())
559+
} else if self.sign == Zero {
560+
Some(Zero::zero())
561+
} else {
562+
None
563+
}
564+
}
565+
}
566+
567+
impl ToBigUint for BigUint {
568+
#[inline]
569+
fn to_biguint(&self) -> Option<BigUint> {
570+
Some(self.clone())
571+
}
572+
}
573+
574+
macro_rules! impl_to_biguint(
575+
($T:ty, $from_ty:path) => {
576+
impl ToBigUint for $T {
577+
#[inline]
578+
fn to_biguint(&self) -> Option<BigUint> {
579+
$from_ty(*self)
580+
}
581+
}
582+
}
583+
)
584+
585+
impl_to_biguint!(int, FromPrimitive::from_int)
586+
impl_to_biguint!(i8, FromPrimitive::from_i8)
587+
impl_to_biguint!(i16, FromPrimitive::from_i16)
588+
impl_to_biguint!(i32, FromPrimitive::from_i32)
589+
impl_to_biguint!(i64, FromPrimitive::from_i64)
590+
impl_to_biguint!(uint, FromPrimitive::from_uint)
591+
impl_to_biguint!(u8, FromPrimitive::from_u8)
592+
impl_to_biguint!(u16, FromPrimitive::from_u16)
593+
impl_to_biguint!(u32, FromPrimitive::from_u32)
594+
impl_to_biguint!(u64, FromPrimitive::from_u64)
595+
550596
impl ToStrRadix for BigUint {
551597
fn to_str_radix(&self, radix: uint) -> ~str {
552598
assert!(1 < radix && radix <= 16);
@@ -1140,6 +1186,50 @@ impl FromPrimitive for BigInt {
11401186
}
11411187
}
11421188

1189+
pub trait ToBigInt {
1190+
fn to_bigint(&self) -> Option<BigInt>;
1191+
}
1192+
1193+
impl ToBigInt for BigInt {
1194+
#[inline]
1195+
fn to_bigint(&self) -> Option<BigInt> {
1196+
Some(self.clone())
1197+
}
1198+
}
1199+
1200+
impl ToBigInt for BigUint {
1201+
#[inline]
1202+
fn to_bigint(&self) -> Option<BigInt> {
1203+
if self.is_zero() {
1204+
Some(Zero::zero())
1205+
} else {
1206+
Some(BigInt { sign: Plus, data: self.clone() })
1207+
}
1208+
}
1209+
}
1210+
1211+
macro_rules! impl_to_bigint(
1212+
($T:ty, $from_ty:path) => {
1213+
impl ToBigInt for $T {
1214+
#[inline]
1215+
fn to_bigint(&self) -> Option<BigInt> {
1216+
$from_ty(*self)
1217+
}
1218+
}
1219+
}
1220+
)
1221+
1222+
impl_to_bigint!(int, FromPrimitive::from_int)
1223+
impl_to_bigint!(i8, FromPrimitive::from_i8)
1224+
impl_to_bigint!(i16, FromPrimitive::from_i16)
1225+
impl_to_bigint!(i32, FromPrimitive::from_i32)
1226+
impl_to_bigint!(i64, FromPrimitive::from_i64)
1227+
impl_to_bigint!(uint, FromPrimitive::from_uint)
1228+
impl_to_bigint!(u8, FromPrimitive::from_u8)
1229+
impl_to_bigint!(u16, FromPrimitive::from_u16)
1230+
impl_to_bigint!(u32, FromPrimitive::from_u32)
1231+
impl_to_bigint!(u64, FromPrimitive::from_u64)
1232+
11431233
impl ToStrRadix for BigInt {
11441234
#[inline]
11451235
fn to_str_radix(&self, radix: uint) -> ~str {

0 commit comments

Comments
 (0)