Mon, 12 May 2025 22:48:16 -0500
instantiated-fubar
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | /*! |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | Helper traits to work with references or owned values of types and their subsets. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | */ |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | #[derive(Clone, Copy)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | pub enum EitherDecomp<A, B> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | Owned(A), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | Borrowed(B), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | |
|
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
11 | /// A very basic implementation of [`std::borrow::Cow`] without a [`Clone`] trait dependency. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | pub type MyCow<'b, X> = EitherDecomp<X, &'b X>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | impl<'b, X> std::ops::Deref for MyCow<'b, X> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | type Target = X; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | fn deref(&self) -> &Self::Target { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | match self { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | EitherDecomp::Owned(x) => &x, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | EitherDecomp::Borrowed(x) => x, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | impl<'b, X> MyCow<'b, X> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
28 | pub fn into_owned(self) -> X |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
29 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
30 | X: Clone, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
31 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | match self { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | EitherDecomp::Owned(x) => x, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | EitherDecomp::Borrowed(x) => x.clone(), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | /// Trait for abitrary mathematical spaces. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
40 | pub trait Space: Instance<Self, Self::Decomp> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | /// Default decomposition for the space |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
42 | type Decomp: Decomposition<Self>; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | #[macro_export] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | macro_rules! impl_basic_space { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | ($($type:ty)*) => { $( |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | impl $crate::instance::Space for $type { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | type Decomp = $crate::instance::BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | )* }; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | ($type:ty where $($where:tt)*) => { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | impl<$($where)*> $crate::instance::Space for $type { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | type Decomp = $crate::instance::BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | }; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
57 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | impl_basic_space!(u8 u16 u32 u64 u128 usize |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
60 | i8 i16 i32 i64 i128 isize |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
61 | f32 f64); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
62 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
63 | /// Marker type for decompositions to be used with [`Instance`]. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
64 | pub trait Decomposition<X: Space>: Sized { |
| 136 | 65 | /// The composition |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
66 | type Decomposition<'b>: Instance<X, Self> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
67 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
68 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | /// values and all sorts of references. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | #[derive(Copy, Clone, Debug)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
74 | pub struct BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
75 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
76 | impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
77 | type Decomposition<'b> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
78 | = MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
79 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
80 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | |
| 137 | 83 | pub trait Instantiated<X: Space, D = <X as Space>::Decomp>: Instance<X, D> |
| 84 | where | |
| 85 | D: Decomposition<X>, | |
| 86 | { | |
| 87 | type RefInstance<'b>: Instance<X, D> + Copy | |
| 88 | where | |
| 89 | Self: 'b; | |
| 90 | ||
| 91 | fn ref_inst(&self) -> Self::RefInstance<'_>; | |
| 92 | } | |
| 93 | ||
| 94 | impl<X: Space> Instantiated<X, BasicDecomposition> for X | |
| 95 | where | |
| 96 | X: Instance<X, BasicDecomposition> + Clone, | |
| 97 | for<'b> &'b X: Instance<X, BasicDecomposition>, | |
| 98 | { | |
| 99 | type RefInstance<'b> | |
| 100 | = &'b X | |
| 101 | where | |
| 102 | Self: 'b; | |
| 103 | ||
| 104 | fn ref_inst(&self) -> Self::RefInstance<'_> { | |
| 105 | self | |
| 106 | } | |
| 107 | } | |
| 108 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
109 | /// Helper trait for functions to work with either owned values or references to either the |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
110 | /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
111 | /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
| 88 | 112 | /// |
| 113 | /// This is used, for example, by [`crate::mapping::Mapping::apply`]. | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
114 | pub trait Instance<X: Space, D = <X as Space>::Decomp>: Sized |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
115 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
116 | D: Decomposition<X>, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
117 | { |
| 137 | 118 | type Instantiated: Space + Instantiated<X, D>; |
| 119 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
120 | /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
121 | /// Consumes self. |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
122 | fn eval_decompose<'b, R>(self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
123 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
124 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
125 | Self: 'b; |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
126 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
127 | /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result. |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
128 | /// Does not consume self. |
| 136 | 129 | fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
130 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
131 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
132 | Self: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
133 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
134 | /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
135 | fn own(self) -> X; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
136 | |
| 137 | 137 | /// Instantiate to something that can be 'referenced'. |
| 138 | /// | |
| 139 | /// This is distinct from [`own`], as it is not guaranteed that `&'b X` is an | |
| 140 | /// [`Instance`] of `X`. | |
| 141 | fn instantiate(self) -> Self::Instantiated; | |
| 142 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
143 | // ************** automatically implemented methods below from here ************** |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
144 | |
| 137 | 145 | /// Evaluate `f` on the result of [`instantiate`]ing self, and then referencing. |
| 146 | fn eval_inst_ref<R>( | |
| 147 | self, | |
| 148 | f: impl for<'b> FnOnce(<Self::Instantiated as Instantiated<X, D>>::RefInstance<'b>) -> R, | |
| 149 | ) -> R { | |
| 150 | let inst = self.instantiate(); | |
| 151 | f(inst.ref_inst()) | |
| 152 | } | |
| 153 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
154 | /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
155 | /// |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
156 | /// Default implementation uses [`Self::own`]. Consumes the input. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
157 | fn cow<'b>(self) -> MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
158 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
159 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
160 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | MyCow::Owned(self.own()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
162 | } |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
163 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
164 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
165 | /// Evaluates `f` on a reference to self. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
166 | /// |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
167 | /// Default implementation uses [`Self::cow`]. Consumes the input. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
168 | fn eval<'b, R>(self, f: impl FnOnce(&X) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
169 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
170 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
171 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
172 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
173 | f(&*self.cow()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
174 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
175 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
176 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
177 | /// Evaluates `f` or `g` depending on whether a reference or owned value is available. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
178 | /// |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
179 | /// Default implementation uses [`Self::cow`]. Consumes the input. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
180 | fn either<'b, R>(self, f: impl FnOnce(X) -> R, g: impl FnOnce(&X) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
181 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
182 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
183 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
184 | match self.cow() { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
185 | EitherDecomp::Owned(x) => f(x), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
186 | EitherDecomp::Borrowed(x) => g(x), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
187 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
189 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
190 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
191 | impl<X: Space + Clone> Instance<X, BasicDecomposition> for X { |
| 137 | 192 | type Instantiated = X; |
| 193 | ||
| 194 | fn instantiate(self) -> Self::Instantiated { | |
| 195 | self | |
| 196 | } | |
| 197 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
198 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
199 | fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
200 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
201 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
202 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
203 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
204 | f(MyCow::Owned(self)) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
205 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
206 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
207 | #[inline] |
| 136 | 208 | fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
209 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
210 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
211 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
212 | { |
| 136 | 213 | f(MyCow::Borrowed(&self)) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | fn own(self) -> X { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
218 | self |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
219 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
220 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
221 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
222 | fn cow<'b>(self) -> MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
223 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
224 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
225 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
226 | MyCow::Owned(self) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
227 | } |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
228 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
229 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
230 | impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a X { |
| 137 | 231 | type Instantiated = X; |
| 232 | ||
| 233 | fn instantiate(self) -> Self::Instantiated { | |
| 234 | self.own() | |
| 235 | } | |
| 236 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
237 | #[inline] |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
238 | fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
239 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
240 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
241 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
242 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
243 | f(MyCow::Borrowed(self)) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
244 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
245 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
246 | #[inline] |
| 136 | 247 | fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
248 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
249 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
250 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
251 | { |
| 136 | 252 | f(MyCow::Borrowed(self)) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
253 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
254 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
255 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
256 | fn own(self) -> X { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
257 | self.clone() |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
258 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
259 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
260 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
261 | fn cow<'b>(self) -> MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
262 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
263 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
264 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
265 | MyCow::Borrowed(self) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
266 | } |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
267 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
268 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
269 | impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a mut X { |
| 137 | 270 | type Instantiated = X; |
| 271 | ||
| 272 | fn instantiate(self) -> Self::Instantiated { | |
| 273 | self.own() | |
| 274 | } | |
| 275 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
276 | #[inline] |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
277 | fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
278 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
279 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
280 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
281 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
282 | f(EitherDecomp::Borrowed(self)) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
283 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
284 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
285 | #[inline] |
| 136 | 286 | fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
287 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
288 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
289 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
290 | { |
| 136 | 291 | f(EitherDecomp::Borrowed(self)) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
292 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
293 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
294 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
295 | fn own(self) -> X { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
296 | self.clone() |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
297 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
298 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
299 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
300 | fn cow<'b>(self) -> MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
301 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
302 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
303 | X: Clone, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
304 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
305 | EitherDecomp::Borrowed(self) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
306 | } |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
307 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
308 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
309 | impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
| 137 | 310 | type Instantiated = X; |
| 311 | ||
| 312 | fn instantiate(self) -> Self::Instantiated { | |
| 313 | self.own() | |
| 314 | } | |
| 315 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
316 | #[inline] |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
317 | fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
318 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
319 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
320 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
321 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
322 | f(self) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
323 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
324 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
325 | #[inline] |
| 136 | 326 | fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
327 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
328 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
329 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
330 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
331 | match self { |
| 136 | 332 | MyCow::Borrowed(a) => f(MyCow::Borrowed(a)), |
| 333 | MyCow::Owned(b) => f(MyCow::Borrowed(&b)), | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
334 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
335 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
336 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
337 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
338 | fn own(self) -> X { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
339 | match self { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
340 | MyCow::Borrowed(a) => a.own(), |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
341 | MyCow::Owned(b) => b.own(), |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
342 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
343 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
344 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
345 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
346 | fn cow<'b>(self) -> MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
347 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
348 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
349 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
350 | match self { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
351 | MyCow::Borrowed(a) => a.cow(), |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
352 | MyCow::Owned(b) => b.cow(), |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
353 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
354 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
355 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
356 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
357 | /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
358 | pub trait DecompositionMut<X: Space>: Sized { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
359 | type ReferenceMut<'b>: InstanceMut<X, Self> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
360 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
361 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
362 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
363 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
364 | /// Helper trait for functions to work with mutable references. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
365 | pub trait InstanceMut<X: Space, D = <X as Space>::Decomp>: Sized |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
366 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
367 | D: DecompositionMut<X>, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
368 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
369 | /// Returns a mutable decomposition of self. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
370 | fn ref_instance_mut(&mut self) -> D::ReferenceMut<'_>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
371 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
372 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
373 | impl<X: Space> DecompositionMut<X> for BasicDecomposition { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
374 | type ReferenceMut<'b> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
375 | = &'b mut X |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
376 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
377 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
378 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
379 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
380 | /// This impl may seem pointless, but allows throwaway mutable scratch variables |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
381 | impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for X { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
382 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
383 | fn ref_instance_mut( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
384 | &mut self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
385 | ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
386 | self |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
387 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
388 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
389 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
390 | impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for &'a mut X { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
391 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
392 | fn ref_instance_mut( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
393 | &mut self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
394 | ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
395 | self |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
396 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
397 | } |