Debian Codegraph @

debian/rust-vendor/bigdecimal/src/impl_convert.rs

raw
1 //! Code for implementing From/To BigDecimals
2
3 use crate::BigDecimal;
4 use crate::stdlib::convert::TryFrom;
5
6 use num_bigint::BigInt;
7
8
9 macro_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
31 impl_from_int_primitive!(u8);
32 impl_from_int_primitive!(u16);
33 impl_from_int_primitive!(u32);
34 impl_from_int_primitive!(u64);
35 impl_from_int_primitive!(u128);
36 impl_from_int_primitive!(i8);
37 impl_from_int_primitive!(i16);
38 impl_from_int_primitive!(i32);
39 impl_from_int_primitive!(i64);
40 impl_from_int_primitive!(i128);
41
42
43 impl 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
52 impl 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
62 impl 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
71 // Anything that may be a big-integer paired with a scale
72 // parameter may be a bigdecimal
73 impl<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 }