src/instance.rs

branch
dev
changeset 166
20fa28637737
parent 165
478c23ce7cef
child 168
93daa824c04a
--- a/src/instance.rs	Wed Sep 03 12:55:27 2025 -0500
+++ b/src/instance.rs	Wed Sep 03 13:15:32 2025 -0500
@@ -38,6 +38,11 @@
     fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
     where
         Self: 'b;
+
+    /// Returns an owned instance or a reference to one.
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b;
 }
 
 impl<'a, X: Ownable> Ownable for &'a X {
@@ -45,12 +50,12 @@
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
-        self.clone_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
-        (*self).into_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
@@ -58,7 +63,14 @@
     where
         Self: 'b,
     {
-        todo!();
+        X::ref_cow_owned(self)
+    }
+
+    fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        X::ref_cow_owned(self)
     }
 }
 
@@ -67,12 +79,12 @@
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
-        self.clone_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
-        (&**self).into_owned()
+        X::clone_owned(self)
     }
 
     #[inline]
@@ -80,35 +92,124 @@
     where
         Self: 'b,
     {
-        todo!();
+        X::ref_cow_owned(self)
+    }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        X::ref_cow_owned(self)
     }
 }
 
-impl<'a, X: Ownable> Ownable for MyCow<'a, X> {
-    type OwnedVariant = X::OwnedVariant;
+impl<'a, A, B> Ownable for EitherDecomp<A, B>
+where
+    A: Ownable,
+    B: Ownable<OwnedVariant = A::OwnedVariant>,
+{
+    type OwnedVariant = A::OwnedVariant;
 
     #[inline]
     fn into_owned(self) -> Self::OwnedVariant {
         match self {
-            EitherDecomp::Owned(x) => x.into_owned(),
-            EitherDecomp::Borrowed(x) => x.into_owned(),
+            EitherDecomp::Owned(a) => A::into_owned(a),
+            EitherDecomp::Borrowed(b) => B::into_owned(b),
         }
     }
 
     #[inline]
     fn clone_owned(&self) -> Self::OwnedVariant {
         match self {
-            EitherDecomp::Owned(x) => x.into_owned(),
-            EitherDecomp::Borrowed(x) => x.into_owned(),
+            EitherDecomp::Owned(a) => A::clone_owned(a),
+            EitherDecomp::Borrowed(b) => B::clone_owned(b),
+        }
+    }
+
+    #[inline]
+    fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        A: 'b,
+        B: 'b,
+    {
+        match self {
+            EitherDecomp::Owned(a) => A::cow_owned(a),
+            EitherDecomp::Borrowed(b) => B::cow_owned(b),
+        }
+    }
+
+    #[inline]
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        match self {
+            EitherDecomp::Owned(a) => A::ref_cow_owned(a),
+            EitherDecomp::Borrowed(b) => B::ref_cow_owned(b),
         }
     }
+}
+
+#[macro_export]
+macro_rules! self_ownable {
+    ($type:ty where $($qual:tt)*) => {
+        impl<$($qual)*> $crate::instance::Ownable for $type {
+            type OwnedVariant = Self;
+
+            #[inline]
+            fn into_owned(self) -> Self::OwnedVariant {
+                self
+            }
+
+            fn clone_owned(&self) -> Self::OwnedVariant {
+                self.clone()
+            }
+
+            fn cow_owned<'b>(self) -> $crate::instance::MyCow<'b, Self::OwnedVariant>
+            where
+                Self: 'b,
+            {
+                $crate::instance::MyCow::Owned(self)
+            }
+
+            fn ref_cow_owned<'b>(&'b self) -> $crate::instance::MyCow<'b, Self::OwnedVariant>
+            where
+                Self: 'b,
+            {
+                $crate::instance::MyCow::Borrowed(self)
+            }
+        }
+    };
+}
+
+self_ownable!(Vec<T> where T : Clone);
+
+impl<'a, T: Clone> Ownable for &'a [T] {
+    type OwnedVariant = Vec<T>;
+
+    #[inline]
+    fn into_owned(self) -> Self::OwnedVariant {
+        Vec::from(self)
+    }
+
+    #[inline]
+    fn clone_owned(&self) -> Self::OwnedVariant {
+        Vec::from(*self)
+    }
 
     #[inline]
     fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
     where
         Self: 'b,
     {
-        todo!();
+        MyCow::Owned(Vec::from(self))
+    }
+
+    fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
+    where
+        Self: 'b,
+    {
+        MyCow::Owned(Vec::from(*self))
     }
 }
 
@@ -161,7 +262,12 @@
 
             #[inline]
             fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b {
-                todo!();
+                MyCow::Owned(self)
+            }
+
+            #[inline]
+            fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b {
+                MyCow::Owned(*self)
             }
         }
     };

mercurial