| 142 i8 i16 i32 i64 i128 isize |
142 i8 i16 i32 i64 i128 isize |
| 143 f32 f64); |
143 f32 f64); |
| 144 |
144 |
| 145 /// Marker type for decompositions to be used with [`Instance`]. |
145 /// Marker type for decompositions to be used with [`Instance`]. |
| 146 pub trait Decomposition<X: Space>: Sized { |
146 pub trait Decomposition<X: Space>: Sized { |
| |
147 /// Owned instance |
| |
148 type OwnedInstance: Instance<X, Self>; |
| |
149 |
| 147 /// Possibly owned form of the decomposition |
150 /// Possibly owned form of the decomposition |
| 148 type Decomposition<'b>: Instance<X, Self> |
151 type Decomposition<'b>: Instance<X, Self> |
| 149 where |
152 where |
| 150 X: 'b; |
153 X: 'b; |
| 151 /// Unlikely owned form of the decomposition. |
154 /// Unlikely owned form of the decomposition. |
| 154 /// contains references; see the implementation for [`crate::direct_product::Pair`]. |
157 /// contains references; see the implementation for [`crate::direct_product::Pair`]. |
| 155 type Reference<'b>: Instance<X, Self> + Copy |
158 type Reference<'b>: Instance<X, Self> + Copy |
| 156 where |
159 where |
| 157 X: 'b; |
160 X: 'b; |
| 158 |
161 |
| 159 /// Left the lightweight reference type into a full decomposition type. |
162 /// Lift the lightweight reference type into a full decomposition type. |
| 160 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; |
163 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; |
| |
164 |
| |
165 // /// Lift the lightweight reference type into a fully owned type |
| |
166 // fn full_lift<'b>(r: Self::Reference<'b>) -> Self::OwnedInstance; |
| 161 } |
167 } |
| 162 |
168 |
| 163 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
169 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
| 164 /// values and all sorts of references. |
170 /// values and all sorts of references. |
| 165 #[derive(Copy, Clone, Debug)] |
171 #[derive(Copy, Clone, Debug)] |
| 166 pub struct BasicDecomposition; |
172 pub struct BasicDecomposition; |
| 167 |
173 |
| 168 impl<X: Space> Decomposition<X> for BasicDecomposition { |
174 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
| |
175 type OwnedInstance = X; |
| |
176 |
| 169 type Decomposition<'b> |
177 type Decomposition<'b> |
| 170 = MyCow<'b, X> |
178 = MyCow<'b, X> |
| 171 where |
179 where |
| 172 X: 'b; |
180 X: 'b; |
| 173 type Reference<'b> |
181 type Reference<'b> |
| 205 where |
213 where |
| 206 X: 'b, |
214 X: 'b, |
| 207 Self: 'b; |
215 Self: 'b; |
| 208 |
216 |
| 209 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
217 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| 210 fn own(self) -> X; |
218 fn own(self) -> D::OwnedInstance; |
| 211 |
219 |
| 212 // ************** automatically implemented methods below from here ************** |
220 // ************** automatically implemented methods below from here ************** |
| 213 |
221 |
| 214 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
222 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
| 215 /// |
223 /// |
| 216 /// Default implementation uses [`Self::own`]. Consumes the input. |
224 /// Default implementation uses [`Self::own`]. Consumes the input. |
| 217 fn cow<'b>(self) -> MyCow<'b, X> |
225 fn cow<'b>(self) -> MyCow<'b, D::OwnedInstance> |
| 218 where |
226 where |
| 219 Self: 'b, |
227 Self: 'b, |
| 220 { |
228 { |
| 221 MyCow::Owned(self.own()) |
229 MyCow::Owned(self.own()) |
| 222 } |
230 } |
| 223 |
231 |
| 224 #[inline] |
232 #[inline] |
| 225 /// Evaluates `f` on a reference to self. |
233 /// Evaluates `f` on a reference to self. |
| 226 /// |
234 /// |
| 227 /// Default implementation uses [`Self::cow`]. Consumes the input. |
235 /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 228 fn eval<'b, R>(self, f: impl FnOnce(&X) -> R) -> R |
236 fn eval<'b, R>(self, f: impl FnOnce(&D::OwnedInstance) -> R) -> R |
| 229 where |
237 where |
| 230 X: 'b, |
238 X: 'b, |
| 231 Self: 'b, |
239 Self: 'b, |
| 232 { |
240 { |
| 233 f(&*self.cow()) |
241 f(&*self.cow()) |
| 235 |
243 |
| 236 #[inline] |
244 #[inline] |
| 237 /// Evaluates `f` or `g` depending on whether a reference or owned value is available. |
245 /// Evaluates `f` or `g` depending on whether a reference or owned value is available. |
| 238 /// |
246 /// |
| 239 /// Default implementation uses [`Self::cow`]. Consumes the input. |
247 /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 240 fn either<'b, R>(self, f: impl FnOnce(X) -> R, g: impl FnOnce(&X) -> R) -> R |
248 fn either<'b, R>( |
| |
249 self, |
| |
250 f: impl FnOnce(D::OwnedInstance) -> R, |
| |
251 g: impl FnOnce(&D::OwnedInstance) -> R, |
| |
252 ) -> R |
| 241 where |
253 where |
| 242 Self: 'b, |
254 Self: 'b, |
| 243 { |
255 { |
| 244 match self.cow() { |
256 match self.cow() { |
| 245 EitherDecomp::Owned(x) => f(x), |
257 EitherDecomp::Owned(x) => f(x), |
| 246 EitherDecomp::Borrowed(x) => g(x), |
258 EitherDecomp::Borrowed(x) => g(x), |
| 247 } |
259 } |
| 248 } |
260 } |
| 249 } |
261 } |
| 250 |
262 |
| 251 impl<X: Space> Instance<X, BasicDecomposition> for X { |
263 impl<X: Space + Clone> Instance<X, BasicDecomposition> for X { |
| 252 #[inline] |
264 #[inline] |
| 253 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
265 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 254 where |
266 where |
| 255 X: 'b, |
267 X: 'b, |
| 256 Self: 'b, |
268 Self: 'b, |
| 312 { |
324 { |
| 313 MyCow::Borrowed(self) |
325 MyCow::Borrowed(self) |
| 314 } |
326 } |
| 315 } |
327 } |
| 316 |
328 |
| 317 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a mut X { |
329 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a mut X { |
| 318 #[inline] |
330 #[inline] |
| 319 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
331 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 320 where |
332 where |
| 321 X: 'b, |
333 X: 'b, |
| 322 Self: 'b, |
334 Self: 'b, |
| 345 { |
357 { |
| 346 EitherDecomp::Borrowed(self) |
358 EitherDecomp::Borrowed(self) |
| 347 } |
359 } |
| 348 } |
360 } |
| 349 |
361 |
| 350 impl<'a, X: Space> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
362 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
| 351 #[inline] |
363 #[inline] |
| 352 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
364 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 353 where |
365 where |
| 354 X: 'b, |
366 X: 'b, |
| 355 Self: 'b, |
367 Self: 'b, |