# HG changeset patch # User Tuomo Valkonen # Date 1747105002 18000 # Node ID 22fd33834ab77a03f55dd8a9ec078751cc81e405 # Parent 2b13f8a0c8ba27411dfe2e0829a43bd7d6e96bb1 instance-lifetime-fubar2 diff -r 2b13f8a0c8ba -r 22fd33834ab7 src/direct_product.rs --- a/src/direct_product.rs Mon May 12 20:40:14 2025 -0500 +++ b/src/direct_product.rs Mon May 12 21:56:42 2025 -0500 @@ -365,15 +365,6 @@ = Pair, Q::Decomposition<'b>> where Pair: 'b; - type Reference<'b> - = Pair, Q::Reference<'b>> - where - Pair: 'b; - - #[inline] - fn lift<'b>(Pair(u, v): Self::Reference<'b>) -> Self::Decomposition<'b> { - Pair(D::lift(u), Q::lift(v)) - } } impl Instance, PairDecomposition> for Pair @@ -399,7 +390,7 @@ fn eval_ref_decompose<'b, R>( &'b self, - f: impl FnOnce(Pair, Q::Reference<'b>>) -> R, + f: impl FnOnce(Pair, Q::Decomposition<'b>>) -> R, ) -> R where Pair: 'b, @@ -442,15 +433,13 @@ Pair: 'b, Self: 'b, { - self.0.eval_ref_decompose(|a| { - self.1 - .eval_ref_decompose(|b| f(Pair(D::lift(a), Q::lift(b)))) - }) + self.0 + .eval_ref_decompose(|a| self.1.eval_ref_decompose(|b| f(Pair(a, b)))) } fn eval_ref_decompose<'b, R>( &'b self, - f: impl FnOnce(Pair, Q::Reference<'b>>) -> R, + f: impl FnOnce(Pair, Q::Decomposition<'b>>) -> R, ) -> R where Pair: 'b, diff -r 2b13f8a0c8ba -r 22fd33834ab7 src/fe_model/p2_local_model.rs --- a/src/fe_model/p2_local_model.rs Mon May 12 20:40:14 2025 -0500 +++ b/src/fe_model/p2_local_model.rs Mon May 12 21:56:42 2025 -0500 @@ -32,7 +32,7 @@ impl<'a, F: Float> Set> for RealInterval { #[inline] fn contains>>(&self, z: I) -> bool { - z.eval_ref_decompose(|&Loc([x])| { + z.eval(|&Loc([x])| { let &[Loc([x0]), Loc([x1])] = &self.0; (x0 < x && x < x1) || (x1 < x && x < x0) }) diff -r 2b13f8a0c8ba -r 22fd33834ab7 src/instance.rs --- a/src/instance.rs Mon May 12 20:40:14 2025 -0500 +++ b/src/instance.rs Mon May 12 21:56:42 2025 -0500 @@ -62,20 +62,10 @@ /// Marker type for decompositions to be used with [`Instance`]. pub trait Decomposition: Sized { - /// Possibly owned form of the decomposition + /// The composition type Decomposition<'b>: Instance where X: 'b; - /// Unlikely owned form of the decomposition. - /// Type for a lightweight intermediate conversion that does not own the original variable. - /// Usually this is just a reference, but may also be a lightweight structure that - /// contains references; see the implementation for [`crate::direct_product::Pair`]. - type Reference<'b>: Instance + Copy - where - X: 'b; - - /// Left the lightweight reference type into a full decomposition type. - fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; } /// Most common [`Decomposition`] (into `Either`) that allows working with owned @@ -88,15 +78,6 @@ = MyCow<'b, X> where X: 'b; - type Reference<'b> - = &'b X - where - X: 'b; - - #[inline] - fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> { - MyCow::Borrowed(r) - } } /// Helper trait for functions to work with either owned values or references to either the @@ -117,7 +98,7 @@ /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result. /// Does not consume self. - fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(D::Reference<'b>) -> R) -> R + fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R where X: 'b, Self: 'b; @@ -175,12 +156,12 @@ } #[inline] - fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R + fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R where X: 'b, Self: 'b, { - f(self) + f(MyCow::Borrowed(&self)) } #[inline] @@ -208,12 +189,12 @@ } #[inline] - fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R + fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R where X: 'b, Self: 'b, { - f(*self) + f(MyCow::Borrowed(self)) } #[inline] @@ -241,12 +222,12 @@ } #[inline] - fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R + fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R where X: 'b, Self: 'b, { - f(*self) + f(EitherDecomp::Borrowed(self)) } #[inline] @@ -275,14 +256,14 @@ } #[inline] - fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R + fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R where X: 'b, Self: 'b, { match self { - MyCow::Borrowed(a) => f(a), - MyCow::Owned(b) => f(&b), + MyCow::Borrowed(a) => f(MyCow::Borrowed(a)), + MyCow::Owned(b) => f(MyCow::Borrowed(&b)), } } diff -r 2b13f8a0c8ba -r 22fd33834ab7 src/nalgebra_support.rs --- a/src/nalgebra_support.rs Mon May 12 20:40:14 2025 -0500 +++ b/src/nalgebra_support.rs Mon May 12 21:56:42 2025 -0500 @@ -224,7 +224,7 @@ { #[inline] fn dot>(&self, other: I) -> E { - other.eval_ref_decompose(|r| Vector::::dot(self, r)) + other.eval(|r| Vector::::dot(self, r)) } #[inline] @@ -234,7 +234,7 @@ #[inline] fn dist2_squared>(&self, other: I) -> E { - other.eval_ref_decompose(|r| metric_distance_squared(self, r)) + other.eval(|r| metric_distance_squared(self, r)) } } @@ -305,7 +305,7 @@ { #[inline] fn dist>(&self, other: I, _: L1) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) + other.eval(|r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) } } @@ -331,7 +331,7 @@ { #[inline] fn dist>(&self, other: I, _: L2) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) + other.eval(|r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) } } @@ -357,7 +357,7 @@ { #[inline] fn dist>(&self, other: I, _: Linfinity) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) + other.eval(|r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) } }