add debug with num-bigint - TODO: add native debug

This commit is contained in:
EvilMuffinHa 2022-08-03 00:03:36 -04:00
parent 9fd27995b2
commit f313051907

View File

@ -58,6 +58,26 @@ enum SmallUintType {
Heap((*mut u32, usize)), 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 { impl Drop for SmallInt {
fn drop(&mut self) { fn drop(&mut self) {
if let Self(SmallIntType::Heap((r, s))) = self { if let Self(SmallIntType::Heap((r, s))) = self {
@ -228,9 +248,9 @@ impl TryFrom<SmallInt> for SmallUint {
} }
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
impl From<BigInt> for SmallInt { impl From<&BigInt> for SmallInt {
fn from(b: BigInt) -> Self { fn from(b: &BigInt) -> Self {
match (&b).try_into() { match b.try_into() {
Ok(i) => Self(SmallIntType::Inline(i)), Ok(i) => Self(SmallIntType::Inline(i)),
Err(_) => { Err(_) => {
let (sign, vec) = b.to_u32_digits(); let (sign, vec) = b.to_u32_digits();
@ -249,8 +269,8 @@ impl From<BigInt> for SmallInt {
} }
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
impl From<SmallInt> for BigInt { impl From<&SmallInt> for BigInt {
fn from(s: SmallInt) -> Self { fn from(s: &SmallInt) -> Self {
match s.0 { match s.0 {
SmallIntType::Inline(i) => Self::from(i), SmallIntType::Inline(i) => Self::from(i),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
@ -271,9 +291,9 @@ impl From<SmallInt> for BigInt {
} }
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
impl From<BigUint> for SmallUint { impl From<&BigUint> for SmallUint {
fn from(b: BigUint) -> Self { fn from(b: &BigUint) -> Self {
match (&b).try_into() { match b.try_into() {
Ok(i) => Self(SmallUintType::Inline(i)), Ok(i) => Self(SmallUintType::Inline(i)),
Err(_) => { Err(_) => {
let vec = b.to_u32_digits(); let vec = b.to_u32_digits();
@ -286,8 +306,8 @@ impl From<BigUint> for SmallUint {
} }
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
impl From<SmallUint> for BigUint { impl From<&SmallUint> for BigUint {
fn from(s: SmallUint) -> Self { fn from(s: &SmallUint) -> Self {
match s.0 { match s.0 {
SmallUintType::Inline(i) => Self::from(i), SmallUintType::Inline(i) => Self::from(i),
SmallUintType::Heap((r, s)) => { SmallUintType::Heap((r, s)) => {
@ -337,10 +357,11 @@ mod conversion_tests {
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_bigint() { fn test_bigint() {
let i = BigInt::new(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]); 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!( assert_eq!(
BigInt::from(s).to_u32_digits(), BigInt::from(&s).to_u32_digits(),
(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]) (Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3])
); );
} }
} }