| 319 /// Helper trait for functions to work with either owned values or references to either the |
319 /// Helper trait for functions to work with either owned values or references to either the |
| 320 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
320 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
| 321 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
321 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
| 322 /// |
322 /// |
| 323 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
323 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
| 324 pub trait Instance<X, D = <X as Space>::Decomp>: |
324 pub trait Instance<X, D = <X as Space>::Decomp>: Sized |
| 325 Sized + Ownable<OwnedVariant = X::Principal> |
|
| 326 where |
325 where |
| 327 X: Space, |
326 X: Space, |
| 328 D: Decomposition<X>, |
327 D: Decomposition<X>, |
| 329 { |
328 { |
| 330 /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
329 /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
| 340 where |
339 where |
| 341 X: 'b, |
340 X: 'b, |
| 342 Self: 'b; |
341 Self: 'b; |
| 343 |
342 |
| 344 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
343 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| 345 fn own(self) -> X::Principal { |
344 fn own(self) -> X::Principal; |
| 346 self.into_owned() |
|
| 347 } |
|
| 348 |
|
| 349 // ************** automatically implemented methods below from here ************** |
|
| 350 |
345 |
| 351 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
346 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
| 352 /// |
347 /// |
| 353 /// Default implementation uses [`Self::own`]. Consumes the input. |
348 /// Default implementation uses [`Self::own`]. Consumes the input. |
| 354 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
349 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| 355 where |
350 where |
| 356 Self: 'b, |
351 Self: 'b; |
| 357 { |
|
| 358 self.cow_owned() |
|
| 359 } |
|
| 360 |
352 |
| 361 #[inline] |
353 #[inline] |
| 362 /// Evaluates `f` on a reference to self. |
354 /// Evaluates `f` on a reference to self. |
| 363 /// |
355 /// |
| 364 /// Default implementation uses [`Self::cow`]. Consumes the input. |
356 /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 405 X: 'b, |
397 X: 'b, |
| 406 Self: 'b, |
398 Self: 'b, |
| 407 { |
399 { |
| 408 f(self) |
400 f(self) |
| 409 } |
401 } |
| |
402 |
| |
403 #[inline] |
| |
404 fn own(self) -> X::Principal { |
| |
405 self.into_owned() |
| |
406 } |
| |
407 |
| |
408 #[inline] |
| |
409 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
410 where |
| |
411 Self: 'b, |
| |
412 { |
| |
413 self.cow_owned() |
| |
414 } |
| 410 } |
415 } |
| 411 |
416 |
| 412 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a X { |
417 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a X { |
| 413 #[inline] |
418 #[inline] |
| 414 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
419 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 425 X: 'b, |
430 X: 'b, |
| 426 Self: 'b, |
431 Self: 'b, |
| 427 { |
432 { |
| 428 f(*self) |
433 f(*self) |
| 429 } |
434 } |
| |
435 |
| |
436 #[inline] |
| |
437 fn own(self) -> X::Principal { |
| |
438 self.into_owned() |
| |
439 } |
| |
440 |
| |
441 #[inline] |
| |
442 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
443 where |
| |
444 Self: 'b, |
| |
445 { |
| |
446 self.cow_owned() |
| |
447 } |
| 430 } |
448 } |
| 431 |
449 |
| 432 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a mut X { |
450 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a mut X { |
| 433 #[inline] |
451 #[inline] |
| 434 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
452 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R |
| 467 { |
498 { |
| 468 match self { |
499 match self { |
| 469 MyCow::Borrowed(a) => f(a), |
500 MyCow::Borrowed(a) => f(a), |
| 470 MyCow::Owned(b) => f(&b), |
501 MyCow::Owned(b) => f(&b), |
| 471 } |
502 } |
| |
503 } |
| |
504 |
| |
505 #[inline] |
| |
506 fn own(self) -> X::Principal { |
| |
507 self.into_owned() |
| |
508 } |
| |
509 |
| |
510 #[inline] |
| |
511 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
512 where |
| |
513 Self: 'b, |
| |
514 { |
| |
515 self.cow_owned() |
| 472 } |
516 } |
| 473 } |
517 } |
| 474 |
518 |
| 475 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
519 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
| 476 pub trait DecompositionMut<X: Space>: Sized { |
520 pub trait DecompositionMut<X: Space>: Sized { |