From 31e687bd76b22430ed3f18e6af761e4a551fa78e Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Mon, 14 Nov 2022 16:41:01 +0100 Subject: [PATCH] Implement Matrix::{as_view, as_view_mut} methods --- src/base/conversion.rs | 3 -- src/base/matrix_view.rs | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 79a61461..20553257 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -373,7 +373,6 @@ where impl<'a, T, R, C, RView, CView, RStride, CStride, S> From<&'a Matrix> for MatrixView<'a, T, RView, CView, RStride, CStride> where - T: Scalar, R: Dim, C: Dim, RView: Dim, @@ -410,7 +409,6 @@ where impl<'a, T, R, C, RView, CView, RStride, CStride, S> From<&'a mut Matrix> for MatrixView<'a, T, RView, CView, RStride, CStride> where - T: Scalar, R: Dim, C: Dim, RView: Dim, @@ -447,7 +445,6 @@ where impl<'a, T, R, C, RView, CView, RStride, CStride, S> From<&'a mut Matrix> for MatrixViewMut<'a, T, RView, CView, RStride, CStride> where - T: Scalar, R: Dim, C: Dim, RView: Dim, diff --git a/src/base/matrix_view.rs b/src/base/matrix_view.rs index ef024f19..210e7a57 100644 --- a/src/base/matrix_view.rs +++ b/src/base/matrix_view.rs @@ -8,6 +8,7 @@ use crate::base::dimension::{Const, Dim, DimName, Dynamic, IsNotStaticOne, U1}; use crate::base::iter::MatrixIter; use crate::base::storage::{IsContiguous, Owned, RawStorage, RawStorageMut, Storage}; use crate::base::{Matrix, Scalar}; +use crate::constraint::{DimEq, ShapeConstraint}; macro_rules! view_storage_impl ( ($doc: expr; $Storage: ident as $SRef: ty; $legacy_name:ident => $T: ident.$get_addr: ident ($Ptr: ty as $Ref: ty)) => { @@ -1091,3 +1092,87 @@ where unsafe { Matrix::from_data_statically_unchecked(data) } } } + +impl Matrix +where + R: Dim, + C: Dim, + S: RawStorage, +{ + /// Returns this matrix as a view. + /// + /// The returned view type is generally ambiguous unless specified. + /// This is particularly useful when working with functions or methods that take + /// matrix views as input. + /// + /// # Panics + /// Panics if the dimensions of the view and the matrix are not compatible and this cannot + /// be proven at compile-time. This might happen, for example, when constructing a static + /// view of size 3x3 from a dynamically sized matrix of dimension 5x5. + /// + /// # Examples + /// ``` + /// use nalgebra::{DMatrixSlice, SMatrixView}; + /// + /// fn consume_view(_: DMatrixSlice) {} + /// + /// let matrix = nalgebra::Matrix3::zeros(); + /// consume_view(matrix.as_view()); + /// + /// let dynamic_view: DMatrixSlice = matrix.as_view(); + /// let static_view_from_dyn: SMatrixView = dynamic_view.as_view(); + /// ``` + pub fn as_view(&self) -> MatrixView<'_, T, RView, CView, RViewStride, CViewStride> + where + RView: Dim, + CView: Dim, + RViewStride: Dim, + CViewStride: Dim, + ShapeConstraint: DimEq + DimEq + DimEq + DimEq + { + // Defer to (&matrix).into() + self.into() + } +} + +impl Matrix +where + R: Dim, + C: Dim, + S: RawStorageMut, +{ + /// Returns this matrix as a mutable view. + /// + /// The returned view type is generally ambiguous unless specified. + /// This is particularly useful when working with functions or methods that take + /// matrix views as input. + /// + /// # Panics + /// Panics if the dimensions of the view and the matrix are not compatible and this cannot + /// be proven at compile-time. This might happen, for example, when constructing a static + /// view of size 3x3 from a dynamically sized matrix of dimension 5x5. + /// + /// # Examples + /// ``` + /// use nalgebra::{DMatrixViewMut, SMatrixViewMut}; + /// + /// fn consume_view(_: DMatrixViewMut) {} + /// + /// let mut matrix = nalgebra::Matrix3::zeros(); + /// consume_view(matrix.as_view_mut()); + /// + /// let mut dynamic_view: DMatrixViewMut = matrix.as_view_mut(); + /// let static_view_from_dyn: SMatrixViewMut = dynamic_view.as_view_mut(); + /// ``` + pub fn as_view_mut(&mut self) -> MatrixViewMut<'_, T, RView, CView, RViewStride, CViewStride> + where + RView: Dim, + CView: Dim, + RViewStride: Dim, + CViewStride: Dim, + ShapeConstraint: DimEq + DimEq + DimEq + DimEq + { + // Defer to (&mut matrix).into() + self.into() + } +}