From 498b377d0e7d31a77cecd4540ce84c4a132aee98 Mon Sep 17 00:00:00 2001 From: Cryptjar Date: Fri, 18 Jun 2021 02:23:40 +0200 Subject: [PATCH] Add `into_slice[_mut]` and impl to SliceStorage[Mut]. --- src/base/matrix_slice.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/base/matrix_slice.rs b/src/base/matrix_slice.rs index 6cb713f1..2b287f33 100644 --- a/src/base/matrix_slice.rs +++ b/src/base/matrix_slice.rs @@ -77,6 +77,21 @@ macro_rules! slice_storage_impl( $T::from_raw_parts(storage.$get_addr(start.0, start.1), shape, strides) } } + + impl <'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> + $T<'a, T, R, C, RStride, CStride> + { + /// Extracts the original slice from this storage + pub fn into_slice(self) -> &'a [T] { + let (nrows, ncols) = self.shape(); + if nrows.value() != 0 && ncols.value() != 0 { + let sz = self.linear_index(nrows.value() - 1, ncols.value() - 1); + unsafe { slice::from_raw_parts(self.ptr, sz + 1) } + } else { + unsafe { slice::from_raw_parts(self.ptr, 0) } + } + } + } } ); @@ -108,6 +123,21 @@ impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> Clone } } +impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> + SliceStorageMut<'a, T, R, C, RStride, CStride> +{ + /// Extracts the original slice from this storage + pub fn into_slice_mut(self) -> &'a mut [T] { + let (nrows, ncols) = self.shape(); + if nrows.value() != 0 && ncols.value() != 0 { + let sz = self.linear_index(nrows.value() - 1, ncols.value() - 1); + unsafe { slice::from_raw_parts_mut(self.ptr, sz + 1) } + } else { + unsafe { slice::from_raw_parts_mut(self.ptr, 0) } + } + } +} + macro_rules! storage_impl( ($($T: ident),* $(,)*) => {$( unsafe impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> Storage