From f313051907ac4144ec49810f0afc40601bf14288 Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Wed, 3 Aug 2022 00:03:36 -0400 Subject: [PATCH] add debug with num-bigint - TODO: add native debug --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d3f54f..9693ea7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,26 @@ enum SmallUintType { Heap((*mut u32, usize)), } +// TODO: Need to reimplement Debug, to_str_radix, and from_str_radix without num-bigint feature for +// SmallInt and SmallUint + +// TODO: add native operations for SmallInt and SmallUint + +#[cfg(feature="num-bigint")] +impl core::fmt::Debug for SmallInt { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", BigInt::from(self)) + } +} + +#[cfg(feature="num-bigint")] +impl core::fmt::Debug for SmallUint { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", BigUint::from(self)) + } +} + + impl Drop for SmallInt { fn drop(&mut self) { if let Self(SmallIntType::Heap((r, s))) = self { @@ -228,9 +248,9 @@ impl TryFrom for SmallUint { } #[cfg(feature = "num-bigint")] -impl From for SmallInt { - fn from(b: BigInt) -> Self { - match (&b).try_into() { +impl From<&BigInt> for SmallInt { + fn from(b: &BigInt) -> Self { + match b.try_into() { Ok(i) => Self(SmallIntType::Inline(i)), Err(_) => { let (sign, vec) = b.to_u32_digits(); @@ -249,8 +269,8 @@ impl From for SmallInt { } #[cfg(feature = "num-bigint")] -impl From for BigInt { - fn from(s: SmallInt) -> Self { +impl From<&SmallInt> for BigInt { + fn from(s: &SmallInt) -> Self { match s.0 { SmallIntType::Inline(i) => Self::from(i), SmallIntType::Heap((r, s)) => { @@ -271,9 +291,9 @@ impl From for BigInt { } #[cfg(feature = "num-bigint")] -impl From for SmallUint { - fn from(b: BigUint) -> Self { - match (&b).try_into() { +impl From<&BigUint> for SmallUint { + fn from(b: &BigUint) -> Self { + match b.try_into() { Ok(i) => Self(SmallUintType::Inline(i)), Err(_) => { let vec = b.to_u32_digits(); @@ -286,8 +306,8 @@ impl From for SmallUint { } #[cfg(feature = "num-bigint")] -impl From for BigUint { - fn from(s: SmallUint) -> Self { +impl From<&SmallUint> for BigUint { + fn from(s: &SmallUint) -> Self { match s.0 { SmallUintType::Inline(i) => Self::from(i), SmallUintType::Heap((r, s)) => { @@ -337,10 +357,11 @@ mod conversion_tests { #[cfg(feature = "num-bigint")] fn test_bigint() { let i = BigInt::new(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]); - let s = SmallInt::from(i); + let s = SmallInt::from(&i); assert_eq!( - BigInt::from(s).to_u32_digits(), + BigInt::from(&s).to_u32_digits(), (Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]) ); } + }