Merge pull request #179 from bfops/self

Add #[must_use] on functions with in-place variants.
This commit is contained in:
Brendan Zabarauskas 2015-02-23 08:58:44 +11:00
commit 0c74d2a551
3 changed files with 28 additions and 0 deletions

View file

@ -294,22 +294,28 @@ pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
+ ApproxEq<S>
+ Sized {
/// Multiply this matrix by a scalar, returning the new matrix.
#[must_use]
fn mul_s(&self, s: S) -> Self;
/// Divide this matrix by a scalar, returning the new matrix.
#[must_use]
fn div_s(&self, s: S) -> Self;
/// Take the remainder of this matrix by a scalar, returning the new
/// matrix.
#[must_use]
fn rem_s(&self, s: S) -> Self;
/// Add this matrix with another matrix, returning the new metrix.
#[must_use]
fn add_m(&self, m: &Self) -> Self;
/// Subtract another matrix from this matrix, returning the new matrix.
#[must_use]
fn sub_m(&self, m: &Self) -> Self;
/// Multiplay a vector by this matrix, returning a new vector.
fn mul_v(&self, v: &V) -> V;
/// Multiply this matrix by another matrix, returning the new matrix.
#[must_use]
fn mul_m(&self, m: &Self) -> Self;
/// Negate this matrix in-place (multiply by scalar -1).
@ -332,6 +338,7 @@ pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
fn mul_self_m(&mut self, m: &Self) { *self = self.mul_m(m); }
/// Transpose this matrix, returning a new matrix.
#[must_use]
fn transpose(&self) -> Self;
/// Transpose this matrix in-place.
fn transpose_self(&mut self);
@ -348,6 +355,7 @@ pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
/// Invert this matrix, returning a new matrix. `m.mul_m(m.invert())` is
/// the identity matrix. Returns `None` if this matrix is not invertible
/// (has a determinant of zero).
#[must_use]
fn invert(&self) -> Option<Self>;
/// Invert this matrix in-place.

View file

@ -73,13 +73,17 @@ pub trait Point<S: BaseNum, V: Vector<S>>: Array1<S> + Clone {
fn to_vec(&self) -> V;
/// Multiply each component by a scalar, returning the new point.
#[must_use]
fn mul_s(&self, s: S) -> Self;
/// Divide each component by a scalar, returning the new point.
#[must_use]
fn div_s(&self, s: S) -> Self;
/// Subtract a scalar from each component, returning the new point.
#[must_use]
fn rem_s(&self, s: S) -> Self;
/// Add a vector to this point, returning the new point.
#[must_use]
fn add_v(&self, v: &V) -> Self;
/// Subtract another point from this one, returning a new vector.
fn sub_p(&self, p: &Self) -> V;
@ -97,8 +101,10 @@ pub trait Point<S: BaseNum, V: Vector<S>>: Array1<S> + Clone {
/// This is a weird one, but its useful for plane calculations.
fn dot(&self, v: &V) -> S;
#[must_use]
fn min(&self, p: &Self) -> Self;
#[must_use]
fn max(&self, p: &Self) -> Self;
}

View file

@ -111,25 +111,35 @@ use num::{BaseNum, BaseFloat, Zero, One, zero, one};
/// for pragmatic reasons.
pub trait Vector<S: BaseNum>: Array1<S> + Zero + One + Neg<Output=Self> {
/// Add a scalar to this vector, returning a new vector.
#[must_use]
fn add_s(&self, s: S) -> Self;
/// Subtract a scalar from this vector, returning a new vector.
#[must_use]
fn sub_s(&self, s: S) -> Self;
/// Multiply this vector by a scalar, returning a new vector.
#[must_use]
fn mul_s(&self, s: S) -> Self;
/// Divide this vector by a scalar, returning a new vector.
#[must_use]
fn div_s(&self, s: S) -> Self;
/// Take the remainder of this vector by a scalar, returning a new vector.
#[must_use]
fn rem_s(&self, s: S) -> Self;
/// Add this vector to another, returning a new vector.
#[must_use]
fn add_v(&self, v: &Self) -> Self;
/// Subtract another vector from this one, returning a new vector.
#[must_use]
fn sub_v(&self, v: &Self) -> Self;
/// Multiply this vector by another, returning a new vector.
#[must_use]
fn mul_v(&self, v: &Self) -> Self;
/// Divide this vector by another, returning a new vector.
#[must_use]
fn div_v(&self, v: &Self) -> Self;
/// Take the remainder of this vector by another, returning a new scalar.
#[must_use]
fn rem_v(&self, v: &Self) -> Self;
/// Negate this vector in-place.
@ -410,6 +420,7 @@ impl<S: BaseNum> Vector3<S> {
/// Returns the cross product of the vector and `other`.
#[inline]
#[must_use]
pub fn cross(&self, other: &Vector3<S>) -> Vector3<S> {
Vector3::new((self.y * other.z) - (self.z * other.y),
(self.z * other.x) - (self.x * other.z),
@ -498,12 +509,14 @@ pub trait EuclideanVector<S: BaseFloat>: Vector<S>
/// Returns a vector with the same direction, but with a `length` (or
/// `norm`) of `1`.
#[inline]
#[must_use]
fn normalize(&self) -> Self {
self.normalize_to(one::<S>())
}
/// Returns a vector with the same direction and a given `length`.
#[inline]
#[must_use]
fn normalize_to(&self, length: S) -> Self {
self.mul_s(length / self.length())
}
@ -511,6 +524,7 @@ pub trait EuclideanVector<S: BaseFloat>: Vector<S>
/// Returns the result of linarly interpolating the length of the vector
/// towards the length of `other` by the specified amount.
#[inline]
#[must_use]
fn lerp(&self, other: &Self, amount: S) -> Self {
self.add_v(&other.sub_v(self).mul_s(amount))
}