helper_macros.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /// Implement impl Op<B> for &A by forwarding to impl Op<B> for A.
  2. /// Includes &B.
  3. macro_rules! forward_binop_first_arg_by_value {
  4. (impl $tr:ident<$t2:ty> for $t1:ty, $f:ident) => {
  5. impl $tr<$t2> for &$t1 {
  6. type Output = <$t1 as $tr<$t2>>::Output;
  7. #[inline]
  8. fn $f(self, rhs: $t2) -> Self::Output {
  9. (*self).$f(rhs)
  10. }
  11. }
  12. impl<'a> $tr<&'a $t2> for &$t1 {
  13. type Output = <$t1 as $tr<&'a $t2>>::Output;
  14. #[inline]
  15. fn $f(self, rhs: &$t2) -> Self::Output {
  16. (*self).$f(rhs)
  17. }
  18. }
  19. };
  20. }
  21. /// Implement impl Op<&B> for A by forwarding to impl Op<B> for A.
  22. /// Includes &A.
  23. macro_rules! forward_binop_second_arg_by_value {
  24. (impl $tr:ident<$t2:ty> for $t1:ty, $f:ident) => {
  25. impl $tr<&$t2> for $t1 {
  26. type Output = <$t1 as $tr<$t2>>::Output;
  27. #[inline]
  28. fn $f(self, rhs: &$t2) -> Self::Output {
  29. self.$f(*rhs)
  30. }
  31. }
  32. impl<'a> $tr<&$t2> for &'a $t1 {
  33. type Output = <&'a $t1 as $tr<$t2>>::Output;
  34. #[inline]
  35. fn $f(self, rhs: &$t2) -> Self::Output {
  36. self.$f(*rhs)
  37. }
  38. }
  39. };
  40. }
  41. /// Implement impl Op<&B> for A by forwarding to impl Op<B> for A.
  42. /// Here Op has OutputDiv and OutputRem, rather than just Output.
  43. /// Includes &A.
  44. macro_rules! forward_div_rem_second_arg_by_value {
  45. (impl $tr:ident<$t2:ty> for $t1:ty, $f:ident) => {
  46. impl $tr<&$t2> for $t1 {
  47. type OutputDiv = <$t1 as $tr<$t2>>::OutputDiv;
  48. type OutputRem = <$t1 as $tr<$t2>>::OutputRem;
  49. #[inline]
  50. fn $f(self, rhs: &$t2) -> (Self::OutputDiv, Self::OutputRem) {
  51. self.$f(*rhs)
  52. }
  53. }
  54. impl<'a> $tr<&$t2> for &'a $t1 {
  55. type OutputDiv = <&'a $t1 as $tr<$t2>>::OutputDiv;
  56. type OutputRem = <&'a $t1 as $tr<$t2>>::OutputRem;
  57. #[inline]
  58. fn $f(self, rhs: &$t2) -> (Self::OutputDiv, Self::OutputRem) {
  59. self.$f(*rhs)
  60. }
  61. }
  62. };
  63. }
  64. /// Implement impl Op<B> for A by forwarding to impl Op<A> for B.
  65. /// Includes &A and &B.
  66. macro_rules! forward_binop_swap_args {
  67. (impl $tr:ident<$t2:ty> for $t1:ty, $f:ident) => {
  68. impl $tr<$t2> for $t1 {
  69. type Output = <$t2 as $tr<$t1>>::Output;
  70. #[inline]
  71. fn $f(self, rhs: $t2) -> Self::Output {
  72. rhs.$f(self)
  73. }
  74. }
  75. impl<'a> $tr<&'a $t2> for $t1 {
  76. type Output = <&'a $t2 as $tr<$t1>>::Output;
  77. #[inline]
  78. fn $f(self, rhs: &$t2) -> Self::Output {
  79. rhs.$f(self)
  80. }
  81. }
  82. impl<'a> $tr<$t2> for &'a $t1 {
  83. type Output = <$t2 as $tr<&'a $t1>>::Output;
  84. #[inline]
  85. fn $f(self, rhs: $t2) -> Self::Output {
  86. rhs.$f(self)
  87. }
  88. }
  89. impl<'a, 'b> $tr<&'a $t2> for &'b $t1 {
  90. type Output = <&'a $t2 as $tr<&'b $t1>>::Output;
  91. #[inline]
  92. fn $f(self, rhs: &$t2) -> Self::Output {
  93. rhs.$f(self)
  94. }
  95. }
  96. };
  97. }
  98. /// Implement impl OpAssign<&B> for A by forwarding to impl OpAssign<B> for A.
  99. macro_rules! forward_binop_assign_arg_by_value {
  100. (impl $tr:ident<$t2:ty> for $t1:ty, $f:ident) => {
  101. impl $tr<&$t2> for $t1 {
  102. #[inline]
  103. fn $f(&mut self, rhs: &$t2) {
  104. self.$f(*rhs)
  105. }
  106. }
  107. };
  108. }
  109. pub(crate) use {
  110. forward_binop_assign_arg_by_value, forward_binop_first_arg_by_value,
  111. forward_binop_second_arg_by_value, forward_binop_swap_args,
  112. forward_div_rem_second_arg_by_value,
  113. };