Added the ability for vectors to use assignment operators.

This added the AddAssign, SubAssign, MulAssign, DivAssign, and RemAssign.
This needs to be looked at a little further to see if the other functions
need to removed now.
This commit is contained in:
Jason Smith
2016-05-12 15:49:24 -04:00
parent a618fb27f7
commit 0b7a338572
3 changed files with 318 additions and 4 deletions

View File

@ -2,6 +2,7 @@ use std::cmp::PartialEq;
use std::mem::size_of;
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Rem};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use super::bounded::Bounded;
@ -10,7 +11,9 @@ use super::bounded::Bounded;
/// a number.
pub trait Number : Zero + One + Add<Output=Self> + Sub<Output=Self> +
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
PartialEq + Copy + Clone
AddAssign<Self> + SubAssign<Self> + MulAssign<Self> +
DivAssign<Self> + RemAssign<Self> + PartialEq +
Copy + Clone
{
type StrRadixError;

View File

@ -3,6 +3,7 @@
//! [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use super::number::Number;
use super::real::Real;
@ -414,7 +415,7 @@ macro_rules! perform_method_on_components
};
}
/// A macro that will define a binary operation
/// A macro that will define a binary operation for
/// a Vector and its components.
macro_rules! binary_operator_impl
{
@ -461,7 +462,7 @@ macro_rules! binary_operator_impl
}
}
impl<'a, T> $traitName<$structName<T>> for $structName<T>
impl<T> $traitName<$structName<T>> for $structName<T>
where T: Number
{
type Output = $structName<T>;
@ -483,7 +484,7 @@ macro_rules! binary_operator_impl
}
}
impl<'a, 'b, T> $traitName<&'a $structName<T>> for $structName<T>
impl<'a, T> $traitName<&'a $structName<T>> for $structName<T>
where T: Number
{
type Output = $structName<T>;
@ -507,6 +508,48 @@ macro_rules! binary_operator_impl
}
}
/// A macro that will define a binary operation for
/// a Vector and its components.
macro_rules! binary_operator_assign_impl
{
($traitName: ident :: $funcName: ident,
$structName: ident {$($field: ident),+}) =>
{
impl<T> $traitName<$structName<T>> for $structName<T> where T: Number
{
fn $funcName(&mut self, rhs: $structName<T>)
{
$(self.$field.$funcName(rhs.$field);)+
}
}
impl<T> $traitName<T> for $structName<T> where T: Number
{
fn $funcName(&mut self, rhs: T)
{
$(self.$field.$funcName(rhs);)+
}
}
impl<'a, T> $traitName<&'a $structName<T>> for $structName<T>
where T: Number
{
fn $funcName(&mut self, rhs: &'a $structName<T>)
{
$(self.$field.$funcName(rhs.$field);)+
}
}
impl<'a, T> $traitName<&'a T> for $structName<T> where T: Number
{
fn $funcName(&mut self, rhs: &'a T)
{
$(self.$field.$funcName(*rhs);)+
}
}
}
}
/// A macro that defines a Vector structure
/// that implements the Vector trait.
macro_rules! define_vector
@ -660,6 +703,18 @@ macro_rules! define_vector
binary_operator_impl!(Mul::mul, $structName {$($field),+});
binary_operator_impl!(Div::div, $structName {$($field),+});
binary_operator_impl!(Rem::rem, $structName {$($field),+});
// Handle the assignment operators.
binary_operator_assign_impl!(AddAssign::add_assign,
$structName {$($field),+});
binary_operator_assign_impl!(SubAssign::sub_assign,
$structName {$($field),+});
binary_operator_assign_impl!(MulAssign::mul_assign,
$structName {$($field),+});
binary_operator_assign_impl!(DivAssign::div_assign,
$structName {$($field),+});
binary_operator_assign_impl!(RemAssign::rem_assign,
$structName {$($field),+});
}
}