Skip to content

Commit

Permalink
Move from_{}_storage impl blocks to matrix.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andlon committed May 7, 2021
1 parent 39b275f commit 3a3bc55
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
12 changes: 0 additions & 12 deletions src/base/array_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::base::storage::{
ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut,
};
use crate::base::Scalar;
use crate::SMatrix;

/*
*
Expand Down Expand Up @@ -300,14 +299,3 @@ where
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) }
}
}
43 changes: 42 additions & 1 deletion src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, D, S> = Matrix<T, D, D, S>;
Expand Down Expand Up @@ -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> {
/// Creates a new matrix with the given data.
#[inline(always)]
Expand Down
19 changes: 0 additions & 19 deletions src/base/vec_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::base::storage::{
};
use crate::base::{Scalar, Vector};

use crate::{DMatrix, DVector};
#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;

Expand Down Expand Up @@ -411,21 +410,3 @@ impl<T> Extend<T> for VecStorage<T, Dynamic, U1> {
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) }
}
}

0 comments on commit 3a3bc55

Please sign in to comment.