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)]
#[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

@ -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`.