From 6ecbf2dbfd0638a17b4d1483f521e0b18e6b51bd Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 6 Feb 2015 00:16:22 -0500 Subject: [PATCH] Implemented short vector constructors --- src/cgmath.rs | 2 +- src/vector.rs | 18 ++++++++++++------ tests/vector.rs | 7 +++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/cgmath.rs b/src/cgmath.rs index 06d14bd..351eb13 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -43,7 +43,7 @@ pub use matrix::{ToMatrix2, ToMatrix3, ToMatrix4}; pub use quaternion::{Quaternion, ToQuaternion}; pub use vector::{Vector, EuclideanVector}; pub use vector::{Vector2, Vector3, Vector4}; -pub use vector::dot; +pub use vector::{dot, vec2, vec3, vec4}; pub use angle::{rad, deg}; pub use angle::{Angle, Rad, Deg}; diff --git a/src/vector.rs b/src/vector.rs index f04ffc4..29a43d8 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -22,10 +22,10 @@ //! vector are also provided: //! //! ```rust -//! use cgmath::{Vector2, Vector3, Vector4, one, zero}; +//! use cgmath::{Vector2, Vector3, Vector4, one, zero, vec3}; //! //! assert_eq!(Vector2::new(1.0f64, 0.0f64), Vector2::unit_x()); -//! assert_eq!(Vector3::new(0.0f64, 0.0f64, 0.0f64), zero()); +//! assert_eq!(vec3(0.0f64, 0.0f64, 0.0f64), zero()); //! assert_eq!(Vector4::from_value(1.0f64), one()); //! ``` //! @@ -177,7 +177,7 @@ pub trait Vector: Array1 + Zero + One + Neg { // Utility macro for generating associated functions for the vectors macro_rules! vec( - ($Self:ident <$S:ident> { $($field:ident),+ }, $n:expr) => ( + ($Self:ident <$S:ident> { $($field:ident),+ }, $n:expr, $constructor:ident) => ( #[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable, Rand)] pub struct $Self { $(pub $field: S),+ } @@ -189,6 +189,12 @@ macro_rules! vec( } } + /// The short constructor. + #[inline] + pub fn $constructor($($field: S),+) -> $Self { + $Self::new($($field),+) + } + impl<$S: Copy> $Self<$S> { /// Construct a vector from a single value, replicating it. #[inline] @@ -369,9 +375,9 @@ macro_rules! fold { ($method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { $x.$method($y).$method($z).$method($w) }; } -vec!(Vector2 { x, y }, 2); -vec!(Vector3 { x, y, z }, 3); -vec!(Vector4 { x, y, z, w }, 4); +vec!(Vector2 { x, y }, 2, vec2); +vec!(Vector3 { x, y, z }, 3, vec3); +vec!(Vector4 { x, y, z, w }, 4, vec4); /// Operations specific to numeric two-dimensional vectors. impl Vector2 { diff --git a/tests/vector.rs b/tests/vector.rs index b7f64a5..9c5a36c 100644 --- a/tests/vector.rs +++ b/tests/vector.rs @@ -20,6 +20,13 @@ use cgmath::*; use std::f64; use std::num::Float; +#[test] +fn test_constructor() { + assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32)); + assert_eq!(vec3(1f64, 2f64, 3f64), Vector3::new(1f64, 2f64, 3f64)); + assert_eq!(vec4(1is, 2is, 3is, 4is), Vector4::new(1is, 2is, 3is, 4is)); +} + #[test] fn test_from_value() { assert_eq!(Vector2::from_value(102i), Vector2::new(102i, 102i));