diff --git a/src/lib.rs b/src/lib.rs index eaec869..27ac2a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1954,6 +1954,24 @@ impl From for BigDecimal { } } +impl From for BigDecimal { + fn from(n: i128) -> Self { + BigDecimal { + int_val: BigInt::from(n), + scale: 0, + } + } +} + +impl From for BigDecimal { + fn from(n: u128) -> Self { + BigDecimal { + int_val: BigInt::from(n), + scale: 0, + } + } +} + impl From<(BigInt, i64)> for BigDecimal { #[inline] fn from((int_val, scale): (BigInt, i64)) -> Self { @@ -2023,6 +2041,16 @@ impl FromPrimitive for BigDecimal { Some(BigDecimal::from(n)) } + #[inline] + fn from_i128(n: i128) -> Option { + Some(BigDecimal::from(n)) + } + + #[inline] + fn from_u128(n: u128) -> Option { + Some(BigDecimal::from(n)) + } + #[inline] fn from_f32(n: f32) -> Option { BigDecimal::try_from(n).ok() @@ -3285,4 +3313,14 @@ mod bigdecimal_tests { fn test_bad_string_only_decimal_and_exponent() { BigDecimal::from_str(".e4").unwrap(); } + + #[test] + fn test_from_i128() { + BigDecimal::from_i128(-368934881474191032320).unwrap(); + } + + #[test] + fn test_from_u128() { + BigDecimal::from_i128(668934881474191032320).unwrap(); + } }