Guest User

Untitled

a guest
Apr 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. macro_rules! derive_ops {
  2. (impl $ops: ident for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
  3. impl $ops for $name {
  4. type Output = $name;
  5. #[inline]
  6. fn $method(self, rhs: $name) -> Self::Output {
  7. $name {
  8. $($field: self.$field.$method(rhs.$field),)*
  9. }
  10. }
  11. }
  12.  
  13. impl<'a> $ops<$name> for &'a $name {
  14. type Output = $name;
  15. #[inline]
  16. fn $method(self, rhs: $name) -> Self::Output {
  17. $name {
  18. $($field: self.$field.$method(rhs.$field),)*
  19. }
  20. }
  21. }
  22. };
  23. (impl $ops: ident<Self> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
  24. impl<'a> $ops<&'a $name> for $name {
  25. type Output = $name;
  26. #[inline]
  27. fn $method(self, rhs: &'a $name) -> Self::Output {
  28. $name {
  29. $($field: self.$field.$method(rhs.$field),)*
  30. }
  31. }
  32. }
  33.  
  34. impl<'a, 'b> $ops<&'a $name> for &'b $name {
  35. type Output = $name;
  36. #[inline]
  37. fn $method(self, rhs: &'a $name) -> Self::Output {
  38. $name {
  39. $($field: self.$field.$method(rhs.$field),)*
  40. }
  41. }
  42. }
  43.  
  44. // recursive
  45. derive_ops!{ impl $ops for $name[$($field)*] { fn $method -> Self } }
  46. };
  47. (impl $ops: ident<$rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
  48. impl $ops<$rhs> for $name {
  49. type Output = $name;
  50. #[inline]
  51. fn $method(self, rhs: $rhs) -> Self::Output {
  52. $name {
  53. $($field: self.$field.$method(rhs),)*
  54. }
  55. }
  56. }
  57.  
  58. impl<'a> $ops<$rhs> for &'a $name {
  59. type Output = $name;
  60. #[inline]
  61. fn $method(self, rhs: $rhs) -> Self::Output {
  62. $name {
  63. $($field: self.$field.$method(rhs),)*
  64. }
  65. }
  66. }
  67.  
  68. impl<'a> $ops<&'a $rhs> for $name {
  69. type Output = $name;
  70. #[inline]
  71. fn $method(self, rhs: &'a $rhs) -> Self::Output {
  72. $name {
  73. $($field: self.$field.$method(rhs),)*
  74. }
  75. }
  76. }
  77.  
  78. impl<'a, 'b> $ops<&'a $rhs> for &'b $name {
  79. type Output = $name;
  80. #[inline]
  81. fn $method(self, rhs: &'a $rhs) -> Self::Output {
  82. $name {
  83. $($field: self.$field.$method(rhs),)*
  84. }
  85. }
  86. }
  87. };
  88. (impl $ops: ident<Self, $rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
  89. // recursive
  90. derive_ops!{ impl $ops<Self> for $name[$($field)*] { fn $method -> Self } }
  91. derive_ops!{ impl $ops<$rhs> for $name[$($field)*] { fn $method -> Self } }
  92. };
  93.  
  94. (impl $ops: ident for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
  95. impl<T> $ops for $name<T>
  96. where
  97. T: Copy + $ops<Output = T>,
  98. {
  99. type Output = $name<T>;
  100. #[inline]
  101. fn $method(self, rhs: $name<T>) -> Self::Output {
  102. $name {
  103. $($field: self.$field.$method(rhs.$field),)*
  104. }
  105. }
  106. }
  107.  
  108. impl<'a, T> $ops<$name<T>> for &'a $name<T>
  109. where
  110. T: Copy + $ops<Output = T>,
  111. {
  112. type Output = $name<T>;
  113. #[inline]
  114. fn $method(self, rhs: $name<T>) -> Self::Output {
  115. $name {
  116. $($field: self.$field.$method(rhs.$field),)*
  117. }
  118. }
  119. }
  120. };
  121. (impl $ops: ident<Self> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
  122. impl<'a, T> $ops<&'a $name<T>> for $name<T>
  123. where
  124. T: Copy + $ops<Output = T>,
  125. {
  126. type Output = $name<T>;
  127. #[inline]
  128. fn $method(self, rhs: &'a $name<T>) -> Self::Output {
  129. $name {
  130. $($field: self.$field.$method(rhs.$field),)*
  131. }
  132. }
  133. }
  134.  
  135. impl<'a, 'b, T> $ops<&'a $name<T>> for &'b $name<T>
  136. where
  137. T: Copy + $ops<Output = T>,
  138. {
  139. type Output = $name<T>;
  140. #[inline]
  141. fn $method(self, rhs: &'a $name<T>) -> Self::Output {
  142. $name {
  143. $($field: self.$field.$method(rhs.$field),)*
  144. }
  145. }
  146. }
  147.  
  148. // recursive
  149. derive_ops!{ impl $ops for $name<T>[$($field)*] { fn $method -> Self } }
  150. };
  151. (impl $ops: ident<T> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
  152. impl<T> $ops<T> for $name<T>
  153. where
  154. T: Copy + $ops<Output = T>,
  155. {
  156. type Output = $name<T>;
  157. #[inline]
  158. fn $method(self, rhs: T) -> Self::Output {
  159. $name {
  160. $($field: self.$field.$method(rhs),)*
  161. }
  162. }
  163. }
  164.  
  165. impl<'a, T> $ops<T> for &'a $name<T>
  166. where
  167. T: Copy + $ops<Output = T>,
  168. {
  169. type Output = $name<T>;
  170. #[inline]
  171. fn $method(self, rhs: T) -> Self::Output {
  172. $name {
  173. $($field: self.$field.$method(rhs),)*
  174. }
  175. }
  176. }
  177.  
  178. impl<'a, T> $ops<&'a T> for $name<T>
  179. where
  180. T: Copy + $ops<Output = T>,
  181. {
  182. type Output = $name<T>;
  183. #[inline]
  184. fn $method(self, rhs: &'a T) -> Self::Output {
  185. $name {
  186. $($field: self.$field.$method(*rhs),)*
  187. }
  188. }
  189. }
  190.  
  191. impl<'a, 'b, T> $ops<&'a T> for &'b $name<T>
  192. where
  193. T: Copy + $ops<Output = T>,
  194. {
  195. type Output = $name<T>;
  196. #[inline]
  197. fn $method(self, rhs: &'a T) -> Self::Output {
  198. $name {
  199. $($field: self.$field.$method(*rhs),)*
  200. }
  201. }
  202. }
  203. };
  204. (impl $ops: ident<Self, T> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
  205. // recursive
  206. derive_ops!{ impl $ops<Self> for $name<T>[$($field)*] { fn $method -> Self } }
  207. derive_ops!{ impl $ops<T> for $name<T>[$($field)*] { fn $method -> Self } }
  208. };
  209.  
  210. (impl $ops: ident<Self> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
  211. impl $ops for $name {
  212. #[inline]
  213. fn $method(&mut self, rhs: $name) {
  214. $(self.$field.$method(rhs.$field);)*
  215. }
  216. }
  217.  
  218. impl<'a> $ops<&'a $name> for $name {
  219. #[inline]
  220. fn $method(&mut self, rhs: &'a $name) {
  221. $(self.$field.$method(rhs.$field);)*
  222. }
  223. }
  224. };
  225. (impl $ops: ident<$rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
  226. impl $ops<$rhs> for $name {
  227. #[inline]
  228. fn $method(&mut self, rhs: $rhs) {
  229. $(self.$field.$method(rhs);)*
  230. }
  231. }
  232.  
  233. impl<'a> $ops<&'a $rhs> for $name {
  234. #[inline]
  235. fn $method(&mut self, rhs: &'a $rhs) {
  236. $(self.$field.$method(*rhs);)*
  237. }
  238. }
  239. };
  240. (impl $ops: ident<Self, $rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
  241. // recursive
  242. derive_ops!{ impl $ops<Self> for $name [$($field)*] { fn $method } }
  243. derive_ops!{ impl $ops<$rhs> for $name [$($field)*] { fn $method } }
  244. };
  245.  
  246. (impl $ops: ident<Self> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
  247. impl<T> $ops for $name<T>
  248. where
  249. T: Copy + $ops,
  250. {
  251. #[inline]
  252. fn $method(&mut self, rhs: $name<T>) {
  253. $(self.$field.$method(rhs.$field);)*
  254. }
  255. }
  256.  
  257. impl<'a, T> $ops<&'a $name<T>> for $name<T>
  258. where
  259. T: Copy + $ops,
  260. {
  261. #[inline]
  262. fn $method(&mut self, rhs: &'a $name<T>) {
  263. $(self.$field.$method(rhs.$field);)*
  264. }
  265. }
  266. };
  267. (impl $ops: ident<T> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
  268. impl<T> $ops<T> for $name<T>
  269. where
  270. T: Copy + $ops,
  271. {
  272. #[inline]
  273. fn $method(&mut self, rhs: T) {
  274. $(self.$field.$method(rhs);)*
  275. }
  276. }
  277.  
  278. impl<'a, T> $ops<&'a T> for $name<T>
  279. where
  280. T: Copy + $ops,
  281. {
  282. #[inline]
  283. fn $method(&mut self, rhs: &'a T) {
  284. $(self.$field.$method(*rhs);)*
  285. }
  286. }
  287. };
  288. (impl $ops: ident<Self, T> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
  289. // recursive
  290. derive_ops!{ impl $ops<Self> for $name<T>[$($field)*] { fn $method } }
  291. derive_ops!{ impl $ops<T> for $name<T>[$($field)*] { fn $method } }
  292. };
  293. }
  294.  
  295. #[derive(Debug)]
  296. struct Vec2<T> {
  297. x: T,
  298. y: T,
  299. }
  300.  
  301. use std::ops::*;
  302.  
  303. derive_ops! { impl Add<Self, T> for Vec2<T>[x y] { fn add -> Self } }
  304. derive_ops! { impl Div<Self, T> for Vec2<T>[x y] { fn div -> Self } }
  305. derive_ops! { impl Mul<Self, T> for Vec2<T>[x y] { fn mul -> Self } }
  306. derive_ops! { impl Sub<Self, T> for Vec2<T>[x y] { fn sub -> Self } }
  307. derive_ops! { impl Rem<Self, T> for Vec2<T>[x y] { fn rem -> Self } }
  308.  
  309. derive_ops! { impl AddAssign<Self, T> for Vec2<T>[x y] { fn add_assign } }
  310. derive_ops! { impl DivAssign<Self, T> for Vec2<T>[x y] { fn div_assign } }
  311. derive_ops! { impl MulAssign<Self, T> for Vec2<T>[x y] { fn mul_assign } }
  312. derive_ops! { impl SubAssign<Self, T> for Vec2<T>[x y] { fn sub_assign } }
  313. derive_ops! { impl RemAssign<Self, T> for Vec2<T>[x y] { fn rem_assign } }
  314.  
  315. fn main() {
  316. let mut vec2 = Vec2 { x: 15.0, y: 10.0 };
  317.  
  318. vec2 += 43.54;
  319. vec2 += Vec2 { x: 15.0, y: 10.0 };
  320.  
  321. println!("{:?}", vec2);
  322. }
Add Comment
Please, Sign In to add comment