Skip to main content

fastnum/decimal/dec/impls/
from.rs

1use crate::decimal::{dec::parse, Decimal, ParseError};
2
3type D<const N: usize> = Decimal<N>;
4
5macro_rules! from_num_impls {
6    ($($name:ident $num:ident $(#$try: ident)?),*) => {
7        impl<const N: usize> D<N> {
8            $(
9                from_num_impls!(@ $($try)? $name $num);
10            )*
11        }
12
13        $(
14            from_num_impls!(@@ $($try)? $name $num);
15        )*
16    };
17    (@ $name:ident $num:ident) => {
18        #[inline]
19        #[doc = concat!("Converts [`", stringify!($num), "`] to [Decimal].")]
20        pub const fn $name(n: $num) -> Self {
21            parse::$name(n)
22        }
23    };
24    (@ TRY $name:ident $num:ident) => {
25        #[inline]
26        #[doc = concat!("Converts [`", stringify!($num), "`] to [Decimal].")]
27        pub const fn $name(n: $num) -> Result<Self, ParseError> {
28            parse::$name(n)
29        }
30    };
31    (@@ $name:ident $num:ident) => {
32        impl<const N: usize> From<$num> for D<N> {
33            #[inline]
34            fn from(n: $num) -> Self {
35                Self::$name(n)
36            }
37        }
38    };
39    (@@ TRY $name:ident $num:ident) => {
40        impl<const N: usize> TryFrom<$num> for D<N> {
41            type Error = ParseError;
42
43            #[inline]
44            fn try_from(n: $num) -> Result<Self, Self::Error> {
45                Self::$name(n)
46            }
47        }
48    };
49}
50
51from_num_impls!(
52    from_u8 u8,
53    from_u16 u16,
54    from_u32 u32,
55    from_u64 u64,
56    from_u128 u128 #TRY,
57    from_usize usize,
58
59    from_i8 i8,
60    from_i16 i16,
61    from_i32 i32,
62    from_i64 i64,
63    from_i128 i128 #TRY,
64    from_isize isize,
65
66    from_f32 f32,
67    from_f64 f64
68);