# HG changeset patch # User Tuomo Valkonen # Date 1756950741 18000 # Node ID 73608862ef54f9be78e47414b1a1b177f2b5d1be # Parent fa8df5a14486ba50716440a2a84bc7e8f777b537 nalgebra instance hacks diff -r fa8df5a14486 -r 73608862ef54 src/nalgebra_support.rs --- a/src/nalgebra_support.rs Wed Sep 03 20:19:41 2025 -0500 +++ b/src/nalgebra_support.rs Wed Sep 03 20:52:21 2025 -0500 @@ -118,7 +118,7 @@ ShapeConstraint: StridesOk + StridesOk, { type Decomposition<'b> - = OMatrix + = MyCow<'b, OMatrix> where Matrix: 'b; type Reference<'b> @@ -131,7 +131,7 @@ where S: 'b, { - r.into_owned() + MyCow::Owned(r.into_owned()) } } @@ -148,14 +148,14 @@ #[inline] fn either<'b, R>( self, - f: impl FnOnce(OMatrix) -> R, + f: impl FnOnce(MyCow<'b, OMatrix>) -> R, _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R, ) -> R where Self: 'b, { // TODO: should not turn non-owned matrices into owned - f(self.into_owned()) + f(MyCow::Owned(self.into_owned())) } #[inline] @@ -184,11 +184,11 @@ } #[inline] - fn decompose<'b>(self) -> OMatrix + fn decompose<'b>(self) -> MyCow<'b, OMatrix> where Self: 'b, { - self.into_owned() + self.cow_owned() } } @@ -206,7 +206,7 @@ #[inline] fn either<'b, R>( self, - _f: impl FnOnce(OMatrix) -> R, + _f: impl FnOnce(MyCow<'b, OMatrix>) -> R, g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R, ) -> R where @@ -240,12 +240,68 @@ } #[inline] - fn decompose<'b>(self) -> OMatrix + fn decompose<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } +} + +impl<'a, S1, M, K, E> Instance, MatrixDecomposition> + for MyCow<'a, OMatrix> +where + S1: Storage, + M: Dim, + K: Dim, + E: Scalar + Zero + One + Copy, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + #[inline] + fn either<'b, R>( + self, + f: impl FnOnce(MyCow<'b, OMatrix>) -> R, + _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R, + ) -> R where Self: 'b, { + f(self) + } + + #[inline] + fn eval_ref<'b, R>( + &'b self, + f: impl FnOnce(>>::Reference<'b>) -> R, + ) -> R + where + Self: 'b, + Matrix: 'b, + { + f(self.as_view::()) + } + + #[inline] + fn own(self) -> OMatrix { self.into_owned() } + + #[inline] + fn cow<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self + } + + #[inline] + fn decompose<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self + } } impl Mapping> for Matrix @@ -262,7 +318,7 @@ #[inline] fn apply>>(&self, x: I) -> Self::Codomain { - x.either(|owned| self.mul(owned), |refr| self.mul(refr)) + x.eval_ref(|refr| self.mul(refr)) } }