changeset 789:b5bca9274335

utils: Make some math functions const Sadly f32::sqrt() isn’t const, so we can’t make normalize() const nor any function which depends on it.
author Link Mauve <linkmauve@linkmauve.fr>
date Sun, 04 Jan 2026 11:49:03 +0100
parents f56b10812b77
children d005f5927447
files utils/src/math.rs
diffstat 1 files changed, 8 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/utils/src/math.rs
+++ b/utils/src/math.rs
@@ -7,19 +7,19 @@
 
 impl Mat4 {
     /// Create a new matrix from a set of 16 f32.
-    pub fn new(inner: [[f32; 4]; 4]) -> Mat4 {
+    pub const fn new(inner: [[f32; 4]; 4]) -> Mat4 {
         Mat4 {
             inner
         }
     }
 
-    fn zero() -> Mat4 {
+    const fn zero() -> Mat4 {
         Mat4 {
             inner: [[0.; 4]; 4]
         }
     }
 
-    fn identity() -> Mat4 {
+    const fn identity() -> Mat4 {
         Mat4 {
             inner: [[1., 0., 0., 0.],
                     [0., 1., 0., 0.],
@@ -29,7 +29,7 @@
     }
 
     /// Immutably borrow the array of f32 inside this matrix.
-    pub fn borrow_inner(&self) -> &[[f32; 4]; 4] {
+    pub const fn borrow_inner(&self) -> &[[f32; 4]; 4] {
         &self.inner
     }
 
@@ -129,7 +129,7 @@
 }
 
 /// Create an orthographic projection matrix.
-pub fn ortho_2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat4 {
+pub const fn ortho_2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat4 {
     let mut mat = Mat4::identity();
     mat.inner[0][0] = 2. / (right - left);
     mat.inner[1][1] = 2. / (top - bottom);
@@ -180,7 +180,7 @@
                [-dot(s, eye), -dot(u, eye), dot(f, eye), 1.]])
 }
 
-fn sub(a: Vec3, b: Vec3) -> Vec3 {
+const fn sub(a: Vec3, b: Vec3) -> Vec3 {
     [a[0] - b[0],
      a[1] - b[1],
      a[2] - b[2]]
@@ -191,12 +191,12 @@
     [vec[0] * normal, vec[1] * normal, vec[2] * normal]
 }
 
-fn cross(a: Vec3, b: Vec3) -> Vec3 {
+const fn cross(a: Vec3, b: Vec3) -> Vec3 {
     [a[1] * b[2] - b[1] * a[2],
      a[2] * b[0] - b[2] * a[0],
      a[0] * b[1] - b[0] * a[1]]
 }
 
-fn dot(a: Vec3, b: Vec3) -> f32 {
+const fn dot(a: Vec3, b: Vec3) -> f32 {
     a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
 }