Move from_{}_storage impl blocks to matrix.rs

This commit is contained in:
Andreas Longva 2021-05-07 09:00:29 +02:00
parent 39b275fc92
commit 3a3bc55f66
3 changed files with 42 additions and 32 deletions

View File

@ -25,7 +25,6 @@ use crate::base::storage::{
ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut, ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut,
}; };
use crate::base::Scalar; use crate::base::Scalar;
use crate::SMatrix;
/* /*
* *
@ -300,14 +299,3 @@ where
self.as_slice().iter().fold(0, |acc, e| acc + e.extent()) self.as_slice().iter().fold(0, |acc, e| acc + e.extent())
} }
} }
// TODO: Where to put this impl block?
impl<T, const R: usize, const C: usize> SMatrix<T, R, C> {
/// Creates a new statically-allocated matrix from the given [ArrayStorage].
#[inline(always)]
pub const fn from_array_storage(storage: ArrayStorage<T, R, C>) -> 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) }
}
}

View File

@ -29,7 +29,7 @@ use crate::base::storage::{
ContiguousStorage, ContiguousStorageMut, Owned, SameShapeStorage, Storage, StorageMut, ContiguousStorage, ContiguousStorageMut, Owned, SameShapeStorage, Storage, StorageMut,
}; };
use crate::base::{Const, DefaultAllocator, OMatrix, OVector, Scalar, Unit}; use crate::base::{Const, DefaultAllocator, OMatrix, OVector, Scalar, Unit};
use crate::SimdComplexField; use crate::{ArrayStorage, DMatrix, DVector, Dynamic, SMatrix, SimdComplexField, VecStorage};
/// A square matrix. /// A square matrix.
pub type SquareMatrix<T, D, S> = Matrix<T, D, D, S>; pub type SquareMatrix<T, D, S> = Matrix<T, D, D, S>;
@ -317,6 +317,47 @@ impl<T, R, C, S> Matrix<T, R, C, S> {
} }
} }
impl<T, const R: usize, const C: usize> SMatrix<T, R, C> {
/// 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<T, R, C>) -> 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<T> DMatrix<T> {
/// 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<T, Dynamic, Dynamic>) -> 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<T> DVector<T> {
/// 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<T, Dynamic, U1>) -> 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<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> { impl<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> {
/// Creates a new matrix with the given data. /// Creates a new matrix with the given data.
#[inline(always)] #[inline(always)]

View File

@ -13,7 +13,6 @@ use crate::base::storage::{
}; };
use crate::base::{Scalar, Vector}; use crate::base::{Scalar, Vector};
use crate::{DMatrix, DVector};
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation; use abomonation::Abomonation;
@ -411,21 +410,3 @@ impl<T> Extend<T> for VecStorage<T, Dynamic, U1> {
self.nrows = Dynamic::new(self.data.len()); self.nrows = Dynamic::new(self.data.len());
} }
} }
impl<T> DMatrix<T> {
/// Creates a new heap-allocated matrix from the given [VecStorage].
pub const fn from_vec_storage(storage: VecStorage<T, Dynamic, Dynamic>) -> 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<T> DVector<T> {
/// Creates a new heap-allocated matrix from the given [VecStorage].
pub const fn from_vec_storage(storage: VecStorage<T, Dynamic, U1>) -> 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) }
}
}