Skip to content

Instantly share code, notes, and snippets.

@rubber-duck
Created June 6, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubber-duck/5722958 to your computer and use it in GitHub Desktop.
Save rubber-duck/5722958 to your computer and use it in GitHub Desktop.
#[deriving(ToStr)]
struct Vec3f {
x: f32,
y: f32,
z: f32
}
macro_rules! impl_vec_op(
($tr: ident, $op: ident, $vt: ident, $($members: ident)+) => (
impl $tr<$vt, $vt> for $vt {
fn $op(&self, other: &$vt) -> $vt {
$vt { $( $members: self.$members.$op(&other.$members) ),+ }
}
}
);
)
macro_rules! impl_vec_ops(
($vt: ident, $($members: ident)+) => (
impl_vec_op!(Sub, sub, $vt, $( $members )+)
impl_vec_op!(Add, add, $vt, $( $members )+)
);
)
impl_vec_ops!(Vec3f, x y z)
fn main() {
let a = Vec3f {x: 0.0, y: 1.0, z: 0.0};
let b = Vec3f {x: 0.0, y: 0.0, z: 1.0};
println((a + b).to_str());
println((a - b).to_str());
}
main.rs:30:13: 30:18 error: binary operation + cannot be applied to type `Vec3f`
main.rs:30 println((a + b).to_str());
^~~~~
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment