Merge pull request #292 from bjz/debug-impls

Improve and add Debug impls
This commit is contained in:
Brendan Zabarauskas 2015-12-29 22:07:22 +11:00
commit 6cf7831275
9 changed files with 63 additions and 49 deletions

View file

@ -6,6 +6,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
### Added
- Implements `fmt::Debug` for `Basis2`, `Basis3`, and `AffineMatrix3`
### Changed
- Improves the `fmt::Debug` impls for `Vector`, `Matrix`, `Point`, `Decomposed`,
`Quaternion` and `Angle` to make them easier to derive, and have clearer
formatting.
## [v0.7.0] - 2015-12-23 ## [v0.7.0] - 2015-12-23
### Added ### Added

View file

@ -218,7 +218,7 @@ macro_rules! impl_angle {
} }
} }
impl<S: BaseFloat> fmt::Debug for $Angle<S> { impl<S: fmt::Debug> fmt::Debug for $Angle<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, $fmt, self.s) write!(f, $fmt, self.s)
} }

View file

@ -1111,30 +1111,24 @@ impl<S: BaseFloat> From<Matrix3<S>> for Quaternion<S> {
} }
} }
impl<S: BaseFloat> fmt::Debug for Matrix2<S> { impl<S: fmt::Debug> fmt::Debug for Matrix2<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[[{:?}, {:?}], [{:?}, {:?}]]", try!(write!(f, "Matrix2 "));
self[0][0], self[0][1], <[[S; 2]; 2] as fmt::Debug>::fmt(self.as_ref(), f)
self[1][0], self[1][1])
} }
} }
impl<S: BaseFloat> fmt::Debug for Matrix3<S> { impl<S: fmt::Debug> fmt::Debug for Matrix3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[[{:?}, {:?}, {:?}], [{:?}, {:?}, {:?}], [{:?}, {:?}, {:?}]]", try!(write!(f, "Matrix3 "));
self[0][0], self[0][1], self[0][2], <[[S; 3]; 3] as fmt::Debug>::fmt(self.as_ref(), f)
self[1][0], self[1][1], self[1][2],
self[2][0], self[2][1], self[2][2])
} }
} }
impl<S: BaseFloat> fmt::Debug for Matrix4<S> { impl<S: fmt::Debug> fmt::Debug for Matrix4<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[[{:?}, {:?}, {:?}, {:?}], [{:?}, {:?}, {:?}, {:?}], [{:?}, {:?}, {:?}, {:?}], [{:?}, {:?}, {:?}, {:?}]]", try!(write!(f, "Matrix4 "));
self[0][0], self[0][1], self[0][2], self[0][3], <[[S; 4]; 4] as fmt::Debug>::fmt(self.as_ref(), f)
self[1][0], self[1][1], self[1][2], self[1][3],
self[2][0], self[2][1], self[2][2], self[2][3],
self[3][0], self[3][1], self[3][2], self[3][3])
} }
} }

View file

@ -189,15 +189,17 @@ impl_fixed_array_conversions!(Point3<S> { x: 0, y: 1, z: 2 }, 3);
impl_tuple_conversions!(Point2<S> { x, y }, (S, S)); impl_tuple_conversions!(Point2<S> { x, y }, (S, S));
impl_tuple_conversions!(Point3<S> { x, y, z }, (S, S, S)); impl_tuple_conversions!(Point3<S> { x, y, z }, (S, S, S));
impl<S: BaseNum> fmt::Debug for Point2<S> { impl<S: fmt::Debug> fmt::Debug for Point2<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}, {:?}]", self.x, self.y) try!(write!(f, "Point2 "));
<[S; 2] as fmt::Debug>::fmt(self.as_ref(), f)
} }
} }
impl<S: BaseNum> fmt::Debug for Point3<S> { impl<S: fmt::Debug> fmt::Debug for Point3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}, {:?}, {:?}]", self.x, self.y, self.z) try!(write!(f, "Point3 "));
<[S; 3] as fmt::Debug>::fmt(self.as_ref(), f)
} }
} }

View file

@ -64,7 +64,7 @@ pub fn ortho<S: BaseFloat>(left: S, right: S, bottom: S, top: S, near: S, far: S
} }
/// A perspective projection based on a vertical field-of-view angle. /// A perspective projection based on a vertical field-of-view angle.
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct PerspectiveFov<S> { pub struct PerspectiveFov<S> {
pub fovy: Rad<S>, pub fovy: Rad<S>,
pub aspect: S, pub aspect: S,
@ -130,7 +130,7 @@ impl<S: BaseFloat> From<PerspectiveFov<S>> for Matrix4<S> {
} }
/// A perspective projection with arbitrary left/right/bottom/top distances /// A perspective projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Perspective<S> { pub struct Perspective<S> {
pub left: S, pub left: S,
pub right: S, pub right: S,
@ -176,7 +176,7 @@ impl<S: BaseFloat> From<Perspective<S>> for Matrix4<S> {
} }
/// An orthographic projection with arbitrary left/right/bottom/top distances /// An orthographic projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Ortho<S> { pub struct Ortho<S> {
pub left: S, pub left: S,
pub right: S, pub right: S,

View file

@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::fmt;
use std::mem; use std::mem;
use std::ops::*; use std::ops::*;
@ -32,7 +31,7 @@ use vector::{Vector3, Vector, EuclideanVector};
/// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector
/// form. /// form.
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Quaternion<S> { pub struct Quaternion<S> {
pub s: S, pub s: S,
pub v: Vector3<S>, pub v: Vector3<S>,
@ -312,16 +311,6 @@ impl<S: BaseFloat> From<Quaternion<S>> for Matrix4<S> {
} }
} }
impl<S: BaseFloat> fmt::Debug for Quaternion<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} + {:?}i + {:?}j + {:?}k",
self.s,
self.v.x,
self.v.y,
self.v.z)
}
}
// Quaternion Rotation impls // Quaternion Rotation impls
impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> { impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {

View file

@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::fmt;
use angle::{Angle, Rad}; use angle::{Angle, Rad};
use approx::ApproxEq; use approx::ApproxEq;
use matrix::SquareMatrix; use matrix::SquareMatrix;
@ -222,6 +224,13 @@ impl<S: BaseFloat> Rotation2<S> for Basis2<S> {
fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } } fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } }
} }
impl<S: fmt::Debug> fmt::Debug for Basis2<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Basis2 "));
<[[S; 2]; 2] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}
/// A three-dimensional rotation matrix. /// A three-dimensional rotation matrix.
/// ///
/// The matrix is guaranteed to be orthogonal, so some operations, specifically /// The matrix is guaranteed to be orthogonal, so some operations, specifically
@ -323,3 +332,10 @@ impl<S: BaseFloat> Rotation3<S> for Basis3<S> {
Basis3 { mat: Matrix3::from_angle_z(theta) } Basis3 { mat: Matrix3::from_angle_z(theta) }
} }
} }
impl<S: fmt::Debug> fmt::Debug for Basis3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Basis3 "));
<[[S; 3]; 3] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}

View file

@ -71,7 +71,7 @@ pub trait Transform<P: Point>: Sized {
/// A generic transformation consisting of a rotation, /// A generic transformation consisting of a rotation,
/// displacement vector and scale amount. /// displacement vector and scale amount.
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Decomposed<V: Vector, R> { pub struct Decomposed<V: Vector, R> {
pub scale: V::Scalar, pub scale: V::Scalar,
pub rot: R, pub rot: R,
@ -163,13 +163,6 @@ impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R>
impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {} impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}
impl<S: BaseFloat, R: fmt::Debug + Rotation3<S>> fmt::Debug for Decomposed<Vector3<S>, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(scale({:?}), rot({:?}), disp{:?})",
self.scale, self.rot, self.disp)
}
}
/// A homogeneous transformation matrix. /// A homogeneous transformation matrix.
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct AffineMatrix3<S> { pub struct AffineMatrix3<S> {
@ -213,3 +206,10 @@ impl<S: BaseNum> From<AffineMatrix3<S>> for Matrix4<S> {
} }
impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {} impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {}
impl<S: fmt::Debug> fmt::Debug for AffineMatrix3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "AffineMatrix3 "));
<[[S; 4]; 4] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}

View file

@ -489,21 +489,24 @@ impl<S: BaseFloat> EuclideanVector for Vector4<S> {
} }
} }
impl<S: BaseNum> fmt::Debug for Vector2<S> { impl<S: fmt::Debug> fmt::Debug for Vector2<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}, {:?}]", self.x, self.y) try!(write!(f, "Vector2 "));
<[S; 2] as fmt::Debug>::fmt(self.as_ref(), f)
} }
} }
impl<S: BaseNum> fmt::Debug for Vector3<S> { impl<S: fmt::Debug> fmt::Debug for Vector3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}, {:?}, {:?}]", self.x, self.y, self.z) try!(write!(f, "Vector3 "));
<[S; 3] as fmt::Debug>::fmt(self.as_ref(), f)
} }
} }
impl<S: BaseNum> fmt::Debug for Vector4<S> { impl<S: fmt::Debug> fmt::Debug for Vector4<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}, {:?}, {:?}, {:?}]", self.x, self.y, self.z, self.w) try!(write!(f, "Vector4 "));
<[S; 4] as fmt::Debug>::fmt(self.as_ref(), f)
} }
} }