chiark / gitweb /
Allow converting back into slice references.
authorSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:55:25 +0000 (12:55 +0100)
committerSimon Tatham <anakin@pobox.com>
Thu, 10 Apr 2025 11:55:25 +0000 (12:55 +0100)
src/finitenimber.rs

index 828ee644d14fd42211a46033616b669e301122c4..610fa3dfb5ea45068372e0f1a0719bcc350a91d1 100644 (file)
@@ -4,14 +4,21 @@ use std::ops::BitXor;
 pub trait FiniteNimberBase:
     BitXor<Output = Self::Owned> + Eq + Debug + Sized
 {
+    type Cheap<'a>: FiniteNimberBase where Self: 'a;
     type Owned: FiniteNimberBase + Clone;
 
+    fn to_cheap<'a>(&'a self) -> Self::Cheap<'a>;
     fn to_owned(self) -> Self::Owned;
 }
 
 impl FiniteNimberBase for u64 {
+    type Cheap<'a> = Self where Self: 'a;
     type Owned = Self;
 
+    fn to_cheap<'a>(&'a self) -> Self::Cheap<'a> {
+        *self
+    }
+
     fn to_owned(self) -> Self::Owned {
         self
     }
@@ -52,16 +59,26 @@ impl BitXor for IntSlice<'_> {
 }
 
 impl FiniteNimberBase for IntVector {
-    type Owned = Self;
+    type Cheap<'a> = IntSlice<'a> where Self: 'a;
+    type Owned = IntVector;
+
+    fn to_cheap<'a>(&'a self) -> Self::Cheap<'a> {
+        IntSlice(&self.0)
+    }
 
     fn to_owned(self) -> Self::Owned {
         self
     }
 }
 
-impl FiniteNimberBase for IntSlice<'_> {
+impl<'a> FiniteNimberBase for IntSlice<'a> {
+    type Cheap<'b> = IntSlice<'b> where Self: 'b;
     type Owned = IntVector;
 
+    fn to_cheap<'b>(&'b self) -> Self::Cheap<'b> {
+        IntSlice(self.0)
+    }
+
     fn to_owned(self) -> Self::Owned {
         IntVector(self.0.into())
     }