Declare the rest of the constructors to be const

This requires removing trait bounds from the constructors, and for Euler, removing the trait bound from the struct.
This commit is contained in:
Nathan Stoddard 2019-01-13 22:41:15 -08:00
parent 19f75b88e6
commit c438536dac
3 changed files with 23 additions and 15 deletions

View file

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

View file

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

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 /// Construct a new quaternion from one scalar component and three
/// imaginary components. /// imaginary components.
#[inline] #[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)) Quaternion::from_sv(w, Vector3::new(xi, yj, zk))
} }
/// Construct a new quaternion from a scalar and a vector. /// Construct a new quaternion from a scalar and a vector.
#[inline] #[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 } Quaternion { s: s, v: v }
} }
}
impl<S: BaseFloat> Quaternion<S> {
/// Construct a new quaternion as a closest arc between two vectors /// Construct a new quaternion as a closest arc between two vectors
/// ///
/// Return the closest rotation that turns `src` vector into `dst`. /// Return the closest rotation that turns `src` vector into `dst`.