Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! derive_ops {
- (impl $ops: ident for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
- impl $ops for $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: $name) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- impl<'a> $ops<$name> for &'a $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: $name) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- };
- (impl $ops: ident<Self> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
- impl<'a> $ops<&'a $name> for $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: &'a $name) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- impl<'a, 'b> $ops<&'a $name> for &'b $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: &'a $name) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- // recursive
- derive_ops!{ impl $ops for $name[$($field)*] { fn $method -> Self } }
- };
- (impl $ops: ident<$rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
- impl $ops<$rhs> for $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: $rhs) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- impl<'a> $ops<$rhs> for &'a $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: $rhs) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- impl<'a> $ops<&'a $rhs> for $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: &'a $rhs) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- impl<'a, 'b> $ops<&'a $rhs> for &'b $name {
- type Output = $name;
- #[inline]
- fn $method(self, rhs: &'a $rhs) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- };
- (impl $ops: ident<Self, $rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident -> Self }) => {
- // recursive
- derive_ops!{ impl $ops<Self> for $name[$($field)*] { fn $method -> Self } }
- derive_ops!{ impl $ops<$rhs> for $name[$($field)*] { fn $method -> Self } }
- };
- (impl $ops: ident for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
- impl<T> $ops for $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: $name<T>) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- impl<'a, T> $ops<$name<T>> for &'a $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: $name<T>) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- };
- (impl $ops: ident<Self> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
- impl<'a, T> $ops<&'a $name<T>> for $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: &'a $name<T>) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- impl<'a, 'b, T> $ops<&'a $name<T>> for &'b $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: &'a $name<T>) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs.$field),)*
- }
- }
- }
- // recursive
- derive_ops!{ impl $ops for $name<T>[$($field)*] { fn $method -> Self } }
- };
- (impl $ops: ident<T> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
- impl<T> $ops<T> for $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: T) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- impl<'a, T> $ops<T> for &'a $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: T) -> Self::Output {
- $name {
- $($field: self.$field.$method(rhs),)*
- }
- }
- }
- impl<'a, T> $ops<&'a T> for $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: &'a T) -> Self::Output {
- $name {
- $($field: self.$field.$method(*rhs),)*
- }
- }
- }
- impl<'a, 'b, T> $ops<&'a T> for &'b $name<T>
- where
- T: Copy + $ops<Output = T>,
- {
- type Output = $name<T>;
- #[inline]
- fn $method(self, rhs: &'a T) -> Self::Output {
- $name {
- $($field: self.$field.$method(*rhs),)*
- }
- }
- }
- };
- (impl $ops: ident<Self, T> for $name: ident<T>[$($field: ident)*] { fn $method: ident -> Self }) => {
- // recursive
- derive_ops!{ impl $ops<Self> for $name<T>[$($field)*] { fn $method -> Self } }
- derive_ops!{ impl $ops<T> for $name<T>[$($field)*] { fn $method -> Self } }
- };
- (impl $ops: ident<Self> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
- impl $ops for $name {
- #[inline]
- fn $method(&mut self, rhs: $name) {
- $(self.$field.$method(rhs.$field);)*
- }
- }
- impl<'a> $ops<&'a $name> for $name {
- #[inline]
- fn $method(&mut self, rhs: &'a $name) {
- $(self.$field.$method(rhs.$field);)*
- }
- }
- };
- (impl $ops: ident<$rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
- impl $ops<$rhs> for $name {
- #[inline]
- fn $method(&mut self, rhs: $rhs) {
- $(self.$field.$method(rhs);)*
- }
- }
- impl<'a> $ops<&'a $rhs> for $name {
- #[inline]
- fn $method(&mut self, rhs: &'a $rhs) {
- $(self.$field.$method(*rhs);)*
- }
- }
- };
- (impl $ops: ident<Self, $rhs: ty> for $name: ident [$($field: ident)*] { fn $method: ident }) => {
- // recursive
- derive_ops!{ impl $ops<Self> for $name [$($field)*] { fn $method } }
- derive_ops!{ impl $ops<$rhs> for $name [$($field)*] { fn $method } }
- };
- (impl $ops: ident<Self> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
- impl<T> $ops for $name<T>
- where
- T: Copy + $ops,
- {
- #[inline]
- fn $method(&mut self, rhs: $name<T>) {
- $(self.$field.$method(rhs.$field);)*
- }
- }
- impl<'a, T> $ops<&'a $name<T>> for $name<T>
- where
- T: Copy + $ops,
- {
- #[inline]
- fn $method(&mut self, rhs: &'a $name<T>) {
- $(self.$field.$method(rhs.$field);)*
- }
- }
- };
- (impl $ops: ident<T> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
- impl<T> $ops<T> for $name<T>
- where
- T: Copy + $ops,
- {
- #[inline]
- fn $method(&mut self, rhs: T) {
- $(self.$field.$method(rhs);)*
- }
- }
- impl<'a, T> $ops<&'a T> for $name<T>
- where
- T: Copy + $ops,
- {
- #[inline]
- fn $method(&mut self, rhs: &'a T) {
- $(self.$field.$method(*rhs);)*
- }
- }
- };
- (impl $ops: ident<Self, T> for $name: ident<T>[$($field: ident)*] { fn $method: ident }) => {
- // recursive
- derive_ops!{ impl $ops<Self> for $name<T>[$($field)*] { fn $method } }
- derive_ops!{ impl $ops<T> for $name<T>[$($field)*] { fn $method } }
- };
- }
- #[derive(Debug)]
- struct Vec2<T> {
- x: T,
- y: T,
- }
- use std::ops::*;
- derive_ops! { impl Add<Self, T> for Vec2<T>[x y] { fn add -> Self } }
- derive_ops! { impl Div<Self, T> for Vec2<T>[x y] { fn div -> Self } }
- derive_ops! { impl Mul<Self, T> for Vec2<T>[x y] { fn mul -> Self } }
- derive_ops! { impl Sub<Self, T> for Vec2<T>[x y] { fn sub -> Self } }
- derive_ops! { impl Rem<Self, T> for Vec2<T>[x y] { fn rem -> Self } }
- derive_ops! { impl AddAssign<Self, T> for Vec2<T>[x y] { fn add_assign } }
- derive_ops! { impl DivAssign<Self, T> for Vec2<T>[x y] { fn div_assign } }
- derive_ops! { impl MulAssign<Self, T> for Vec2<T>[x y] { fn mul_assign } }
- derive_ops! { impl SubAssign<Self, T> for Vec2<T>[x y] { fn sub_assign } }
- derive_ops! { impl RemAssign<Self, T> for Vec2<T>[x y] { fn rem_assign } }
- fn main() {
- let mut vec2 = Vec2 { x: 15.0, y: 10.0 };
- vec2 += 43.54;
- vec2 += Vec2 { x: 15.0, y: 10.0 };
- println!("{:?}", vec2);
- }
Add Comment
Please, Sign In to add comment