From bedf48dbc24df2ca4fa3e7aac299cf272fd89180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Mon, 12 Apr 2021 09:18:17 +0200 Subject: [PATCH] Mark small matrix/vector "new" constractors as const-fn. --- src/base/construction.rs | 272 +++++++++++++++++++++------------------ src/base/matrix.rs | 12 +- 2 files changed, 154 insertions(+), 130 deletions(-) diff --git a/src/base/construction.rs b/src/base/construction.rs index ef9d7c0f..e3e56b53 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -22,7 +22,9 @@ use simba::scalar::{ClosedAdd, ClosedMul}; use crate::base::allocator::Allocator; use crate::base::dimension::{Dim, DimName, Dynamic, ToTypenum}; use crate::base::storage::Storage; -use crate::base::{Const, DefaultAllocator, Matrix, OMatrix, OVector, Scalar, Unit, Vector}; +use crate::base::{ + ArrayStorage, Const, DefaultAllocator, Matrix, OMatrix, OVector, Scalar, Unit, Vector, +}; /// When "no_unsound_assume_init" is enabled, expands to `unimplemented!()` instead of `new_uninitialized_generic().assume_init()`. /// Intended as a placeholder, each callsite should be refactored to use uninitialized memory soundly @@ -877,22 +879,44 @@ where * Constructors for small matrices and vectors. * */ + +macro_rules! transpose_array( + [$($a: ident),*;] => { + [$([$a]),*] + }; + [$($a: ident),*; $($b: ident),*;] => { + [$([$a, $b]),*]; + }; + [$($a: ident),*; $($b: ident),*; $($c: ident),*;] => { + [$([$a, $b, $c]),*]; + }; + [$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*;] => { + [$([$a, $b, $c, $d]),*]; + }; + [$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*; $($e: ident),*;] => { + [$([$a, $b, $c, $d, $e]),*]; + }; + [$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*; $($e: ident),*; $($f: ident),*;] => { + [$([$a, $b, $c, $d, $e, $f]),*]; + }; +); + macro_rules! componentwise_constructors_impl( - ($($R: expr, $C: expr, $($args: ident:($irow: expr,$icol: expr)),*);* $(;)*) => {$( - impl OMatrix, Const<$C>> - where T: Scalar, - DefaultAllocator: Allocator, Const<$C>> { + ($($R: expr, $C: expr, [$($($args: ident),*);*] $(;)*)*) => {$( + impl Matrix, Const<$C>, ArrayStorage> { /// Initializes this matrix from its components. #[inline] - pub fn new($($args: T),*) -> Self { + pub const fn new($($($args: T),*),*) -> Self { unsafe { - #[cfg(feature="no_unsound_assume_init")] - let mut res: Self = unimplemented!(); - #[cfg(not(feature="no_unsound_assume_init"))] - let mut res = Self::new_uninitialized().assume_init(); - $( *res.get_unchecked_mut(($irow, $icol)) = $args; )* - - res + Self::from_data_statically_unchecked( + ArrayStorage( + transpose_array![ + $( + $($args),* + ;)* + ] + ) + ) } } } @@ -903,145 +927,145 @@ componentwise_constructors_impl!( /* * Square matrices 1 .. 6. */ - 2, 2, m11:(0,0), m12:(0,1), - m21:(1,0), m22:(1,1); - 3, 3, m11:(0,0), m12:(0,1), m13:(0,2), - m21:(1,0), m22:(1,1), m23:(1,2), - m31:(2,0), m32:(2,1), m33:(2,2); - 4, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3); - 5, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4); - 6, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5), - m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4), m66:(5,5); + 2, 2, [m11, m12; + m21, m22]; + 3, 3, [m11, m12, m13; + m21, m22, m23; + m31, m32, m33]; + 4, 4, [m11, m12, m13, m14; + m21, m22, m23, m24; + m31, m32, m33, m34; + m41, m42, m43, m44]; + 5, 5, [m11, m12, m13, m14, m15; + m21, m22, m23, m24, m25; + m31, m32, m33, m34, m35; + m41, m42, m43, m44, m45; + m51, m52, m53, m54, m55]; + 6, 6, [m11, m12, m13, m14, m15, m16; + m21, m22, m23, m24, m25, m26; + m31, m32, m33, m34, m35, m36; + m41, m42, m43, m44, m45, m46; + m51, m52, m53, m54, m55, m56; + m61, m62, m63, m64, m65, m66]; /* * Rectangular matrices with 2 rows. */ - 2, 3, m11:(0,0), m12:(0,1), m13:(0,2), - m21:(1,0), m22:(1,1), m23:(1,2); - 2, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3); - 2, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4); - 2, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5); + 2, 3, [m11, m12, m13; + m21, m22, m23]; + 2, 4, [m11, m12, m13, m14; + m21, m22, m23, m24]; + 2, 5, [m11, m12, m13, m14, m15; + m21, m22, m23, m24, m25]; + 2, 6, [m11, m12, m13, m14, m15, m16; + m21, m22, m23, m24, m25, m26]; /* * Rectangular matrices with 3 rows. */ - 3, 2, m11:(0,0), m12:(0,1), - m21:(1,0), m22:(1,1), - m31:(2,0), m32:(2,1); - 3, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3); - 3, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4); - 3, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5); + 3, 2, [m11, m12; + m21, m22; + m31, m32]; + 3, 4, [m11, m12, m13, m14; + m21, m22, m23, m24; + m31, m32, m33, m34]; + 3, 5, [m11, m12, m13, m14, m15; + m21, m22, m23, m24, m25; + m31, m32, m33, m34, m35]; + 3, 6, [m11, m12, m13, m14, m15, m16; + m21, m22, m23, m24, m25, m26; + m31, m32, m33, m34, m35, m36]; /* * Rectangular matrices with 4 rows. */ - 4, 2, m11:(0,0), m12:(0,1), - m21:(1,0), m22:(1,1), - m31:(2,0), m32:(2,1), - m41:(3,0), m42:(3,1); - 4, 3, m11:(0,0), m12:(0,1), m13:(0,2), - m21:(1,0), m22:(1,1), m23:(1,2), - m31:(2,0), m32:(2,1), m33:(2,2), - m41:(3,0), m42:(3,1), m43:(3,2); - 4, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4); - 4, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5); + 4, 2, [m11, m12; + m21, m22; + m31, m32; + m41, m42]; + 4, 3, [m11, m12, m13; + m21, m22, m23; + m31, m32, m33; + m41, m42, m43]; + 4, 5, [m11, m12, m13, m14, m15; + m21, m22, m23, m24, m25; + m31, m32, m33, m34, m35; + m41, m42, m43, m44, m45]; + 4, 6, [m11, m12, m13, m14, m15, m16; + m21, m22, m23, m24, m25, m26; + m31, m32, m33, m34, m35, m36; + m41, m42, m43, m44, m45, m46]; /* * Rectangular matrices with 5 rows. */ - 5, 2, m11:(0,0), m12:(0,1), - m21:(1,0), m22:(1,1), - m31:(2,0), m32:(2,1), - m41:(3,0), m42:(3,1), - m51:(4,0), m52:(4,1); - 5, 3, m11:(0,0), m12:(0,1), m13:(0,2), - m21:(1,0), m22:(1,1), m23:(1,2), - m31:(2,0), m32:(2,1), m33:(2,2), - m41:(3,0), m42:(3,1), m43:(3,2), - m51:(4,0), m52:(4,1), m53:(4,2); - 5, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3); - 5, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5); + 5, 2, [m11, m12; + m21, m22; + m31, m32; + m41, m42; + m51, m52]; + 5, 3, [m11, m12, m13; + m21, m22, m23; + m31, m32, m33; + m41, m42, m43; + m51, m52, m53]; + 5, 4, [m11, m12, m13, m14; + m21, m22, m23, m24; + m31, m32, m33, m34; + m41, m42, m43, m44; + m51, m52, m53, m54]; + 5, 6, [m11, m12, m13, m14, m15, m16; + m21, m22, m23, m24, m25, m26; + m31, m32, m33, m34, m35, m36; + m41, m42, m43, m44, m45, m46; + m51, m52, m53, m54, m55, m56]; /* * Rectangular matrices with 6 rows. */ - 6, 2, m11:(0,0), m12:(0,1), - m21:(1,0), m22:(1,1), - m31:(2,0), m32:(2,1), - m41:(3,0), m42:(3,1), - m51:(4,0), m52:(4,1), - m61:(5,0), m62:(5,1); - 6, 3, m11:(0,0), m12:(0,1), m13:(0,2), - m21:(1,0), m22:(1,1), m23:(1,2), - m31:(2,0), m32:(2,1), m33:(2,2), - m41:(3,0), m42:(3,1), m43:(3,2), - m51:(4,0), m52:(4,1), m53:(4,2), - m61:(5,0), m62:(5,1), m63:(5,2); - 6, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), - m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3); - 6, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), - m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), - m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), - m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), - m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), - m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4); + 6, 2, [m11, m12; + m21, m22; + m31, m32; + m41, m42; + m51, m52; + m61, m62]; + 6, 3, [m11, m12, m13; + m21, m22, m23; + m31, m32, m33; + m41, m42, m43; + m51, m52, m53; + m61, m62, m63]; + 6, 4, [m11, m12, m13, m14; + m21, m22, m23, m24; + m31, m32, m33, m34; + m41, m42, m43, m44; + m51, m52, m53, m54; + m61, m62, m63, m64]; + 6, 5, [m11, m12, m13, m14, m15; + m21, m22, m23, m24, m25; + m31, m32, m33, m34, m35; + m41, m42, m43, m44, m45; + m51, m52, m53, m54, m55; + m61, m62, m63, m64, m65]; /* * Row vectors 1 .. 6. */ - 1, 1, x:(0,0); - 1, 2, x:(0,0), y:(0,1); - 1, 3, x:(0,0), y:(0,1), z:(0,2); - 1, 4, x:(0,0), y:(0,1), z:(0,2), w:(0,3); - 1, 5, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4); - 1, 6, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4), b:(0,5); + 1, 1, [x]; + 1, 2, [x, y]; + 1, 3, [x, y, z]; + 1, 4, [x, y, z, w]; + 1, 5, [x, y, z, w, a]; + 1, 6, [x, y, z, w, a, b]; /* * Column vectors 1 .. 6. */ - 2, 1, x:(0,0), y:(1,0); - 3, 1, x:(0,0), y:(1,0), z:(2,0); - 4, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0); - 5, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0); - 6, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0), b:(5,0); + 2, 1, [x; y]; + 3, 1, [x; y; z]; + 4, 1, [x; y; z; w]; + 5, 1, [x; y; z; w; a]; + 6, 1, [x; y; z; w; a; b]; ); /* diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 6d326e58..cbd3bdd9 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -150,7 +150,7 @@ pub type MatrixCross = /// some concrete types for `T` and a compatible data storage type `S`). #[repr(C)] #[derive(Clone, Copy)] -pub struct Matrix { +pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? /// /// Well, if you came here to see how you can access the matrix components, @@ -179,7 +179,7 @@ pub struct Matrix { _phantoms: PhantomData<(T, R, C)>, } -impl fmt::Debug for Matrix { +impl fmt::Debug for Matrix { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { formatter .debug_struct("Matrix") @@ -295,11 +295,11 @@ where { } -impl Matrix { +impl Matrix { /// Creates a new matrix with the given data without statically checking that the matrix /// dimension matches the storage dimension. - #[inline] - pub unsafe fn from_data_statically_unchecked(data: S) -> Matrix { + #[inline(always)] + pub const unsafe fn from_data_statically_unchecked(data: S) -> Matrix { Matrix { data, _phantoms: PhantomData, @@ -309,7 +309,7 @@ impl Matrix { impl> Matrix { /// Creates a new matrix with the given data. - #[inline] + #[inline(always)] pub fn from_data(data: S) -> Self { unsafe { Self::from_data_statically_unchecked(data) } }