bigdecimal/macros.rs
1// \file src/macros.rs
2//! macros for
3/*
4macro_rules! forward_val_val_binop {
5 (impl $imp:ident for $res:ty, $method:ident) => {
6 impl $imp<$res> for $res {
7 type Output = $res;
8
9 #[inline]
10 fn $method(self, other: $res) -> $res {
11 // forward to val-ref
12 $imp::$method(self, &other)
13 }
14 }
15 };
16}
17
18*/
19macro_rules! forward_ref_val_binop {
20 (impl $imp:ident for $res:ty, $method:ident) => {
21 impl<'a> $imp<$res> for &'a $res {
22 type Output = $res;
23
24 #[inline]
25 fn $method(self, other: $res) -> $res {
26 // forward to ref-ref
27 $imp::$method(self, &other)
28 }
29 }
30 };
31}
32
33macro_rules! forward_communative_binop {
34 (impl $trait:ident<$t1:ty>::$method:ident for $t2:ty) => {
35 forward_communative_binop!(
36 impl $trait<$t1>::$method for $t2; Output=BigDecimal
37 );
38 };
39 (impl $trait:ident<$t1:ty>::$method:ident for $t2:ty; Output=$output:ty) => {
40 impl $trait<$t1> for $t2 {
41 type Output = $output;
42
43 #[inline]
44 fn $method(self, rhs: $t1) -> Self::Output {
45 // swap operands
46 $trait::$method(rhs, self)
47 }
48 }
49 };
50}
51
52macro_rules! forward_bigdecimalref_binop {
53 (impl $trait:ident<$t1:ty>::$method:ident for $t2:ty) => {
54 impl $trait<$t1> for $t2 {
55 type Output = BigDecimal;
56
57 #[inline]
58 fn $method(self, rhs: $t1) -> Self::Output {
59 // convert to BigDecimalRef and apply method
60 $trait::$method(BigDecimalRef::from(self), rhs)
61 }
62 }
63 };
64}
65
66/*
67macro_rules! forward_val_ref_binop {
68 (impl $imp:ident for $res:ty, $method:ident) => {
69 impl<'a> $imp<&'a $res> for $res {
70 type Output = $res;
71
72 #[inline]
73 fn $method(self, other: &$res) -> $res {
74 // forward to ref-ref
75 $imp::$method(&self, other)
76 }
77 }
78 };
79}
80
81// Forward everything to ref-ref, when reusing storage is not helpful
82macro_rules! forward_all_binop_to_ref_ref {
83 (impl $imp:ident for $res:ty, $method:ident) => {
84 forward_val_val_binop!(impl $imp for $res, $method);
85 forward_val_ref_binop!(impl $imp for $res, $method);
86 forward_ref_val_binop!(impl $imp for $res, $method);
87 };
88}
89*/