daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::ops::{Add, Mul, Div, Sub};
  2.  
  3. fn main() {
  4.     let a = &(&Vec3 { x: 10.0, y: 20.0, z: 30.0 } + 5.0) * 2.0;
  5.     assert_eq!(a, Vec3 { x: 30.0, y: 50.0, z: 70.0 });
  6. }
  7.  
  8. #[derive(Debug, PartialEq)]
  9. struct Vec3 {
  10.     x: f32,
  11.     y: f32,
  12.     z: f32,
  13. }
  14.  
  15. macro_rules! impl_for_ref {
  16.     ( $trait:ty, $other:ty, $method:ident, $symb:tt ) => {
  17.         impl $trait for &Vec3 {
  18.             type Output = Vec3;
  19.  
  20.             fn $method(self, other: $other) -> Vec3 {
  21.                 Vec3 {
  22.                     x: self.x $symb other,
  23.                     y: self.y $symb other,
  24.                     z: self.z $symb other,
  25.                 }
  26.             }
  27.         }
  28.     };
  29. }
  30.  
  31. impl_for_ref!(Add<f32>, f32, add, +);
  32. impl_for_ref!(Mul<f32>, f32, mul, *);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top