hmath/ubigint/convert/
from.rs

1use crate::{Ratio, BigInt, UBigInt, ConversionError};
2use crate::{impl_from_for_ref, impl_tryfrom_for_ref, impl_trait_for_general};
3
4macro_rules! impl_from_ref_ubigint {
5    ($t: ty) => (
6        impl_from_for_ref!(UBigInt, $t);
7    );
8    ($t: ty, $($u: ty), +) => (
9        impl_from_ref_ubigint!($t);
10        impl_from_ref_ubigint!($($u),+);
11    )
12}
13
14macro_rules! impl_tryfrom_ref_ubigint {
15    ($t: ty) => (
16        impl_tryfrom_for_ref!(UBigInt, $t);
17    );
18    ($t: ty, $($u: ty), +) => (
19        impl_tryfrom_ref_ubigint!($t);
20        impl_tryfrom_ref_ubigint!($($u),+);
21    )
22}
23
24impl_from_ref_ubigint!(bool, u8, u16, u32, u64, u128, usize);
25impl_tryfrom_ref_ubigint!(f32, f64, i8, i16, i32, i64, i128, isize);
26
27impl From<bool> for UBigInt {
28    fn from(b: bool) -> Self {
29        if b {
30            UBigInt::one()
31        } else {
32            UBigInt::zero()
33        }
34    }
35}
36
37/// It returns the truncated value of `Ratio::from(n)`.
38impl TryFrom<f32> for UBigInt {
39    type Error = ConversionError;
40
41    fn try_from(n: f32) -> Result<Self, Self::Error> {
42        Ratio::try_from(n)?.truncate_bi().try_into()
43    }
44}
45
46/// It returns the truncated value of `Ratio::from(n)`.
47impl TryFrom<f64> for UBigInt {
48    type Error = ConversionError;
49
50    fn try_from(n: f64) -> Result<Self, Self::Error> {
51        Ratio::try_from(n)?.truncate_bi().try_into()
52    }
53}
54
55impl TryFrom<i8> for UBigInt {
56    type Error = ConversionError;
57
58    fn try_from(n: i8) -> Result<Self, Self::Error> {
59        Ok(UBigInt::from_u32(u32::try_from(n)?))
60    }
61}
62
63impl TryFrom<i16> for UBigInt {
64    type Error = ConversionError;
65
66    fn try_from(n: i16) -> Result<Self, Self::Error> {
67        Ok(UBigInt::from_u32(u32::try_from(n)?))
68    }
69}
70
71impl TryFrom<i32> for UBigInt {
72    type Error = ConversionError;
73
74    fn try_from(n: i32) -> Result<Self, Self::Error> {
75        Ok(UBigInt::from_u32(u32::try_from(n)?))
76    }
77}
78
79impl TryFrom<i64> for UBigInt {
80    type Error = ConversionError;
81
82    fn try_from(n: i64) -> Result<Self, Self::Error> {
83        Ok(UBigInt::from_u64(u64::try_from(n)?))
84    }
85}
86
87impl TryFrom<i128> for UBigInt {
88    type Error = ConversionError;
89
90    fn try_from(n: i128) -> Result<Self, Self::Error> {
91        Ok(UBigInt::from_u128(u128::try_from(n)?))
92    }
93}
94
95impl TryFrom<isize> for UBigInt {
96    type Error = ConversionError;
97
98    fn try_from(n: isize) -> Result<Self, Self::Error> {
99        Ok(UBigInt::from_u64(u64::try_from(n)?))
100    }
101}
102
103impl_trait_for_general!(From, u8, UBigInt, from_u32);
104impl_trait_for_general!(From, u16, UBigInt, from_u32);
105impl_trait_for_general!(From, u32, UBigInt, from_u32);
106impl_trait_for_general!(From, u64, UBigInt, from_u64);
107impl_trait_for_general!(From, u128, UBigInt, from_u128);
108
109impl_trait_for_general!(TryFrom, &str, UBigInt, from_string);
110
111impl From<usize> for UBigInt {
112    fn from(n: usize) -> Self {
113        UBigInt::from_u64(n as u64)
114    }
115}
116
117impl TryFrom<String> for UBigInt {
118    type Error = ConversionError;
119
120    fn try_from(n: String) -> Result<Self, Self::Error> {
121        UBigInt::from_string(&n)
122    }
123}
124
125impl TryFrom<&Ratio> for UBigInt {
126    type Error = ConversionError;
127
128    fn try_from(n: &Ratio) -> Result<Self, Self::Error> {
129        n.truncate_bi().try_into()
130    }
131}
132
133impl TryFrom<&BigInt> for UBigInt {
134    type Error = ConversionError;
135
136    fn try_from(n: &BigInt) -> Result<Self, Self::Error> {
137        n.to_ubi()
138    }
139}