From 23d646c34263970c69767266ea12611af56b665e Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Fri, 28 Sep 2018 16:54:41 -0400 Subject: [PATCH] Add an `Into` impl for converting `MatrixSlice`s into `MatrixMN` See #342. --- src/base/conversion.rs | 99 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 1b9d68e1..2d857a69 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -5,12 +5,16 @@ use std::convert::{AsMut, AsRef, From, Into}; use std::mem; use std::ptr; +use std::ops::Mul; +use typenum::Prod; +use generic_array::ArrayLength; + use base::allocator::{Allocator, SameShapeAllocator}; use base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; -use base::dimension::{Dim, U1, U10, U11, U12, U13, U14, U15, U16, U2, U3, U4, U5, U6, U7, U8, U9}; +use base::dimension::{Dim, DimName, Dynamic, U1, U10, U11, U12, U13, U14, U15, U16, U2, U3, U4, U5, U6, U7, U8, U9}; use base::iter::{MatrixIter, MatrixIterMut}; use base::storage::{ContiguousStorage, ContiguousStorageMut, Storage, StorageMut}; -use base::{DefaultAllocator, Matrix, MatrixMN, Scalar}; +use base::{DefaultAllocator, Matrix, MatrixMN, MatrixArray, MatrixSlice, MatrixSliceMut, MatrixVec, Scalar}; // FIXME: too bad this won't work allo slice conversions. impl SubsetOf> for MatrixMN @@ -326,3 +330,94 @@ impl_from_into_mint_2D!( (U3, U4) => ColumnMatrix3x4{x, y, z}[3]; (U4, U4) => ColumnMatrix4{x, y, z, w}[4]; ); + + +impl<'a, N, R, C, RStride, CStride> From> + for Matrix> +where + N: Scalar, + R: DimName, + C: DimName, + RStride: Dim, + CStride: Dim, + R::Value: Mul, + Prod: ArrayLength, +{ + fn from(matrix_slice: MatrixSlice<'a, N, R, C, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +} + +impl<'a, N, C, RStride, CStride> From> + for Matrix> +where + N: Scalar, + C: Dim, + RStride: Dim, + CStride: Dim, +{ + fn from(matrix_slice: MatrixSlice<'a, N, Dynamic, C, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +} + +impl<'a, N, R, RStride, CStride> From> + for Matrix> +where + N: Scalar, + R: DimName, + RStride: Dim, + CStride: Dim, +{ + fn from(matrix_slice: MatrixSlice<'a, N, R, Dynamic, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +} + +impl<'a, N, R, C, RStride, CStride> From> + for Matrix> +where + N: Scalar, + R: DimName, + C: DimName, + RStride: Dim, + CStride: Dim, + R::Value: Mul, + Prod: ArrayLength, +{ + fn from(matrix_slice: MatrixSliceMut<'a, N, R, C, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +} + +impl<'a, N, C, RStride, CStride> From> + for Matrix> +where + N: Scalar, + C: Dim, + RStride: Dim, + CStride: Dim, +{ + fn from(matrix_slice: MatrixSliceMut<'a, N, Dynamic, C, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +} + +impl<'a, N, R, RStride, CStride> From> + for Matrix> +where + N: Scalar, + R: DimName, + RStride: Dim, + CStride: Dim, +{ + fn from(matrix_slice: MatrixSliceMut<'a, N, R, Dynamic, RStride, CStride>) -> Self + { + matrix_slice.into_owned() + } +}