2013-09-15 03:11:43 +08:00
|
|
|
#[macro_escape];
|
|
|
|
|
2013-09-22 20:22:17 +08:00
|
|
|
#[doc(hidden)]; // we hide doc to not have to document the $trhs double dispatch trait.
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
// Create the traits needed to do fancy operator oveloading.
|
|
|
|
// This is a meta version of
|
|
|
|
// http://smallcultfollowing.com/babysteps/blog/2012/10/04/refining-traits-slash-impls/
|
|
|
|
//
|
|
|
|
// Hopefully future version of the language will make this useless.
|
|
|
|
macro_rules! double_dispatch_binop_decl_trait(
|
|
|
|
($t: ident, $trhs: ident) => (
|
|
|
|
pub trait $trhs<N, Res> {
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<N>, right: &Self) -> Res;
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Macro driving the `Mul` trait to use the related trait for double redispatch.
|
|
|
|
macro_rules! mul_redispatch_impl(
|
2013-09-15 16:48:18 +08:00
|
|
|
($t: ident, $trhs: ident) => (
|
|
|
|
impl<N, Rhs: $trhs<N, Res>, Res> Mul<Rhs, Res> for $t<N> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul(&self, other: &Rhs) -> Res {
|
|
|
|
$trhs::binop(self, other)
|
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
)
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Macro driving the `Div` trait to use the related trait for double redispatch.
|
|
|
|
macro_rules! div_redispatch_impl(
|
2013-09-15 16:48:18 +08:00
|
|
|
($t: ident, $trhs: ident) => (
|
|
|
|
impl<N, Rhs: $trhs<N, Res>, Res> Div<Rhs, Res> for $t<N> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn div(&self, other: &Rhs) -> Res {
|
|
|
|
$trhs::binop(self, other)
|
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
)
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
// Macro driving the `Add` trait to use the related trait for double redispatch.
|
2013-09-15 03:11:43 +08:00
|
|
|
macro_rules! add_redispatch_impl(
|
2013-09-15 16:48:18 +08:00
|
|
|
($t: ident, $trhs: ident) => (
|
|
|
|
impl<N, Rhs: $trhs<N, Res>, Res> Add<Rhs, Res> for $t<N> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn add(&self, other: &Rhs) -> Res {
|
|
|
|
$trhs::binop(self, other)
|
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
)
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
// Macro driving the `Sub` trait to use the related trait for double redispatch.
|
2013-09-15 03:11:43 +08:00
|
|
|
macro_rules! sub_redispatch_impl(
|
2013-09-15 16:48:18 +08:00
|
|
|
($t: ident, $trhs: ident) => (
|
|
|
|
impl<N, Rhs: $trhs<N, Res>, Res> Sub<Rhs, Res> for $t<N> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub(&self, other: &Rhs) -> Res {
|
|
|
|
$trhs::binop(self, other)
|
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
)
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|