src/instance.rs

branch
dev
changeset 162
bea0c3841ced
parent 159
279b1f5b8608
child 163
b4a47e8e80d1
--- 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<X: Space>: Sized {
-    /// Owned instance
-    type OwnedInstance: Instance<X, Self>;
-
     /// Possibly owned form of the decomposition
     type Decomposition<'b>: Instance<X, Self>
     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<X, &'b X>`) that allows working with owned
@@ -172,8 +194,6 @@
 pub struct BasicDecomposition;
 
 impl<X: Space + Clone> Decomposition<X> 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<X, BasicDecomposition> 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<X, BasicDecomposition> 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<X, BasicDecomposition> 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`].

mercurial