Skip to main content

fastnum/decimal/udec/impls/
from.rs

1use crate::decimal::{Decimal, DecimalError, ParseError, UnsignedDecimal};
2
3type D<const N: usize> = Decimal<N>;
4type UD<const N: usize> = UnsignedDecimal<N>;
5
6impl<const N: usize> TryFrom<D<N>> for UD<N> {
7    type Error = DecimalError;
8
9    #[inline]
10    fn try_from(d: D<N>) -> Result<Self, Self::Error> {
11        if d.is_negative() {
12            return Err(DecimalError::Invalid);
13        }
14        Ok(Self::new(d))
15    }
16}
17
18macro_rules! from_uint_impl {
19    ($($name:ident $uint:ident $(#$try: ident)?),*) => {
20        impl<const N: usize> UD<N> {
21            $(
22                from_uint_impl!(@ $($try)? $name $uint);
23            )*
24        }
25
26        $(
27            from_uint_impl!(@@ $($try)? $name $uint);
28        )*
29    };
30    (@ $name:ident $uint:ident) => {
31        #[inline]
32        #[doc = concat!("Converts [`", stringify!($uint), "`] to [Decimal].")]
33        pub const fn $name(n: $uint) -> Self {
34            Self::new(D::$name(n))
35        }
36    };
37    (@ TRY $name:ident $uint:ident) => {
38        #[inline]
39        #[doc = concat!("Converts [`", stringify!($uint), "`] to [Decimal].")]
40        pub const fn $name(n: $uint) -> Result<Self, ParseError> {
41            match D::$name(n) {
42                Ok(d) => Ok(Self::new(d)),
43                Err(e) => Err(e),
44            }
45        }
46    };
47    (@@ $name:ident $uint:ident) => {
48        impl<const N: usize> From<$uint> for UD<N> {
49            #[inline]
50            fn from(n: $uint) -> Self {
51                Self::$name(n)
52            }
53        }
54    };
55    (@@ TRY $name:ident $uint:ident) => {
56        impl<const N: usize> TryFrom<$uint> for UD<N> {
57            type Error = ParseError;
58
59            #[inline]
60            fn try_from(n: $uint) -> Result<Self, Self::Error> {
61                Self::$name(n)
62            }
63        }
64    };
65}
66
67macro_rules! from_int_impl {
68    ($($name:ident $int:ident $(#$try: ident)?),*) => {
69        impl<const N: usize> UD<N> {
70            $(
71                #[inline]
72                #[doc = concat!("Try converts [`", stringify!($int), "`] to [UnsignedDecimal].")]
73                pub const fn $name(int: $int) -> Result<Self, ParseError> {
74                    if int < 0 {
75                        return Err(ParseError::Signed);
76                    }
77
78                    from_int_impl!(@ $($try)? $name int)
79                }
80            )*
81        }
82
83        $(
84            impl<const N: usize> TryFrom<$int> for UD<N> {
85                type Error = ParseError;
86
87                #[inline]
88                fn try_from(int: $int) -> Result<Self, Self::Error> {
89                    Self::$name(int)
90                }
91            }
92        )*
93    };
94    (@ $name:ident $int:ident) => {{
95        Ok(Self::new(D::$name($int)))
96    }};
97    (@ TRY $name:ident $int:ident) => {{
98        match D::$name($int) {
99            Ok(d) => Ok(Self::new(d)),
100            Err(e) => Err(e),
101        }
102    }};
103}
104
105macro_rules! try_from_f_impl {
106    ($($name:ident $pname:ident $num:ty,)*) => {
107        impl<const N: usize> UD<N> {
108            $(
109                #[inline]
110                #[doc = concat!("Try converts [`", stringify!($num), "`] to [UnsignedDecimal].")]
111                pub const fn $name(n: $num) -> Result<Self, ParseError> {
112                    if n.is_sign_negative() {
113                        return Err(ParseError::Signed);
114                    }
115                    Ok(Self::new(D::$pname(n)))
116                }
117            )*
118        }
119
120        $(
121            impl<const N: usize> TryFrom<$num> for UD<N> {
122                type Error = ParseError;
123
124                #[inline]
125                fn try_from(n: $num) -> Result<Self, Self::Error> {
126                    Self::$name(n)
127                }
128            }
129        )*
130    };
131}
132
133from_uint_impl!(
134    from_u8 u8,
135    from_u16 u16,
136    from_u32 u32,
137    from_u64 u64,
138    from_u128 u128 #TRY,
139    from_usize usize
140);
141
142from_int_impl!(
143    from_i8 i8,
144    from_i16 i16,
145    from_i32 i32,
146    from_i64 i64,
147    from_i128 i128 #TRY,
148    from_isize isize
149);
150
151try_from_f_impl!(
152    from_f32 from_f32 f32,
153    from_f64 from_f64 f64,
154);