Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::*;
- macro_rules! impl_op {
- (impl $trait:ident for $type:ident {
- fn $fn:ident;
- }) => {
- impl $trait for $type {
- type Output = $type;
- fn $fn(self, other: $type) -> $type {
- $type((self.0).$fn(other.0))
- }
- }
- }
- }
- macro_rules! impl_op_assign {
- (impl $trait:ident for $type:ident {
- fn $fn:ident;
- }) => {
- impl $trait for $type {
- fn $fn(&mut self, other: $type) {
- (self.0).$fn(other.0)
- }
- }
- }
- }
- #[derive(Debug, Copy, Clone)]
- struct X(i32);
- impl_op! {
- impl Add for X {
- fn add;
- }
- }
- impl_op_assign! {
- impl AddAssign for X {
- fn add_assign;
- }
- }
- fn main() {
- let mut x1 = X(10);
- let x2 = X(5);
- dbg!(x1 + x2);
- x1 += x2;
- dbg!(x1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement