Decimal import.

This commit is contained in:
chriseth 2022-10-03 23:57:36 +02:00
parent c1f7de858c
commit 4963f5edeb
2 changed files with 28 additions and 2 deletions

View file

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
json = "0.12.4" json = "^0.12.4"
num-bigint = "^0.4.3"

View file

@ -48,7 +48,14 @@ impl From<&str> for U256 {
}; };
U256([high, low]) U256([high, low])
} else { } else {
todo!("Decimal import"); let digits = item.parse::<num_bigint::BigUint>().unwrap().to_u64_digits();
assert!(digits.len() <= 4);
U256([
u128::from(*digits.get(3).unwrap_or(&0)) << 64
| u128::from(*digits.get(2).unwrap_or(&0)),
u128::from(*digits.get(1).unwrap_or(&0)) << 64
| u128::from(*digits.get(0).unwrap_or(&0)),
])
} }
} }
} }
@ -149,4 +156,22 @@ mod test {
U256::from(u128::MAX) + U256::from(1) U256::from(u128::MAX) + U256::from(1)
); );
} }
#[test]
fn from_decimal() {
assert_eq!(U256::from("0"), U256::from(0));
assert_eq!(U256::from("10"), U256::from(10));
assert_eq!(
U256::from("680564733841876926926749214863536422910"),
U256::from(u128::MAX) + U256::from(u128::MAX)
);
assert_eq!(
U256::from("000680564733841876926926749214863536422910"),
U256::from(u128::MAX) + U256::from(u128::MAX)
);
assert_eq!(
U256::from("340282366920938463463374607431768211456"),
U256::from(u128::MAX) + U256::from(1)
);
}
} }