bigdecimal/
impl_convert.rs
1use crate::BigDecimal;
4use crate::stdlib::convert::TryFrom;
5
6use num_bigint::BigInt;
7
8
9macro_rules! impl_from_int_primitive {
10 ($t:ty) => {
11 impl From<$t> for BigDecimal {
12 fn from(n: $t) -> Self {
13 BigDecimal {
14 int_val: n.into(),
15 scale: 0,
16 }
17 }
18 }
19
20 impl From<&$t> for BigDecimal {
21 fn from(n: &$t) -> Self {
22 BigDecimal {
23 int_val: (*n).into(),
24 scale: 0,
25 }
26 }
27 }
28 };
29}
30
31impl_from_int_primitive!(u8);
32impl_from_int_primitive!(u16);
33impl_from_int_primitive!(u32);
34impl_from_int_primitive!(u64);
35impl_from_int_primitive!(u128);
36impl_from_int_primitive!(i8);
37impl_from_int_primitive!(i16);
38impl_from_int_primitive!(i32);
39impl_from_int_primitive!(i64);
40impl_from_int_primitive!(i128);
41
42
43impl TryFrom<f32> for BigDecimal {
44 type Error = super::ParseBigDecimalError;
45
46 #[inline]
47 fn try_from(n: f32) -> Result<Self, Self::Error> {
48 crate::parsing::try_parse_from_f32(n)
49 }
50}
51
52impl TryFrom<f64> for BigDecimal {
53 type Error = super::ParseBigDecimalError;
54
55 #[inline]
56 fn try_from(n: f64) -> Result<Self, Self::Error> {
57 crate::parsing::try_parse_from_f64(n)
58 }
59}
60
61
62impl From<BigInt> for BigDecimal {
63 fn from(int_val: BigInt) -> Self {
64 BigDecimal {
65 int_val: int_val,
66 scale: 0,
67 }
68 }
69}
70
71impl<T: Into<BigInt>> From<(T, i64)> for BigDecimal {
74 fn from((int_val, scale): (T, i64)) -> Self {
75 Self {
76 int_val: int_val.into(),
77 scale: scale,
78 }
79 }
80}