# HG changeset patch # User Link Mauve # Date 1768655528 -3600 # Node ID d005f592744700cc6151c673777f3e62ba21ff5e # Parent b5bca9274335b1021ef1f51163f0c535d6e088de utils: Use const_for to make more const fn diff --git a/utils/Cargo.toml b/utils/Cargo.toml --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -10,4 +10,5 @@ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +const_for = "0.1.5" getrandom = "0.3.4" diff --git a/utils/src/math.rs b/utils/src/math.rs --- a/utils/src/math.rs +++ b/utils/src/math.rs @@ -1,5 +1,7 @@ //! Various helpers to deal with vectors and matrices. +use const_for::const_for; + /// A 4×4 f32 matrix type. pub struct Mat4 { inner: [[f32; 4]; 4] @@ -34,18 +36,18 @@ } /// Scale the matrix in 2D. - pub fn scale2d(&mut self, x: f32, y: f32) { - for i in 0..4 { + pub const fn scale2d(&mut self, x: f32, y: f32) { + const_for!(i in 0..4 => { self.inner[0][i] *= x; self.inner[1][i] *= y; - } + }); } /// Flip the matrix. - pub fn flip(&mut self) { - for i in 0..4 { + pub const fn flip(&mut self) { + const_for!(i in 0..4 => { self.inner[0][i] = -self.inner[0][i]; - } + }); } /// Rotate the matrix around its x angle (in radians). @@ -53,14 +55,14 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[1][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[1][i] = cos_a * lines[i] - sin_a * lines[4+i]; self.inner[2][i] = sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Rotate the matrix around its y angle (in radians). @@ -68,14 +70,14 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[2][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[0][i] = cos_a * lines[i] + sin_a * lines[4+i]; self.inner[2][i] = -sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Rotate the matrix around its z angle (in radians). @@ -83,31 +85,31 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[1][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[0][i] = cos_a * lines[i] - sin_a * lines[4+i]; self.inner[1][i] = sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Translate the matrix by a 3D offset. - pub fn translate(&mut self, offset: [f32; 3]) { + pub const fn translate(&mut self, offset: [f32; 3]) { let mut item: [f32; 3] = [0.; 3]; - for i in 0..3 { + const_for!(i in 0..3 => { item[i] = self.inner[3][i] * offset[i]; - } - for i in 0..3 { - for j in 0..4 { + }); + const_for!(i in 0..3 => { + const_for!(j in 0..4 => { self.inner[i][j] += item[i]; - } - } + }); + }); } /// Translate the matrix by a 2D offset. - pub fn translate_2d(&mut self, x: f32, y: f32) { + pub const fn translate_2d(&mut self, x: f32, y: f32) { let offset = [x, y, 0.]; self.translate(offset); } @@ -117,13 +119,13 @@ type Output = Mat4; fn mul(self, rhs: Mat4) -> Mat4 { let mut tmp = Mat4::zero(); - for i in 0..4 { - for j in 0..4 { - for k in 0..4 { + const_for!(i in 0..4 => { + const_for!(j in 0..4 => { + const_for!(k in 0..4 => { tmp.inner[i][j] += self.inner[i][k] * rhs.inner[k][j]; - } - } - } + }); + }); + }); tmp } }