Skip to main content

fastnum/decimal/macros/
dec.rs

1macro_rules! macro_impl {
2    ($d:tt, $DEC: ident, $bits: literal, $sign: ident, $name: ident) => {
3        #[macro_export]
4        #[doc = concat!("A macro to construct ", $bits, "-bit ", stringify!($sign), " [`", stringify!($DEC), "`](crate::", stringify!($DEC), ") decimal from literals in compile time.")]
5        ///
6        /// Const-evaluated in compile time macro-helper can be used for definitions of constants or variables whose value is known in compile time.
7        ///
8        /// # Examples:
9        ///
10        /// Basic usage:
11        ///
12        /// ```
13        /// use fastnum::*;
14        ///
15        #[doc = concat!("const N: ", stringify!($DEC), " = ", stringify!($name), "!(1.23456789);")]
16        /// assert!(!N.is_zero());
17        ///
18        #[doc = concat!("let num = ", stringify!($name), "!(0);")]
19        /// assert!(num.is_zero());
20        ///
21        #[doc = concat!("const A: ", stringify!($DEC), " = ", stringify!($name), "!(5);")]
22        #[doc = concat!("const B: ", stringify!($DEC), " = ", stringify!($name), "!(1_000);")]
23        #[doc = concat!("const C: ", stringify!($DEC), " = A.div(B);")]
24        ///
25        #[doc = concat!("assert_eq!(C, ", stringify!($name), "!(0.005));")]
26        ///
27        /// ```
28        ///
29        /// ## Static assertions:
30        ///
31        /// ```compile_fail
32        /// // The below example will fail to compile, as the function will panic at compile time:
33        #[doc = concat!("use fastnum::{", stringify!($name), ", ", stringify!($DEC), "}")]
34        ///
35        /// // Gives a compile error of "error[E0080]: evaluation of constant value failed...
36        /// // the evaluated program panicked at 'attempt to parse decimal from string containing invalid digit'",
37        #[doc = concat!("const N: ", stringify!($DEC), " = ", stringify!($name), "!(A1.23456789);")]
38        /// ```
39        ///
40        /// This allows you to perform all the necessary checks such as potentialy overflow or calculation accuracy loss and others at the compile time.
41        /// Protect from unexpected errors in runtime.
42        ///
43        macro_rules! $name {
44            ($d($d body:tt)*) => {{
45                const __CTX: $crate::decimal::Context = $crate::decimal::Context::default();
46                const __DECIMAL: $crate::$DEC = $crate::$DEC::parse_str(concat!($d(stringify!($d body)),*), __CTX);
47                __DECIMAL
48            }};
49        }
50    };
51}
52
53macro_impl!($, UD64, 64, unsigned, udec64);
54macro_impl!($, UD128, 128, unsigned, udec128);
55macro_impl!($, UD256, 256, unsigned, udec256);
56macro_impl!($, UD512, 512, unsigned, udec512);
57macro_impl!($, UD1024, 1024, unsigned, udec1024);
58// macro_impl!($, UD2048, 2048, unsigned, udec2048);
59// macro_impl!($, UD4096, 4096, unsigned, udec4096);
60// macro_impl!($, UD8192, 8192, unsigned, udec8192);
61
62macro_impl!($, D64, 64, signed, dec64);
63macro_impl!($, D128, 128, signed, dec128);
64macro_impl!($, D256, 256, signed, dec256);
65macro_impl!($, D512, 512, signed, dec512);
66macro_impl!($, D1024, 1024, signed, dec1024);
67// macro_impl!($, D2048, 2048, signed, dec2048);
68// macro_impl!($, D4096, 4096, signed, dec4096);
69// macro_impl!($, D8192, 8192, signed, dec8192);