475: Declare vector constructors to be const r=kvark a=nstoddard

This makes it easier to create vectors in constants.

Co-authored-by: Nathan Stoddard <nstoddard@users.noreply.github.com>
This commit is contained in:
bors[bot] 2019-01-14 16:48:32 +00:00
commit 649fef941b
5 changed files with 27 additions and 19 deletions

View file

@ -78,7 +78,7 @@ use num::BaseFloat;
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Euler<A: Angle> {
pub struct Euler<A> {
/// The angle to apply around the _x_ axis. Also known at the _pitch_.
pub x: A,
/// The angle to apply around the _y_ axis. Also known at the _yaw_.
@ -87,7 +87,7 @@ pub struct Euler<A: Angle> {
pub z: A,
}
impl<A: Angle> Euler<A> {
impl<A> Euler<A> {
/// Construct a set of euler angles.
///
/// # Arguments
@ -95,7 +95,7 @@ impl<A: Angle> Euler<A> {
/// * `x` - The angle to apply around the _x_ axis. Also known at the _pitch_.
/// * `y` - The angle to apply around the _y_ axis. Also known at the _yaw_.
/// * `z` - The angle to apply around the _z_ axis. Also known at the _roll_.
pub fn new(x: A, y: A, z: A) -> Euler<A> {
pub const fn new(x: A, y: A, z: A) -> Euler<A> {
Euler { x: x, y: y, z: z }
}
}

View file

@ -81,19 +81,21 @@ pub struct Matrix4<S> {
pub w: Vector4<S>,
}
impl<S: BaseFloat> Matrix2<S> {
impl<S> Matrix2<S> {
/// Create a new matrix, providing values for each index.
#[inline]
pub fn new(c0r0: S, c0r1: S, c1r0: S, c1r1: S) -> Matrix2<S> {
pub const fn new(c0r0: S, c0r1: S, c1r0: S, c1r1: S) -> Matrix2<S> {
Matrix2::from_cols(Vector2::new(c0r0, c0r1), Vector2::new(c1r0, c1r1))
}
/// Create a new matrix, providing columns.
#[inline]
pub fn from_cols(c0: Vector2<S>, c1: Vector2<S>) -> Matrix2<S> {
pub const fn from_cols(c0: Vector2<S>, c1: Vector2<S>) -> Matrix2<S> {
Matrix2 { x: c0, y: c1 }
}
}
impl<S: BaseFloat> Matrix2<S> {
/// Create a transformation matrix that will cause a vector to point at
/// `dir`, using `up` for orientation.
pub fn look_at(dir: Vector2<S>, up: Vector2<S>) -> Matrix2<S> {
@ -114,11 +116,11 @@ impl<S: BaseFloat> Matrix2<S> {
}
}
impl<S: BaseFloat> Matrix3<S> {
impl<S> Matrix3<S> {
/// Create a new matrix, providing values for each index.
#[inline]
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn new(
pub const fn new(
c0r0:S, c0r1:S, c0r2:S,
c1r0:S, c1r1:S, c1r2:S,
c2r0:S, c2r1:S, c2r2:S,
@ -132,14 +134,16 @@ impl<S: BaseFloat> Matrix3<S> {
/// Create a new matrix, providing columns.
#[inline]
pub fn from_cols(c0: Vector3<S>, c1: Vector3<S>, c2: Vector3<S>) -> Matrix3<S> {
pub const fn from_cols(c0: Vector3<S>, c1: Vector3<S>, c2: Vector3<S>) -> Matrix3<S> {
Matrix3 {
x: c0,
y: c1,
z: c2,
}
}
}
impl<S: BaseFloat> Matrix3<S> {
/// Create a rotation matrix that will cause a vector to point at
/// `dir`, using `up` for orientation.
pub fn look_at(dir: Vector3<S>, up: Vector3<S>) -> Matrix3<S> {
@ -218,11 +222,11 @@ impl<S: BaseFloat> Matrix3<S> {
}
}
impl<S: BaseFloat> Matrix4<S> {
impl<S> Matrix4<S> {
/// Create a new matrix, providing values for each index.
#[inline]
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn new(
pub const fn new(
c0r0: S, c0r1: S, c0r2: S, c0r3: S,
c1r0: S, c1r1: S, c1r2: S, c1r3: S,
c2r0: S, c2r1: S, c2r2: S, c2r3: S,
@ -238,7 +242,7 @@ impl<S: BaseFloat> Matrix4<S> {
/// Create a new matrix, providing columns.
#[inline]
pub fn from_cols(c0: Vector4<S>, c1: Vector4<S>, c2: Vector4<S>, c3: Vector4<S>) -> Matrix4<S> {
pub const fn from_cols(c0: Vector4<S>, c1: Vector4<S>, c2: Vector4<S>, c3: Vector4<S>) -> Matrix4<S> {
Matrix4 {
x: c0,
y: c1,
@ -246,7 +250,9 @@ impl<S: BaseFloat> Matrix4<S> {
w: c3,
}
}
}
impl<S: BaseFloat> Matrix4<S> {
/// Create a homogeneous transformation matrix from a translation vector.
#[inline]
pub fn from_translation(v: Vector3<S>) -> Matrix4<S> {

View file

@ -82,7 +82,7 @@ macro_rules! impl_point {
impl<S> $PointN<S> {
/// Construct a new point, using the provided values.
#[inline]
pub fn new($($field: S),+) -> $PointN<S> {
pub const fn new($($field: S),+) -> $PointN<S> {
$PointN { $($field: $field),+ }
}

View file

@ -76,20 +76,22 @@ impl Into<Simdf32x4> for Quaternion<f32> {
}
}
impl<S: BaseFloat> Quaternion<S> {
impl<S> Quaternion<S> {
/// Construct a new quaternion from one scalar component and three
/// imaginary components.
#[inline]
pub fn new(w: S, xi: S, yj: S, zk: S) -> Quaternion<S> {
pub const fn new(w: S, xi: S, yj: S, zk: S) -> Quaternion<S> {
Quaternion::from_sv(w, Vector3::new(xi, yj, zk))
}
/// Construct a new quaternion from a scalar and a vector.
#[inline]
pub fn from_sv(s: S, v: Vector3<S>) -> Quaternion<S> {
pub const fn from_sv(s: S, v: Vector3<S>) -> Quaternion<S> {
Quaternion { s: s, v: v }
}
}
impl<S: BaseFloat> Quaternion<S> {
/// Construct a new quaternion as a closest arc between two vectors
///
/// Return the closest rotation that turns `src` vector into `dst`.

View file

@ -99,7 +99,7 @@ macro_rules! impl_vector {
impl<S> $VectorN<S> {
/// Construct a new vector, using the provided values.
#[inline]
pub fn new($($field: S),+) -> $VectorN<S> {
pub const fn new($($field: S),+) -> $VectorN<S> {
$VectorN { $($field: $field),+ }
}
@ -115,7 +115,7 @@ macro_rules! impl_vector {
/// The short constructor.
#[inline]
pub fn $constructor<S>($($field: S),+) -> $VectorN<S> {
pub const fn $constructor<S>($($field: S),+) -> $VectorN<S> {
$VectorN::new($($field),+)
}
@ -358,7 +358,7 @@ macro_rules! impl_vector_default {
impl<S> $VectorN<S> {
/// Construct a new vector, using the provided values.
#[inline]
pub fn new($($field: S),+) -> $VectorN<S> {
pub const fn new($($field: S),+) -> $VectorN<S> {
$VectorN { $($field: $field),+ }
}
}