mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-04 09:01:39 -05:00
add debug with num-bigint - TODO: add native debug
This commit is contained in:
parent
9fd27995b2
commit
f313051907
45
src/lib.rs
45
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<SmallInt> for SmallUint {
|
|||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl From<BigInt> 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<BigInt> for SmallInt {
|
|||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl From<SmallInt> 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<SmallInt> for BigInt {
|
|||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl From<BigUint> 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<BigUint> for SmallUint {
|
|||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl From<SmallUint> 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])
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user