From 3a3bc55f667b7a928eea0608e650ac4c6c3f4ffc Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Fri, 7 May 2021 09:00:29 +0200 Subject: [PATCH] Move from_{}_storage impl blocks to matrix.rs --- src/base/array_storage.rs | 12 ----------- src/base/matrix.rs | 43 ++++++++++++++++++++++++++++++++++++++- src/base/vec_storage.rs | 19 ----------------- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index dc6b22ce..68da4c51 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -25,7 +25,6 @@ use crate::base::storage::{ ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut, }; use crate::base::Scalar; -use crate::SMatrix; /* * @@ -300,14 +299,3 @@ where self.as_slice().iter().fold(0, |acc, e| acc + e.extent()) } } - -// TODO: Where to put this impl block? -impl SMatrix { - /// Creates a new statically-allocated matrix from the given [ArrayStorage]. - #[inline(always)] - pub const fn from_array_storage(storage: ArrayStorage) -> Self { - // This is sound because the row and column types are exactly the same as that of the - // storage, so there can be no mismatch - unsafe { Self::from_data_statically_unchecked(storage) } - } -} diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 3b4bc50c..f9b391ef 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -29,7 +29,7 @@ use crate::base::storage::{ ContiguousStorage, ContiguousStorageMut, Owned, SameShapeStorage, Storage, StorageMut, }; use crate::base::{Const, DefaultAllocator, OMatrix, OVector, Scalar, Unit}; -use crate::SimdComplexField; +use crate::{ArrayStorage, DMatrix, DVector, Dynamic, SMatrix, SimdComplexField, VecStorage}; /// A square matrix. pub type SquareMatrix = Matrix; @@ -317,6 +317,47 @@ impl Matrix { } } +impl SMatrix { + /// Creates a new statically-allocated matrix from the given [ArrayStorage]. + /// + /// This method exists primarily as a workaround for the fact that `from_data` can not + /// work in `const fn` contexts. + #[inline(always)] + pub const fn from_array_storage(storage: ArrayStorage) -> Self { + // This is sound because the row and column types are exactly the same as that of the + // storage, so there can be no mismatch + unsafe { Self::from_data_statically_unchecked(storage) } + } +} + +// TODO: Consider removing/deprecating `from_vec_storage` once we are able to make +// `from_data` const fn compatible +impl DMatrix { + /// Creates a new heap-allocated matrix from the given [VecStorage]. + /// + /// This method exists primarily as a workaround for the fact that `from_data` can not + /// work in `const fn` contexts. + pub const fn from_vec_storage(storage: VecStorage) -> Self { + // This is sound because the dimensions of the matrix and the storage are guaranteed + // to be the same + unsafe { Self::from_data_statically_unchecked(storage) } + } +} + +// TODO: Consider removing/deprecating `from_vec_storage` once we are able to make +// `from_data` const fn compatible +impl DVector { + /// Creates a new heap-allocated matrix from the given [VecStorage]. + /// + /// This method exists primarily as a workaround for the fact that `from_data` can not + /// work in `const fn` contexts. + pub const fn from_vec_storage(storage: VecStorage) -> Self { + // This is sound because the dimensions of the matrix and the storage are guaranteed + // to be the same + unsafe { Self::from_data_statically_unchecked(storage) } + } +} + impl> Matrix { /// Creates a new matrix with the given data. #[inline(always)] diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index f8d28769..29da127d 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -13,7 +13,6 @@ use crate::base::storage::{ }; use crate::base::{Scalar, Vector}; -use crate::{DMatrix, DVector}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -411,21 +410,3 @@ impl Extend for VecStorage { self.nrows = Dynamic::new(self.data.len()); } } - -impl DMatrix { - /// Creates a new heap-allocated matrix from the given [VecStorage]. - pub const fn from_vec_storage(storage: VecStorage) -> Self { - // This is sound because the dimensions of the matrix and the storage are guaranteed - // to be the same - unsafe { Self::from_data_statically_unchecked(storage) } - } -} - -impl DVector { - /// Creates a new heap-allocated matrix from the given [VecStorage]. - pub const fn from_vec_storage(storage: VecStorage) -> Self { - // This is sound because the dimensions of the matrix and the storage are guaranteed - // to be the same - unsafe { Self::from_data_statically_unchecked(storage) } - } -}