| 76 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
76 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
| 77 type Decomposition<'b> |
77 type Decomposition<'b> |
| 78 = MyCow<'b, X> |
78 = MyCow<'b, X> |
| 79 where |
79 where |
| 80 X: 'b; |
80 X: 'b; |
| |
81 } |
| |
82 |
| |
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 } |
| 81 } |
107 } |
| 82 |
108 |
| 83 /// Helper trait for functions to work with either owned values or references to either the |
109 /// Helper trait for functions to work with either owned values or references to either the |
| 84 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
110 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
| 85 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
111 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
| 87 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
113 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
| 88 pub trait Instance<X: Space, D = <X as Space>::Decomp>: Sized |
114 pub trait Instance<X: Space, D = <X as Space>::Decomp>: Sized |
| 89 where |
115 where |
| 90 D: Decomposition<X>, |
116 D: Decomposition<X>, |
| 91 { |
117 { |
| |
118 type Instantiated: Space + Instantiated<X, D>; |
| |
119 |
| 92 /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
120 /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
| 93 /// Consumes self. |
121 /// Consumes self. |
| 94 fn eval_decompose<'b, R>(self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
122 fn eval_decompose<'b, R>(self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
| 95 where |
123 where |
| 96 X: 'b, |
124 X: 'b, |
| 104 Self: 'b; |
132 Self: 'b; |
| 105 |
133 |
| 106 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
134 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| 107 fn own(self) -> X; |
135 fn own(self) -> X; |
| 108 |
136 |
| |
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 |
| 109 // ************** automatically implemented methods below from here ************** |
143 // ************** automatically implemented methods below from here ************** |
| |
144 |
| |
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 } |
| 110 |
153 |
| 111 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
154 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
| 112 /// |
155 /// |
| 113 /// Default implementation uses [`Self::own`]. Consumes the input. |
156 /// Default implementation uses [`Self::own`]. Consumes the input. |
| 114 fn cow<'b>(self) -> MyCow<'b, X> |
157 fn cow<'b>(self) -> MyCow<'b, X> |
| 144 } |
187 } |
| 145 } |
188 } |
| 146 } |
189 } |
| 147 |
190 |
| 148 impl<X: Space + Clone> Instance<X, BasicDecomposition> for X { |
191 impl<X: Space + Clone> Instance<X, BasicDecomposition> for X { |
| |
192 type Instantiated = X; |
| |
193 |
| |
194 fn instantiate(self) -> Self::Instantiated { |
| |
195 self |
| |
196 } |
| |
197 |
| 149 #[inline] |
198 #[inline] |
| 150 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
199 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 151 where |
200 where |
| 152 X: 'b, |
201 X: 'b, |
| 153 Self: 'b, |
202 Self: 'b, |
| 177 MyCow::Owned(self) |
226 MyCow::Owned(self) |
| 178 } |
227 } |
| 179 } |
228 } |
| 180 |
229 |
| 181 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a X { |
230 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a X { |
| |
231 type Instantiated = X; |
| |
232 |
| |
233 fn instantiate(self) -> Self::Instantiated { |
| |
234 self.own() |
| |
235 } |
| |
236 |
| 182 #[inline] |
237 #[inline] |
| 183 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
238 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 184 where |
239 where |
| 185 X: 'b, |
240 X: 'b, |
| 186 Self: 'b, |
241 Self: 'b, |
| 210 MyCow::Borrowed(self) |
265 MyCow::Borrowed(self) |
| 211 } |
266 } |
| 212 } |
267 } |
| 213 |
268 |
| 214 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a mut X { |
269 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a mut X { |
| |
270 type Instantiated = X; |
| |
271 |
| |
272 fn instantiate(self) -> Self::Instantiated { |
| |
273 self.own() |
| |
274 } |
| |
275 |
| 215 #[inline] |
276 #[inline] |
| 216 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
277 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 217 where |
278 where |
| 218 X: 'b, |
279 X: 'b, |
| 219 Self: 'b, |
280 Self: 'b, |
| 244 EitherDecomp::Borrowed(self) |
305 EitherDecomp::Borrowed(self) |
| 245 } |
306 } |
| 246 } |
307 } |
| 247 |
308 |
| 248 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
309 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
| |
310 type Instantiated = X; |
| |
311 |
| |
312 fn instantiate(self) -> Self::Instantiated { |
| |
313 self.own() |
| |
314 } |
| |
315 |
| 249 #[inline] |
316 #[inline] |
| 250 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
317 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 251 where |
318 where |
| 252 X: 'b, |
319 X: 'b, |
| 253 Self: 'b, |
320 Self: 'b, |