Implement ToStr for angle types

This commit is contained in:
Brendan Zabarauskas 2012-11-29 16:31:14 +10:00
parent eeaf6cb3c1
commit 9b05325a31
2 changed files with 13 additions and 0 deletions

View file

@ -1,5 +1,6 @@
use core::cmp::{Eq, Ord}; use core::cmp::{Eq, Ord};
use core::f64::consts::pi; use core::f64::consts::pi;
use funs::triganomic::{cos, sin}; use funs::triganomic::{cos, sin};
use mat::{Mat3, Mat4}; use mat::{Mat3, Mat4};
use num::cast::{NumCast, cast}; use num::cast::{NumCast, cast};
@ -100,6 +101,10 @@ pub impl<T:Copy Ord> Radians<T>: Ord {
#[inline(always)] pure fn gt(&self, other: &Radians<T>) -> bool { **self > **other } #[inline(always)] pure fn gt(&self, other: &Radians<T>) -> bool { **self > **other }
} }
pub impl<T> Radians<T>: ToStr {
pure fn to_str() -> ~str { fmt!("%? rad", *self) }
}
@ -174,6 +179,10 @@ pub impl<T:Copy Ord> Degrees<T>: Ord {
#[inline(always)] pure fn gt(&self, other: &Degrees<T>) -> bool { **self > **other } #[inline(always)] pure fn gt(&self, other: &Degrees<T>) -> bool { **self > **other }
} }
pub impl<T> Degrees<T>: ToStr {
pure fn to_str() -> ~str { fmt!("%?\xB0", *self) }
}

View file

@ -31,6 +31,8 @@ fn test_radians() {
assert *(Radians(pi) / 2.0) == *Radians(pi / 2.0); assert *(Radians(pi) / 2.0) == *Radians(pi / 2.0);
assert *(Radians(3.0 * pi) % (2.0 * pi)) == *Radians(pi); assert *(Radians(3.0 * pi) % (2.0 * pi)) == *Radians(pi);
assert *(-Radians(pi)) == *Radians(-pi); assert *(-Radians(pi)) == *Radians(-pi);
assert fmt!("%s", Radians(1).to_str()) == ~"1 rad";
} }
#[test] #[test]
@ -60,6 +62,8 @@ fn test_degrees() {
assert (Degrees(180.0) / 2.0) == Degrees(90.0); assert (Degrees(180.0) / 2.0) == Degrees(90.0);
assert (Degrees(540.0) % (360.0)) == Degrees(180.0); assert (Degrees(540.0) % (360.0)) == Degrees(180.0);
assert (-Degrees(180.0)) == Degrees(-180.0); assert (-Degrees(180.0)) == Degrees(-180.0);
assert fmt!("%s", Degrees(180.0).to_str()) == ~"180°";
} }