tw_asset_plus/
impl_std.rs1use std::{
2 fmt::Display,
3 ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign},
4};
5
6use cosmwasm_std::Uint128;
7
8use crate::asset::{Asset, AssetInfo};
9
10impl Display for Asset {
11 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12 write!(f, "{} {}", self.info, self.amount)
13 }
14}
15
16impl Display for AssetInfo {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 match self {
19 AssetInfo::Token { contract_addr } => {
20 write!(f, "Token at {}", contract_addr)
21 }
22 AssetInfo::NativeToken { denom } => write!(f, "Native token with denom {}", denom),
23 }
24 }
25}
26
27crate::oprt_impl!(add, Add, add_assign, AddAssign);
28crate::oprt_impl!(sub, Sub, sub_assign, SubAssign);
29crate::oprt_impl!(mul, Mul, mul_assign, MulAssign);
30crate::oprt_impl!(div, Div, div_assign, DivAssign);
31crate::oprt_impl!(rem, Rem, rem_assign, RemAssign);
32
33#[macro_export]
34macro_rules! oprt_impl {
35 ($method: ident, $op: ident, $assign_method: ident, $op_assign: ident) => {
36 impl<T> $op<T> for Asset
37 where
38 T: Into<Uint128>,
39 {
40 type Output = Self;
41
42 #[inline]
43 fn $method(self, rhs: T) -> Self::Output {
44 Asset {
45 amount: self.amount.$method(rhs.into()),
46 ..self
47 }
48 }
49 }
50
51 impl<T> $op<T> for &Asset
52 where
53 T: Into<Uint128>,
54 {
55 type Output = Asset;
56
57 #[inline]
58 fn $method(self, rhs: T) -> Self::Output {
59 Asset {
60 amount: self.amount.$method(rhs.into()),
61 info: self.info.to_owned(),
62 }
63 }
64 }
65
66 impl $op for Asset {
67 type Output = Self;
68
69 #[inline]
70 fn $method(self, rhs: Self) -> Self::Output {
71 assert!(
72 self.info == rhs.info,
73 "Attempt arithmetic operation with different info",
74 );
75 Asset {
76 amount: self.amount.$method(rhs.amount),
77 ..self
78 }
79 }
80 }
81
82 impl $op for &Asset {
83 type Output = Asset;
84
85 #[inline]
86 fn $method(self, rhs: Self) -> Self::Output {
87 assert!(
88 self.info == rhs.info,
89 "Attempt arithmetic operation with different info",
90 );
91 Asset {
92 amount: self.amount.$method(rhs.amount),
93 info: self.info.to_owned(),
94 }
95 }
96 }
97
98 impl $op<&Asset> for Asset {
99 type Output = Asset;
100
101 #[inline]
102 fn $method(self, rhs: &Asset) -> Self::Output {
103 assert!(
104 self.info == rhs.info,
105 "Attempt arithmetic operation with different info",
106 );
107 Asset {
108 amount: self.amount.$method(rhs.amount),
109 ..self
110 }
111 }
112 }
113
114 impl $op<Asset> for &Asset {
115 type Output = Asset;
116
117 #[inline]
118 fn $method(self, rhs: Asset) -> Self::Output {
119 assert!(
120 self.info == rhs.info,
121 "Attempt arithmetic operation with different info",
122 );
123 Asset {
124 amount: self.amount.$method(rhs.amount),
125 ..rhs
126 }
127 }
128 }
129
130 impl<T> $op_assign<T> for Asset
131 where
132 T: Into<Uint128>,
133 {
134 #[inline]
135 fn $assign_method(&mut self, rhs: T) {
136 self.amount.$assign_method(rhs.into())
137 }
138 }
139 };
140}