Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! impl_all {
- ($for:ty, $from:ty) => {
- impl Add<$from> for $for {
- type Output = $for;
- fn add(self, other: $from) -> $for {
- Self(self.0 + Self::from(other).0)
- }
- }
- impl Sub<$from> for $for {
- type Output = $for;
- fn sub(self, other: $from) -> $for {
- Self(self.0 - Self::from(other).0)
- }
- }
- impl Mul<$from> for $for {
- type Output = $for;
- fn mul(self, other: $from) -> $for {
- Self(self.0 * Self::from(other).0)
- }
- }
- impl Div<$from> for $for {
- type Output = $for;
- fn div(self, other: $from) -> $for {
- Self(self.0 / Self::from(other).0)
- }
- }
- impl AddAssign<$from> for $for {
- fn add_assign(&mut self, other: $from) {
- self.0 += Self::from(other).0;
- }
- }
- impl SubAssign<$from> for $for {
- fn sub_assign(&mut self, other: $from) {
- self.0 -= Self::from(other).0;
- }
- }
- impl MulAssign<$from> for $for {
- fn mul_assign(&mut self, other: $from) {
- self.0 *= Self::from(other).0;
- }
- }
- impl DivAssign<$from> for $for {
- fn div_assign(&mut self, other: $from) {
- self.0 /= Self::from(other).0;
- }
- }
- impl PartialEq<$from> for $for {
- fn eq(&self, other: &$from) -> bool {
- self.0 == Self::from(*other).0
- }
- }
- impl PartialOrd<$from> for $for {
- fn partial_cmp(&self, other: &$from) -> Option<std::cmp::Ordering> {
- self.0.partial_cmp(&Self::from(*other).0)
- }
- }
- };
- }
- macro_rules! impl_arith {
- ($for:ty, $from:ty) => {
- impl_all!($for, $from);
- impl $for {
- pub fn min(&self, other: $from) -> $for {
- let other_as_self = Self::from(other);
- if self.0 < other_as_self.0 {
- *self
- } else {
- other_as_self
- }
- }
- pub fn max(&self, other: $from) -> $for {
- let other_as_lamports = Self::from(other);
- if self.0 > other_as_lamports.0 {
- *self
- } else {
- other_as_lamports
- }
- }
- pub fn pow(&self, other: $from) -> $for {
- Self::from(Decimal::from(self.0).powd(Decimal::from(Self::from(other).0)))
- }
- pub fn log(&self, other: $from) -> $for {
- Self::from(
- Decimal::from(self.0).log10() / Decimal::from(Self::from(other).0).log10(),
- )
- }
- }
- };
- ($self:ty) => {
- impl_all!($self, $self);
- impl $self {
- pub fn is_zero(&self) -> bool {
- Decimal::from(self.0).is_zero()
- }
- }
- };
- }
- impl_arith!(Lamports, Sol);
- impl_arith!(Sol, Lamports);
- impl_arith!(Lamports);
- impl_arith!(Sol);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement