Skip to content

Commit

Permalink
added must-use to many functions (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbox-irl authored Dec 18, 2023
1 parent 353baca commit 142708e
Show file tree
Hide file tree
Showing 58 changed files with 546 additions and 0 deletions.
4 changes: 4 additions & 0 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,7 @@ impl {{ self_t }} {
{% endif %}

/// Multiplies two {{ nxn }} matrices.
#[must_use]
#[inline]
pub fn mul_mat{{ dim }}(&self, rhs: &Self) -> Self {
{% if self_t == "Mat2" and is_sse2 %}
Expand Down Expand Up @@ -1944,6 +1945,7 @@ impl {{ self_t }} {
}

/// Adds two {{ nxn }} matrices.
#[must_use]
#[inline]
pub fn add_mat{{ dim }}(&self, rhs: &Self) -> Self {
{% if self_t == "Mat2" and is_sse2 %}
Expand All @@ -1962,6 +1964,7 @@ impl {{ self_t }} {
}

/// Subtracts two {{ nxn }} matrices.
#[must_use]
#[inline]
pub fn sub_mat{{ dim }}(&self, rhs: &Self) -> Self {
{% if self_t == "Mat2" and is_sse2 %}
Expand All @@ -1980,6 +1983,7 @@ impl {{ self_t }} {
}

/// Multiplies a {{ nxn }} matrix by a scalar.
#[must_use]
#[inline]
pub fn mul_scalar(&self, rhs: {{ scalar_t }}) -> Self {
{% if self_t == "Mat2" and is_sse2 %}
Expand Down
3 changes: 3 additions & 0 deletions codegen/templates/quat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
#[must_use]
#[inline]
#[doc(alias = "mix")]
pub fn lerp(self, end: Self, s: {{ scalar_t }}) -> Self {
Expand Down Expand Up @@ -714,6 +715,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
#[must_use]
#[inline]
pub fn slerp(self, mut end: Self, s: {{ scalar_t }}) -> Self {
// http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
Expand Down Expand Up @@ -832,6 +834,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
#[must_use]
#[inline]
pub fn mul_quat(self, rhs: Self) -> Self {
glam_assert!(self.is_normalized());
Expand Down
3 changes: 3 additions & 0 deletions codegen/templates/swizzle_traits.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
{% endif %}

{% if dim == 4 %}
#[must_use]
#[inline]
fn xyzw(self) -> Self {
self
}
{% elif dim == 3 %}
#[must_use]
#[inline]
fn xyz(self) -> Self {
self
}
{% elif dim == 2 %}
#[must_use]
#[inline]
fn xy(self) -> Self {
self
Expand Down
27 changes: 27 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ impl {{ self_t }} {
}

/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[must_use]
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
{% if is_sse2 %}
Expand All @@ -529,6 +530,7 @@ impl {{ self_t }} {

{% if dim == 3 %}
/// Computes the cross product of `self` and `rhs`.
#[must_use]
#[inline]
pub fn cross(self, rhs: Self) -> Self {
{% if is_scalar %}
Expand Down Expand Up @@ -572,6 +574,7 @@ impl {{ self_t }} {
/// Returns a vector containing the minimum values for each element of `self` and `rhs`.
///
/// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
#[must_use]
#[inline]
pub fn min(self, rhs: Self) -> Self {
{% if is_scalar %}
Expand All @@ -592,6 +595,7 @@ impl {{ self_t }} {
/// Returns a vector containing the maximum values for each element of `self` and `rhs`.
///
/// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
#[must_use]
#[inline]
pub fn max(self, rhs: Self) -> Self {
{% if is_scalar %}
Expand All @@ -616,6 +620,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
#[must_use]
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self {
glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
Expand Down Expand Up @@ -862,6 +867,7 @@ impl {{ self_t }} {

{% if is_signed %}
/// Returns a vector containing the absolute value of each element of `self`.
#[must_use]
#[inline]
pub fn abs(self) -> Self {
{% if is_scalar %}
Expand Down Expand Up @@ -894,6 +900,7 @@ impl {{ self_t }} {
/// - `1` if the number is positive
/// - `-1` if the number is negative
{%- endif %}
#[must_use]
#[inline]
pub fn signum(self) -> Self {
{% if is_scalar %}
Expand Down Expand Up @@ -925,6 +932,7 @@ impl {{ self_t }} {

{% if is_float %}
/// Returns a vector with signs of `rhs` and the magnitudes of `self`.
#[must_use]
#[inline]
pub fn copysign(self, rhs: Self) -> Self {
{% if is_scalar %}
Expand Down Expand Up @@ -1117,6 +1125,7 @@ impl {{ self_t }} {
/// # Panics
/// This function will panic if any `rhs` element is 0 or the division results in overflow.
{%- endif %}
#[must_use]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
Self::new(
Expand All @@ -1138,6 +1147,7 @@ impl {{ self_t }} {
{%- endif %}
///
/// [Euclidean division]: {{scalar_t}}::rem_euclid
#[must_use]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
Self::new(
Expand Down Expand Up @@ -1299,6 +1309,7 @@ impl {{ self_t }} {

/// Returns a vector containing the nearest integer to a number for each element of `self`.
/// Round half-way cases away from 0.0.
#[must_use]
#[inline]
pub fn round(self) -> Self {
{% if is_scalar %}
Expand All @@ -1318,6 +1329,7 @@ impl {{ self_t }} {

/// Returns a vector containing the largest integer less than or equal to a number for each
/// element of `self`.
#[must_use]
#[inline]
pub fn floor(self) -> Self {
{% if is_scalar %}
Expand All @@ -1337,6 +1349,7 @@ impl {{ self_t }} {

/// Returns a vector containing the smallest integer greater than or equal to a number for
/// each element of `self`.
#[must_use]
#[inline]
pub fn ceil(self) -> Self {
{% if is_scalar %}
Expand All @@ -1356,6 +1369,7 @@ impl {{ self_t }} {

/// Returns a vector containing the integer part each element of `self`. This means numbers are
/// always truncated towards zero.
#[must_use]
#[inline]
pub fn trunc(self) -> Self {
{% if is_scalar %}
Expand All @@ -1377,13 +1391,15 @@ impl {{ self_t }} {
/// self.floor()`.
///
/// Note that this is fast but not precise for large numbers.
#[must_use]
#[inline]
pub fn fract(self) -> Self {
self - self.floor()
}

/// Returns a vector containing `e^self` (the exponential function) for each element of
/// `self`.
#[must_use]
#[inline]
pub fn exp(self) -> Self {
Self::new(
Expand All @@ -1394,6 +1410,7 @@ impl {{ self_t }} {
}

/// Returns a vector containing each element of `self` raised to the power of `n`.
#[must_use]
#[inline]
pub fn powf(self, n: {{ scalar_t }}) -> Self {
Self::new(
Expand All @@ -1404,6 +1421,7 @@ impl {{ self_t }} {
}

/// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
#[must_use]
#[inline]
pub fn recip(self) -> Self {
{% if is_scalar %}
Expand All @@ -1427,6 +1445,7 @@ impl {{ self_t }} {
/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
/// extrapolated.
#[doc(alias = "mix")]
#[must_use]
#[inline]
pub fn lerp(self, rhs: Self, s: {{ scalar_t }}) -> Self {
self + ((rhs - self) * s)
Expand All @@ -1451,6 +1470,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
#[must_use]
#[inline]
pub fn clamp_length(self, min: {{ scalar_t }}, max: {{ scalar_t }}) -> Self {
glam_assert!(min <= max);
Expand All @@ -1465,6 +1485,7 @@ impl {{ self_t }} {
}

/// Returns a vector with a length no more than `max`
#[must_use]
pub fn clamp_length_max(self, max: {{ scalar_t }}) -> Self {
let length_sq = self.length_squared();
if length_sq > max * max {
Expand All @@ -1475,6 +1496,7 @@ impl {{ self_t }} {
}

/// Returns a vector with a length no less than `min`
#[must_use]
pub fn clamp_length_min(self, min: {{ scalar_t }}) -> Self {
let length_sq = self.length_squared();
if length_sq < min * min {
Expand All @@ -1491,6 +1513,7 @@ impl {{ self_t }} {
/// architecture has a dedicated fma CPU instruction. However, this is not always true,
/// and will be heavily dependant on designing algorithms with specific target hardware in
/// mind.
#[must_use]
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> Self {
{% if is_sse2 %}
Expand All @@ -1514,6 +1537,7 @@ impl {{ self_t }} {
/// conjunction with the [`rotate()`][Self::rotate()] method, e.g.
/// `{{ vec2_t }}::from_angle(PI).rotate({{ vec2_t }}::Y)` will create the vector `[-1, 0]`
/// and rotate [`{{ vec2_t }}::Y`] around it returning `-{{ vec2_t }}::Y`.
#[must_use]
#[inline]
pub fn from_angle(angle: {{ scalar_t }}) -> Self {
let (sin, cos) = math::sin_cos(angle);
Expand Down Expand Up @@ -1558,6 +1582,7 @@ impl {{ self_t }} {
///
/// The output vector is not necessarily unit length. For that use
/// [`Self::any_orthonormal_vector()`] instead.
#[must_use]
#[inline]
pub fn any_orthogonal_vector(&self) -> Self {
// This can probably be optimized
Expand All @@ -1575,6 +1600,7 @@ impl {{ self_t }} {
/// # Panics
///
/// Will panic if `self` is not normalized when `glam_assert` is enabled.
#[must_use]
#[inline]
pub fn any_orthonormal_vector(&self) -> Self {
glam_assert!(self.is_normalized());
Expand Down Expand Up @@ -1608,6 +1634,7 @@ impl {{ self_t }} {

{% if is_signed and dim == 2 %}
/// Returns a vector that is equal to `self` rotated by 90 degrees.
#[must_use]
#[inline]
pub fn perp(self) -> Self {
Self {
Expand Down
4 changes: 4 additions & 0 deletions src/f32/coresimd/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl Mat2 {
}

/// Multiplies two 2x2 matrices.
#[must_use]
#[inline]
pub fn mul_mat2(&self, rhs: &Self) -> Self {
let abcd = self.0;
Expand All @@ -253,18 +254,21 @@ impl Mat2 {
}

/// Adds two 2x2 matrices.
#[must_use]
#[inline]
pub fn add_mat2(&self, rhs: &Self) -> Self {
Self(self.0 + rhs.0)
}

/// Subtracts two 2x2 matrices.
#[must_use]
#[inline]
pub fn sub_mat2(&self, rhs: &Self) -> Self {
Self(self.0 - rhs.0)
}

/// Multiplies a 2x2 matrix by a scalar.
#[must_use]
#[inline]
pub fn mul_scalar(&self, rhs: f32) -> Self {
Self(self.0 * f32x4::splat(rhs))
Expand Down
4 changes: 4 additions & 0 deletions src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ impl Mat3A {
}

/// Multiplies two 3x3 matrices.
#[must_use]
#[inline]
pub fn mul_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -496,6 +497,7 @@ impl Mat3A {
}

/// Adds two 3x3 matrices.
#[must_use]
#[inline]
pub fn add_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -506,6 +508,7 @@ impl Mat3A {
}

/// Subtracts two 3x3 matrices.
#[must_use]
#[inline]
pub fn sub_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -516,6 +519,7 @@ impl Mat3A {
}

/// Multiplies a 3x3 matrix by a scalar.
#[must_use]
#[inline]
pub fn mul_scalar(&self, rhs: f32) -> Self {
Self::from_cols(
Expand Down
4 changes: 4 additions & 0 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ impl Mat4 {
}

/// Multiplies two 4x4 matrices.
#[must_use]
#[inline]
pub fn mul_mat4(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -1118,6 +1119,7 @@ impl Mat4 {
}

/// Adds two 4x4 matrices.
#[must_use]
#[inline]
pub fn add_mat4(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -1129,6 +1131,7 @@ impl Mat4 {
}

/// Subtracts two 4x4 matrices.
#[must_use]
#[inline]
pub fn sub_mat4(&self, rhs: &Self) -> Self {
Self::from_cols(
Expand All @@ -1140,6 +1143,7 @@ impl Mat4 {
}

/// Multiplies a 4x4 matrix by a scalar.
#[must_use]
#[inline]
pub fn mul_scalar(&self, rhs: f32) -> Self {
Self::from_cols(
Expand Down
Loading

0 comments on commit 142708e

Please sign in to comment.