Merge pull request #425 from Osspial/master

Implement num_traits::Bounded for Points, Vectors, and Angles
This commit is contained in:
Brendan Zabarauskas 2017-08-12 12:57:36 +10:00 committed by GitHub
commit 7823f46b68
4 changed files with 40 additions and 4 deletions

View file

@ -22,7 +22,7 @@ use std::ops::*;
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use rand::distributions::range::SampleRange; use rand::distributions::range::SampleRange;
use num_traits::cast; use num_traits::{cast, Bounded};
use structure::*; use structure::*;
@ -117,6 +117,18 @@ macro_rules! impl_angle {
fn neg(self) -> $Angle<S> { $Angle(-self.0) } fn neg(self) -> $Angle<S> { $Angle(-self.0) }
} }
impl<S: Bounded> Bounded for $Angle<S> {
#[inline]
fn min_value() -> $Angle<S> {
$Angle(S::min_value())
}
#[inline]
fn max_value() -> $Angle<S> {
$Angle(S::max_value())
}
}
impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> { impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
fn add(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 + rhs.0) } fn add(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 + rhs.0) }
}); });

View file

@ -17,7 +17,7 @@
//! disinguishes them from vectors, which have a length and direction, but do //! disinguishes them from vectors, which have a length and direction, but do
//! not have a fixed position. //! not have a fixed position.
use num_traits::NumCast; use num_traits::{NumCast, Bounded};
use std::fmt; use std::fmt;
use std::mem; use std::mem;
use std::ops::*; use std::ops::*;
@ -195,6 +195,18 @@ macro_rules! impl_point {
} }
} }
impl<S: Bounded> Bounded for $PointN<S> {
#[inline]
fn min_value() -> $PointN<S> {
$PointN { $($field: S::min_value()),+ }
}
#[inline]
fn max_value() -> $PointN<S> {
$PointN { $($field: S::max_value()),+ }
}
}
impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $PointN<S> { impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $PointN<S> {
fn add(lhs, rhs) -> $PointN<S> { $PointN::new($(lhs.$field + rhs.$field),+) } fn add(lhs, rhs) -> $PointN<S> { $PointN::new($(lhs.$field + rhs.$field),+) }
}); });

View file

@ -25,7 +25,7 @@ use approx::ApproxEq;
use angle::Rad; use angle::Rad;
use num::{BaseNum, BaseFloat}; use num::{BaseNum, BaseFloat};
pub use num_traits::{One, Zero}; pub use num_traits::{One, Zero, Bounded};
/// An array containing elements of type `Element` /// An array containing elements of type `Element`
pub trait Array where pub trait Array where

View file

@ -14,7 +14,7 @@
// limitations under the License. // limitations under the License.
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num_traits::NumCast; use num_traits::{NumCast, Bounded};
use std::fmt; use std::fmt;
use std::iter; use std::iter;
use std::mem; use std::mem;
@ -223,6 +223,18 @@ macro_rules! impl_vector {
} }
} }
impl<S: Bounded> Bounded for $VectorN<S> {
#[inline]
fn min_value() -> $VectorN<S> {
$VectorN { $($field: S::min_value()),+ }
}
#[inline]
fn max_value() -> $VectorN<S> {
$VectorN { $($field: S::max_value()),+ }
}
}
impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $VectorN<S> { impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $VectorN<S> {
fn add(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field + rhs.$field),+) } fn add(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field + rhs.$field),+) }
}); });