# HG changeset patch # User Tuomo Valkonen # Date 1756908963 18000 # Node ID bea0c3841cedd0aa34e7a2fb5c8e2d624fbbfa71 # Parent e7920e205785a66ff51ffe731aef4fa25a109683 cow_owned etc. diff -r e7920e205785 -r bea0c3841ced src/bisection_tree/btfn.rs --- a/src/bisection_tree/btfn.rs Tue Sep 02 15:18:30 2025 -0500 +++ b/src/bisection_tree/btfn.rs Wed Sep 03 09:16:03 2025 -0500 @@ -1,4 +1,4 @@ -use crate::instance::{ClosedSpace, Instance, Ownable, Space}; +use crate::instance::{ClosedSpace, Instance, MyCow, Ownable, Space}; use crate::mapping::{BasicDecomposition, DifferentiableImpl, DifferentiableMapping, Mapping}; use crate::types::Float; use numeric_literals::replace_float_literals; @@ -48,10 +48,16 @@ self } - /// Returns an owned instance of a reference. fn clone_owned(&self) -> Self::OwnedVariant { self.clone() } + + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + MyCow::Owned(self) + } } impl Space for BTFN diff -r e7920e205785 -r bea0c3841ced src/direct_product.rs --- a/src/direct_product.rs Tue Sep 02 15:18:30 2025 -0500 +++ b/src/direct_product.rs Wed Sep 03 09:16:03 2025 -0500 @@ -275,10 +275,18 @@ Pair(self.0.into_owned(), self.1.into_owned()) } - /// Returns an owned instance of a reference. + #[inline] fn clone_owned(&self) -> Self::OwnedVariant { Pair(self.0.clone_owned(), self.1.clone_owned()) } + + #[inline] + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + MyCow::Owned(self.into_owned()) + } } /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause @@ -392,8 +400,6 @@ D: Decomposition, Q: Decomposition, { - type OwnedInstance = Pair; - type Decomposition<'b> = Pair, Q::Decomposition<'b>> where @@ -443,7 +449,7 @@ } #[inline] - fn cow<'b>(self) -> MyCow<'b, Pair> + fn cow<'b>(self) -> MyCow<'b, Pair> where Self: 'b, { @@ -451,7 +457,7 @@ } #[inline] - fn own(self) -> Pair { + fn own(self) -> Pair { Pair(self.0.own(), self.1.own()) } } @@ -494,7 +500,7 @@ } #[inline] - fn cow<'b>(self) -> MyCow<'b, Pair> + fn cow<'b>(self) -> MyCow<'b, Pair> where Self: 'b, { @@ -502,7 +508,7 @@ } #[inline] - fn own(self) -> Pair { + fn own(self) -> Pair { let Pair(ref u, ref v) = self; Pair(u.own(), v.own()) } diff -r e7920e205785 -r bea0c3841ced src/instance.rs --- a/src/instance.rs Tue Sep 02 15:18:30 2025 -0500 +++ b/src/instance.rs Wed Sep 03 09:16:03 2025 -0500 @@ -33,45 +33,61 @@ /// Returns an owned instance of a reference. fn clone_owned(&self) -> Self::OwnedVariant; + + /// Returns an owned instance or a reference to one. + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b; } impl<'a, X: Ownable> Ownable for &'a X { type OwnedVariant = X::OwnedVariant; #[inline] - /// Returns an owned instance. fn into_owned(self) -> Self::OwnedVariant { self.clone_owned() } #[inline] - /// Returns an owned instance. fn clone_owned(&self) -> Self::OwnedVariant { (*self).into_owned() } + + #[inline] + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + todo!(); + } } impl<'a, X: Ownable> Ownable for &'a mut X { type OwnedVariant = X::OwnedVariant; #[inline] - /// Returns an owned instance. fn into_owned(self) -> Self::OwnedVariant { self.clone_owned() } #[inline] - /// Returns an owned instance. fn clone_owned(&self) -> Self::OwnedVariant { (&**self).into_owned() } + + #[inline] + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + todo!(); + } } impl<'a, X: Ownable> Ownable for MyCow<'a, X> { type OwnedVariant = X::OwnedVariant; #[inline] - /// Returns an owned instance. fn into_owned(self) -> Self::OwnedVariant { match self { EitherDecomp::Owned(x) => x.into_owned(), @@ -80,13 +96,20 @@ } #[inline] - /// Returns an owned instance. fn clone_owned(&self) -> Self::OwnedVariant { match self { EitherDecomp::Owned(x) => x.into_owned(), EitherDecomp::Borrowed(x) => x.into_owned(), } } + + #[inline] + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + todo!(); + } } /// Trait for abitrary mathematical spaces. @@ -134,6 +157,11 @@ fn clone_owned(&self) -> Self::OwnedVariant { *self } + + #[inline] + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { + todo!(); + } } }; } @@ -144,9 +172,6 @@ /// Marker type for decompositions to be used with [`Instance`]. pub trait Decomposition: Sized { - /// Owned instance - type OwnedInstance: Instance; - /// Possibly owned form of the decomposition type Decomposition<'b>: Instance where @@ -161,9 +186,6 @@ /// Lift the lightweight reference type into a full decomposition type. fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; - - // /// Lift the lightweight reference type into a fully owned type - // fn full_lift<'b>(r: Self::Reference<'b>) -> Self::OwnedInstance; } /// Most common [`Decomposition`] (into `Either`) that allows working with owned @@ -172,8 +194,6 @@ pub struct BasicDecomposition; impl Decomposition for BasicDecomposition { - type OwnedInstance = X; - type Decomposition<'b> = MyCow<'b, X> where @@ -215,25 +235,27 @@ Self: 'b; /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. - fn own(self) -> D::OwnedInstance; + fn own(self) -> X::OwnedSpace { + self.into_owned() + } // ************** automatically implemented methods below from here ************** /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. /// /// Default implementation uses [`Self::own`]. Consumes the input. - fn cow<'b>(self) -> MyCow<'b, D::OwnedInstance> + fn cow<'b>(self) -> MyCow<'b, X::OwnedSpace> where Self: 'b, { - MyCow::Owned(self.own()) + self.cow_owned() } #[inline] /// Evaluates `f` on a reference to self. /// /// Default implementation uses [`Self::cow`]. Consumes the input. - fn eval<'b, R>(self, f: impl FnOnce(&D::OwnedInstance) -> R) -> R + fn eval<'b, R>(self, f: impl FnOnce(&X::OwnedSpace) -> R) -> R where X: 'b, Self: 'b, @@ -247,8 +269,8 @@ /// Default implementation uses [`Self::cow`]. Consumes the input. fn either<'b, R>( self, - f: impl FnOnce(D::OwnedInstance) -> R, - g: impl FnOnce(&D::OwnedInstance) -> R, + f: impl FnOnce(X::OwnedSpace) -> R, + g: impl FnOnce(&X::OwnedSpace) -> R, ) -> R where Self: 'b, @@ -278,19 +300,6 @@ { f(self) } - - #[inline] - fn own(self) -> X { - self - } - - #[inline] - fn cow<'b>(self) -> MyCow<'b, X> - where - Self: 'b, - { - MyCow::Owned(self) - } } impl<'a, X: Space + Clone> Instance for &'a X { @@ -311,19 +320,6 @@ { f(*self) } - - #[inline] - fn own(self) -> X { - self.clone() - } - - #[inline] - fn cow<'b>(self) -> MyCow<'b, X> - where - Self: 'b, - { - MyCow::Borrowed(self) - } } impl<'a, X: Space + Clone> Instance for &'a mut X { @@ -344,19 +340,6 @@ { f(*self) } - - #[inline] - fn own(self) -> X { - self.clone() - } - - #[inline] - fn cow<'b>(self) -> MyCow<'b, X> - where - Self: 'b, - { - EitherDecomp::Borrowed(self) - } } impl<'a, X: Space + Clone> Instance for MyCow<'a, X> { @@ -380,25 +363,6 @@ MyCow::Owned(b) => f(&b), } } - - #[inline] - fn own(self) -> X { - match self { - MyCow::Borrowed(a) => a.own(), - MyCow::Owned(b) => b.own(), - } - } - - #[inline] - fn cow<'b>(self) -> MyCow<'b, X> - where - Self: 'b, - { - match self { - MyCow::Borrowed(a) => a.cow(), - MyCow::Owned(b) => b.cow(), - } - } } /// Marker type for mutable decompositions to be used with [`InstanceMut`]. diff -r e7920e205785 -r bea0c3841ced src/loc.rs --- a/src/loc.rs Tue Sep 02 15:18:30 2025 -0500 +++ b/src/loc.rs Wed Sep 03 09:16:03 2025 -0500 @@ -4,7 +4,7 @@ */ use crate::euclidean::*; -use crate::instance::{BasicDecomposition, Instance, Ownable}; +use crate::instance::{BasicDecomposition, Instance, MyCow, Ownable}; use crate::linops::{Linear, Mapping, VectorSpace, AXPY}; use crate::mapping::Space; use crate::maputil::{map1, map1_mut, map2, map2_mut, FixedLength, FixedLengthMut}; @@ -40,6 +40,14 @@ fn clone_owned(&self) -> Self::OwnedVariant { self.clone() } + + /// Returns an owned instance of a reference. + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + MyCow::Owned(self) + } } impl Display for Loc { diff -r e7920e205785 -r bea0c3841ced src/nalgebra_support.rs --- a/src/nalgebra_support.rs Tue Sep 02 15:18:30 2025 -0500 +++ b/src/nalgebra_support.rs Wed Sep 03 09:16:03 2025 -0500 @@ -9,7 +9,7 @@ */ use crate::euclidean::*; -use crate::instance::{Decomposition, Instance, Ownable, Space}; +use crate::instance::{Decomposition, Instance, MyCow, Ownable, Space}; use crate::linops::*; use crate::norms::*; use crate::types::Float; @@ -43,6 +43,14 @@ fn clone_owned(&self) -> Self::OwnedVariant { Matrix::clone_owned(self) } + + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + todo!() + //MyCow::owned(self.into_owned()) + } } trait StridesOk>::Buffer>: @@ -103,8 +111,6 @@ DefaultAllocator: Allocator, ShapeConstraint: StridesOk + StridesOk, { - type OwnedInstance = OMatrix; - type Decomposition<'b> = OMatrix where