From: Simon Tatham Date: Thu, 10 Apr 2025 11:55:25 +0000 (+0100) Subject: Allow converting back into slice references. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=318ff12bebef5050c79374431039a7e5a84f53be;p=nimber.git Allow converting back into slice references. --- diff --git a/src/finitenimber.rs b/src/finitenimber.rs index 828ee64..610fa3d 100644 --- a/src/finitenimber.rs +++ b/src/finitenimber.rs @@ -4,14 +4,21 @@ use std::ops::BitXor; pub trait FiniteNimberBase: BitXor + 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()) }