Advertisement
AlexUaY

Untitled

Jan 29th, 2024
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.25 KB | None | 0 0
  1. macro_rules! impl_all {
  2.     ($for:ty, $from:ty) => {
  3.         impl Add<$from> for $for {
  4.             type Output = $for;
  5.  
  6.             fn add(self, other: $from) -> $for {
  7.                 Self(self.0 + Self::from(other).0)
  8.             }
  9.         }
  10.  
  11.         impl Sub<$from> for $for {
  12.             type Output = $for;
  13.  
  14.             fn sub(self, other: $from) -> $for {
  15.                 Self(self.0 - Self::from(other).0)
  16.             }
  17.         }
  18.  
  19.         impl Mul<$from> for $for {
  20.             type Output = $for;
  21.  
  22.             fn mul(self, other: $from) -> $for {
  23.                 Self(self.0 * Self::from(other).0)
  24.             }
  25.         }
  26.  
  27.         impl Div<$from> for $for {
  28.             type Output = $for;
  29.  
  30.             fn div(self, other: $from) -> $for {
  31.                 Self(self.0 / Self::from(other).0)
  32.             }
  33.         }
  34.  
  35.         impl AddAssign<$from> for $for {
  36.             fn add_assign(&mut self, other: $from) {
  37.                 self.0 += Self::from(other).0;
  38.             }
  39.         }
  40.  
  41.         impl SubAssign<$from> for $for {
  42.             fn sub_assign(&mut self, other: $from) {
  43.                 self.0 -= Self::from(other).0;
  44.             }
  45.         }
  46.  
  47.         impl MulAssign<$from> for $for {
  48.             fn mul_assign(&mut self, other: $from) {
  49.                 self.0 *= Self::from(other).0;
  50.             }
  51.         }
  52.  
  53.         impl DivAssign<$from> for $for {
  54.             fn div_assign(&mut self, other: $from) {
  55.                 self.0 /= Self::from(other).0;
  56.             }
  57.         }
  58.  
  59.         impl PartialEq<$from> for $for {
  60.             fn eq(&self, other: &$from) -> bool {
  61.                 self.0 == Self::from(*other).0
  62.             }
  63.         }
  64.  
  65.         impl PartialOrd<$from> for $for {
  66.             fn partial_cmp(&self, other: &$from) -> Option<std::cmp::Ordering> {
  67.                 self.0.partial_cmp(&Self::from(*other).0)
  68.             }
  69.         }
  70.     };
  71. }
  72. macro_rules! impl_arith {
  73.     ($for:ty, $from:ty) => {
  74.         impl_all!($for, $from);
  75.         impl $for {
  76.             pub fn min(&self, other: $from) -> $for {
  77.                 let other_as_self = Self::from(other);
  78.                 if self.0 < other_as_self.0 {
  79.                     *self
  80.                 } else {
  81.                     other_as_self
  82.                 }
  83.             }
  84.             pub fn max(&self, other: $from) -> $for {
  85.                 let other_as_lamports = Self::from(other);
  86.                 if self.0 > other_as_lamports.0 {
  87.                     *self
  88.                 } else {
  89.                     other_as_lamports
  90.                 }
  91.             }
  92.             pub fn pow(&self, other: $from) -> $for {
  93.                 Self::from(Decimal::from(self.0).powd(Decimal::from(Self::from(other).0)))
  94.             }
  95.             pub fn log(&self, other: $from) -> $for {
  96.                 Self::from(
  97.                     Decimal::from(self.0).log10() / Decimal::from(Self::from(other).0).log10(),
  98.                 )
  99.             }
  100.         }
  101.     };
  102.     ($self:ty) => {
  103.         impl_all!($self, $self);
  104.         impl $self {
  105.             pub fn is_zero(&self) -> bool {
  106.                 Decimal::from(self.0).is_zero()
  107.             }
  108.         }
  109.     };
  110. }
  111.  
  112. impl_arith!(Lamports, Sol);
  113. impl_arith!(Sol, Lamports);
  114. impl_arith!(Lamports);
  115. impl_arith!(Sol);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement