| 21 EitherDecomp::Borrowed(x) => x, |
21 EitherDecomp::Borrowed(x) => x, |
| 22 } |
22 } |
| 23 } |
23 } |
| 24 } |
24 } |
| 25 |
25 |
| 26 impl<'b, X> MyCow<'b, X> { |
26 /// Trait for ownable-by-consumption objects |
| 27 #[inline] |
27 pub trait Ownable { |
| 28 pub fn into_owned(self) -> X where X : Clone { |
28 type OwnedVariant: Clone; |
| |
29 |
| |
30 /// Returns an owned instance, possibly consuming the original, |
| |
31 /// avoiding cloning when possible. |
| |
32 fn into_owned(self) -> Self::OwnedVariant; |
| |
33 |
| |
34 /// Returns an owned instance of a reference. |
| |
35 fn clone_owned(&self) -> Self::OwnedVariant; |
| |
36 |
| |
37 /// Returns an owned instance or a reference to one. |
| |
38 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
39 where |
| |
40 Self: 'b; |
| |
41 |
| |
42 /// Returns an owned instance or a reference to one. |
| |
43 fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant> |
| |
44 where |
| |
45 Self: 'b; |
| |
46 } |
| |
47 |
| |
48 impl<'a, X: Ownable> Ownable for &'a X { |
| |
49 type OwnedVariant = X::OwnedVariant; |
| |
50 |
| |
51 #[inline] |
| |
52 fn into_owned(self) -> Self::OwnedVariant { |
| |
53 X::clone_owned(self) |
| |
54 } |
| |
55 |
| |
56 #[inline] |
| |
57 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
58 X::clone_owned(self) |
| |
59 } |
| |
60 |
| |
61 #[inline] |
| |
62 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
63 where |
| |
64 Self: 'b, |
| |
65 { |
| |
66 X::ref_cow_owned(self) |
| |
67 } |
| |
68 |
| |
69 fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant> |
| |
70 where |
| |
71 Self: 'b, |
| |
72 { |
| |
73 X::ref_cow_owned(self) |
| |
74 } |
| |
75 } |
| |
76 |
| |
77 impl<'a, X: Ownable> Ownable for &'a mut X { |
| |
78 type OwnedVariant = X::OwnedVariant; |
| |
79 |
| |
80 #[inline] |
| |
81 fn into_owned(self) -> Self::OwnedVariant { |
| |
82 X::clone_owned(self) |
| |
83 } |
| |
84 |
| |
85 #[inline] |
| |
86 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
87 X::clone_owned(self) |
| |
88 } |
| |
89 |
| |
90 #[inline] |
| |
91 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
92 where |
| |
93 Self: 'b, |
| |
94 { |
| |
95 X::ref_cow_owned(self) |
| |
96 } |
| |
97 |
| |
98 fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant> |
| |
99 where |
| |
100 Self: 'b, |
| |
101 { |
| |
102 X::ref_cow_owned(self) |
| |
103 } |
| |
104 } |
| |
105 |
| |
106 impl<'a, A, B> Ownable for EitherDecomp<A, B> |
| |
107 where |
| |
108 A: Ownable, |
| |
109 B: Ownable<OwnedVariant = A::OwnedVariant>, |
| |
110 { |
| |
111 type OwnedVariant = A::OwnedVariant; |
| |
112 |
| |
113 #[inline] |
| |
114 fn into_owned(self) -> Self::OwnedVariant { |
| 29 match self { |
115 match self { |
| 30 EitherDecomp::Owned(x) => x, |
116 EitherDecomp::Owned(a) => A::into_owned(a), |
| 31 EitherDecomp::Borrowed(x) => x.clone(), |
117 EitherDecomp::Borrowed(b) => B::into_owned(b), |
| 32 } |
118 } |
| |
119 } |
| |
120 |
| |
121 #[inline] |
| |
122 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
123 match self { |
| |
124 EitherDecomp::Owned(a) => A::clone_owned(a), |
| |
125 EitherDecomp::Borrowed(b) => B::clone_owned(b), |
| |
126 } |
| |
127 } |
| |
128 |
| |
129 #[inline] |
| |
130 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
131 where |
| |
132 A: 'b, |
| |
133 B: 'b, |
| |
134 { |
| |
135 match self { |
| |
136 EitherDecomp::Owned(a) => A::cow_owned(a), |
| |
137 EitherDecomp::Borrowed(b) => B::cow_owned(b), |
| |
138 } |
| |
139 } |
| |
140 |
| |
141 #[inline] |
| |
142 fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant> |
| |
143 where |
| |
144 Self: 'b, |
| |
145 { |
| |
146 match self { |
| |
147 EitherDecomp::Owned(a) => A::ref_cow_owned(a), |
| |
148 EitherDecomp::Borrowed(b) => B::ref_cow_owned(b), |
| |
149 } |
| |
150 } |
| |
151 } |
| |
152 |
| |
153 #[macro_export] |
| |
154 macro_rules! self_ownable { |
| |
155 ($type:ty where $($qual:tt)*) => { |
| |
156 impl<$($qual)*> $crate::instance::Ownable for $type { |
| |
157 type OwnedVariant = Self; |
| |
158 |
| |
159 #[inline] |
| |
160 fn into_owned(self) -> Self::OwnedVariant { |
| |
161 self |
| |
162 } |
| |
163 |
| |
164 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
165 self.clone() |
| |
166 } |
| |
167 |
| |
168 fn cow_owned<'b>(self) -> $crate::instance::MyCow<'b, Self::OwnedVariant> |
| |
169 where |
| |
170 Self: 'b, |
| |
171 { |
| |
172 $crate::instance::MyCow::Owned(self) |
| |
173 } |
| |
174 |
| |
175 fn ref_cow_owned<'b>(&'b self) -> $crate::instance::MyCow<'b, Self::OwnedVariant> |
| |
176 where |
| |
177 Self: 'b, |
| |
178 { |
| |
179 $crate::instance::MyCow::Borrowed(self) |
| |
180 } |
| |
181 } |
| |
182 }; |
| |
183 } |
| |
184 |
| |
185 self_ownable!(Vec<T> where T : Clone); |
| |
186 |
| |
187 impl<'a, T: Clone> Ownable for &'a [T] { |
| |
188 type OwnedVariant = Vec<T>; |
| |
189 |
| |
190 #[inline] |
| |
191 fn into_owned(self) -> Self::OwnedVariant { |
| |
192 Vec::from(self) |
| |
193 } |
| |
194 |
| |
195 #[inline] |
| |
196 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
197 Vec::from(*self) |
| |
198 } |
| |
199 |
| |
200 #[inline] |
| |
201 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
202 where |
| |
203 Self: 'b, |
| |
204 { |
| |
205 MyCow::Owned(Vec::from(self)) |
| |
206 } |
| |
207 |
| |
208 fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant> |
| |
209 where |
| |
210 Self: 'b, |
| |
211 { |
| |
212 MyCow::Owned(Vec::from(*self)) |
| 33 } |
213 } |
| 34 } |
214 } |
| 35 |
215 |
| 36 /// Trait for abitrary mathematical spaces. |
216 /// Trait for abitrary mathematical spaces. |
| 37 pub trait Space : Instance<Self, Self::Decomp> { |
217 pub trait Space: Ownable<OwnedVariant = Self::Principal> + Sized { |
| |
218 /// Principal, typically owned realisation of the space. |
| |
219 type Principal: ClosedSpace; |
| |
220 |
| 38 /// Default decomposition for the space |
221 /// Default decomposition for the space |
| 39 type Decomp : Decomposition<Self>; |
222 type Decomp: Decomposition<Self>; |
| 40 } |
223 } |
| |
224 |
| |
225 mod private { |
| |
226 pub trait Sealed {} |
| |
227 } |
| |
228 |
| |
229 /// Helper trait for working with own types. |
| |
230 pub trait Owned: Ownable<OwnedVariant = Self> + private::Sealed {} |
| |
231 impl<X: Ownable<OwnedVariant = X>> private::Sealed for X {} |
| |
232 impl<X: Ownable<OwnedVariant = X>> Owned for X {} |
| |
233 |
| |
234 /// Helper trait for working with closed spaces, operations in which should |
| |
235 /// return members of the same space |
| |
236 pub trait ClosedSpace: Space<Principal = Self> + Owned + Instance<Self> {} |
| |
237 impl<X: Space<Principal = Self> + Owned + Instance<Self>> ClosedSpace for X {} |
| 41 |
238 |
| 42 #[macro_export] |
239 #[macro_export] |
| 43 macro_rules! impl_basic_space { |
240 macro_rules! impl_basic_space { |
| 44 ($($type:ty)*) => { $( |
241 ($($type:ty)*) => { |
| 45 impl $crate::instance::Space for $type { |
242 $( $crate::impl_basic_space!($type where ); )* |
| 46 type Decomp = $crate::instance::BasicDecomposition; |
243 }; |
| 47 } |
|
| 48 )* }; |
|
| 49 ($type:ty where $($where:tt)*) => { |
244 ($type:ty where $($where:tt)*) => { |
| 50 impl<$($where)*> $crate::instance::Space for $type { |
245 impl<$($where)*> $crate::instance::Space for $type { |
| |
246 type Principal = Self; |
| 51 type Decomp = $crate::instance::BasicDecomposition; |
247 type Decomp = $crate::instance::BasicDecomposition; |
| |
248 } |
| |
249 |
| |
250 impl<$($where)*> $crate::instance::Ownable for $type { |
| |
251 type OwnedVariant = Self; |
| |
252 |
| |
253 #[inline] |
| |
254 fn into_owned(self) -> Self::OwnedVariant { |
| |
255 self |
| |
256 } |
| |
257 |
| |
258 #[inline] |
| |
259 fn clone_owned(&self) -> Self::OwnedVariant { |
| |
260 *self |
| |
261 } |
| |
262 |
| |
263 #[inline] |
| |
264 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { |
| |
265 MyCow::Owned(self) |
| |
266 } |
| |
267 |
| |
268 #[inline] |
| |
269 fn ref_cow_owned<'b>(&self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { |
| |
270 MyCow::Owned(*self) |
| |
271 } |
| 52 } |
272 } |
| 53 }; |
273 }; |
| 54 } |
274 } |
| 55 |
275 |
| 56 impl_basic_space!(u8 u16 u32 u64 u128 usize |
276 impl_basic_space!(u8 u16 u32 u64 u128 usize |
| 57 i8 i16 i32 i64 i128 isize |
277 i8 i16 i32 i64 i128 isize |
| 58 f32 f64); |
278 f32 f64); |
| 59 |
279 |
| 60 /// Marker type for decompositions to be used with [`Instance`]. |
280 /// Marker type for decompositions to be used with [`Instance`]. |
| 61 pub trait Decomposition<X : Space> : Sized { |
281 pub trait Decomposition<X: Space>: Sized { |
| 62 /// Possibly owned form of the decomposition |
282 /// Possibly owned form of the decomposition |
| 63 type Decomposition<'b> : Instance<X, Self> where X : 'b; |
283 type Decomposition<'b>: Instance<X, Self> |
| |
284 where |
| |
285 X: 'b; |
| 64 /// Unlikely owned form of the decomposition. |
286 /// Unlikely owned form of the decomposition. |
| 65 /// Type for a lightweight intermediate conversion that does not own the original variable. |
287 /// Type for a lightweight intermediate conversion that does not own the original variable. |
| 66 /// Usually this is just a reference, but may also be a lightweight structure that |
288 /// Usually this is just a reference, but may also be a lightweight structure that |
| 67 /// contains references; see the implementation for [`crate::direct_product::Pair`]. |
289 /// contains references; see the implementation for [`crate::direct_product::Pair`]. |
| 68 type Reference<'b> : Instance<X, Self> + Copy where X : 'b; |
290 type Reference<'b>: Instance<X, Self> + Copy |
| 69 |
291 where |
| 70 /// Left the lightweight reference type into a full decomposition type. |
292 X: 'b; |
| 71 fn lift<'b>(r : Self::Reference<'b>) -> Self::Decomposition<'b>; |
293 |
| |
294 /// Lift the lightweight reference type into a full decomposition type. |
| |
295 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; |
| 72 } |
296 } |
| 73 |
297 |
| 74 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
298 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
| 75 /// values and all sorts of references. |
299 /// values and all sorts of references. |
| 76 #[derive(Copy, Clone, Debug)] |
300 #[derive(Copy, Clone, Debug)] |
| 77 pub struct BasicDecomposition; |
301 pub struct BasicDecomposition; |
| 78 |
302 |
| 79 impl<X : Space + Clone> Decomposition<X> for BasicDecomposition { |
303 impl<X: Space> Decomposition<X> for BasicDecomposition { |
| 80 type Decomposition<'b> = MyCow<'b, X> where X : 'b; |
304 type Decomposition<'b> |
| 81 type Reference<'b> = &'b X where X : 'b; |
305 = MyCow<'b, X> |
| 82 |
306 where |
| 83 #[inline] |
307 X: 'b; |
| 84 fn lift<'b>(r : Self::Reference<'b>) -> Self::Decomposition<'b> { |
308 type Reference<'b> |
| |
309 = &'b X |
| |
310 where |
| |
311 X: 'b; |
| |
312 |
| |
313 #[inline] |
| |
314 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> { |
| 85 MyCow::Borrowed(r) |
315 MyCow::Borrowed(r) |
| 86 } |
316 } |
| 87 } |
317 } |
| 88 |
318 |
| 89 /// 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 |
| 90 /// “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 |
| 91 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
321 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
| 92 /// |
322 /// |
| 93 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
323 /// This is used, for example, by [`crate::mapping::Mapping::apply`]. |
| 94 pub trait Instance<X : Space, D = <X as Space>::Decomp> : Sized where D : Decomposition<X> { |
324 pub trait Instance<X, D = <X as Space>::Decomp>: Sized |
| 95 /// Decomposes self according to `decomposer`. |
325 where |
| |
326 X: Space, |
| |
327 D: Decomposition<X>, |
| |
328 { |
| |
329 /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
| |
330 /// Consumes self. |
| |
331 #[inline] |
| |
332 fn eval_decompose<'b, R>(self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
| |
333 where |
| |
334 X: 'b, |
| |
335 Self: 'b, |
| |
336 { |
| |
337 f(self.decompose()) |
| |
338 } |
| |
339 |
| |
340 /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result. |
| |
341 /// Does not consume self. |
| |
342 fn eval_ref<'b, R>(&'b self, f: impl FnOnce(D::Reference<'b>) -> R) -> R |
| |
343 where |
| |
344 X: 'b, |
| |
345 Self: 'b; |
| |
346 |
| |
347 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| |
348 fn own(self) -> X::Principal; |
| |
349 |
| 96 fn decompose<'b>(self) -> D::Decomposition<'b> |
350 fn decompose<'b>(self) -> D::Decomposition<'b> |
| 97 where Self : 'b, X : 'b; |
351 where |
| 98 |
352 Self: 'b; |
| 99 /// Returns a lightweight instance of `self`. |
|
| 100 fn ref_instance(&self) -> D::Reference<'_>; |
|
| 101 |
|
| 102 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
|
| 103 fn own(self) -> X; |
|
| 104 |
|
| 105 // ************** automatically implemented methods below from here ************** |
|
| 106 |
353 |
| 107 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
354 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
| 108 /// |
355 /// |
| 109 /// Default implementation uses [`Self::own`]. Consumes the input. |
356 /// Default implementation uses [`Self::own`]. Consumes the input. |
| 110 fn cow<'b>(self) -> MyCow<'b, X> where Self : 'b { |
357 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| 111 MyCow::Owned(self.own()) |
358 where |
| 112 } |
359 Self: 'b; |
| 113 |
360 |
| 114 #[inline] |
361 #[inline] |
| 115 /// Evaluates `f` on a reference to self. |
362 /// Evaluates `f` on a reference to self. |
| 116 /// |
363 /// |
| 117 /// Default implementation uses [`Self::cow`]. Consumes the input. |
364 /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 118 fn eval<'b, R>(self, f : impl FnOnce(&X) -> R) -> R |
365 fn eval<'b, R>(self, f: impl FnOnce(&X::Principal) -> R) -> R |
| 119 where X : 'b, Self : 'b |
366 where |
| |
367 X: 'b, |
| |
368 Self: 'b, |
| 120 { |
369 { |
| 121 f(&*self.cow()) |
370 f(&*self.cow()) |
| 122 } |
371 } |
| 123 |
372 } |
| 124 #[inline] |
373 |
| 125 /// Evaluates `f` or `g` depending on whether a reference or owned value is available. |
374 impl<X: Space> Instance<X, BasicDecomposition> for X { |
| 126 /// |
375 #[inline] |
| 127 /// Default implementation uses [`Self::cow`]. Consumes the input. |
376 fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
| 128 fn either<'b, R>( |
377 where |
| 129 self, |
378 X: 'b, |
| 130 f : impl FnOnce(X) -> R, |
379 Self: 'b, |
| 131 g : impl FnOnce(&X) -> R |
380 { |
| 132 ) -> R |
381 f(self) |
| 133 where Self : 'b |
382 } |
| 134 { |
383 |
| 135 match self.cow() { |
384 #[inline] |
| 136 EitherDecomp::Owned(x) => f(x), |
385 fn own(self) -> X::Principal { |
| 137 EitherDecomp::Borrowed(x) => g(x), |
386 self.into_owned() |
| 138 } |
387 } |
| 139 } |
388 |
| 140 } |
389 #[inline] |
| 141 |
390 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| 142 |
391 where |
| 143 impl<X : Space + Clone> Instance<X, BasicDecomposition> for X { |
392 Self: 'b, |
| 144 #[inline] |
393 { |
| 145 fn decompose<'b>(self) -> <BasicDecomposition as Decomposition<X>>::Decomposition<'b> |
394 self.cow_owned() |
| 146 where Self : 'b, X : 'b |
395 } |
| |
396 |
| |
397 #[inline] |
| |
398 fn decompose<'b>(self) -> MyCow<'b, X> |
| |
399 where |
| |
400 Self: 'b, |
| 147 { |
401 { |
| 148 MyCow::Owned(self) |
402 MyCow::Owned(self) |
| 149 } |
403 } |
| 150 |
404 } |
| 151 #[inline] |
405 |
| 152 fn own(self) -> X { |
406 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a X { |
| |
407 #[inline] |
| |
408 fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
| |
409 where |
| |
410 X: 'b, |
| |
411 Self: 'b, |
| |
412 { |
| |
413 f(*self) |
| |
414 } |
| |
415 |
| |
416 #[inline] |
| |
417 fn own(self) -> X::Principal { |
| |
418 self.into_owned() |
| |
419 } |
| |
420 |
| |
421 #[inline] |
| |
422 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
423 where |
| |
424 Self: 'b, |
| |
425 { |
| |
426 self.cow_owned() |
| |
427 } |
| |
428 |
| |
429 #[inline] |
| |
430 fn decompose<'b>(self) -> MyCow<'b, X> |
| |
431 where |
| |
432 Self: 'b, |
| |
433 { |
| |
434 MyCow::Borrowed(self) |
| |
435 } |
| |
436 } |
| |
437 |
| |
438 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a mut X { |
| |
439 #[inline] |
| |
440 fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
| |
441 where |
| |
442 X: 'b, |
| |
443 Self: 'b, |
| |
444 { |
| |
445 f(*self) |
| |
446 } |
| |
447 |
| |
448 #[inline] |
| |
449 fn own(self) -> X::Principal { |
| |
450 self.into_owned() |
| |
451 } |
| |
452 |
| |
453 #[inline] |
| |
454 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
455 where |
| |
456 Self: 'b, |
| |
457 { |
| |
458 self.cow_owned() |
| |
459 } |
| |
460 |
| |
461 #[inline] |
| |
462 fn decompose<'b>(self) -> MyCow<'b, X> |
| |
463 where |
| |
464 Self: 'b, |
| |
465 { |
| |
466 MyCow::Borrowed(self) |
| |
467 } |
| |
468 } |
| |
469 |
| |
470 impl<'a, X: Space> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
| |
471 #[inline] |
| |
472 fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
| |
473 where |
| |
474 X: 'b, |
| |
475 Self: 'b, |
| |
476 { |
| |
477 match self { |
| |
478 MyCow::Borrowed(a) => f(a), |
| |
479 MyCow::Owned(b) => f(&b), |
| |
480 } |
| |
481 } |
| |
482 |
| |
483 #[inline] |
| |
484 fn own(self) -> X::Principal { |
| |
485 self.into_owned() |
| |
486 } |
| |
487 |
| |
488 #[inline] |
| |
489 fn cow<'b>(self) -> MyCow<'b, X::Principal> |
| |
490 where |
| |
491 Self: 'b, |
| |
492 { |
| |
493 self.cow_owned() |
| |
494 } |
| |
495 |
| |
496 #[inline] |
| |
497 fn decompose<'b>(self) -> MyCow<'b, X> |
| |
498 where |
| |
499 Self: 'b, |
| |
500 { |
| 153 self |
501 self |
| 154 } |
502 } |
| 155 |
|
| 156 #[inline] |
|
| 157 fn cow<'b>(self) -> MyCow<'b, X> where Self : 'b { |
|
| 158 MyCow::Owned(self) |
|
| 159 } |
|
| 160 |
|
| 161 #[inline] |
|
| 162 fn ref_instance(&self) -> <BasicDecomposition as Decomposition<X>>::Reference<'_> { |
|
| 163 self |
|
| 164 } |
|
| 165 } |
|
| 166 |
|
| 167 impl<'a, X : Space + Clone> Instance<X, BasicDecomposition> for &'a X { |
|
| 168 #[inline] |
|
| 169 fn decompose<'b>(self) -> <BasicDecomposition as Decomposition<X>>::Decomposition<'b> |
|
| 170 where Self : 'b, X : 'b |
|
| 171 { |
|
| 172 MyCow::Borrowed(self) |
|
| 173 } |
|
| 174 |
|
| 175 #[inline] |
|
| 176 fn own(self) -> X { |
|
| 177 self.clone() |
|
| 178 } |
|
| 179 |
|
| 180 #[inline] |
|
| 181 fn cow<'b>(self) -> MyCow<'b, X> where Self : 'b { |
|
| 182 MyCow::Borrowed(self) |
|
| 183 } |
|
| 184 |
|
| 185 #[inline] |
|
| 186 fn ref_instance(&self) -> <BasicDecomposition as Decomposition<X>>::Reference<'_> { |
|
| 187 *self |
|
| 188 } |
|
| 189 } |
|
| 190 |
|
| 191 impl<'a, X : Space + Clone> Instance<X, BasicDecomposition> for &'a mut X { |
|
| 192 #[inline] |
|
| 193 fn decompose<'b>(self) -> <BasicDecomposition as Decomposition<X>>::Decomposition<'b> |
|
| 194 where Self : 'b, X : 'b |
|
| 195 { |
|
| 196 EitherDecomp::Borrowed(self) |
|
| 197 } |
|
| 198 |
|
| 199 #[inline] |
|
| 200 fn own(self) -> X { |
|
| 201 self.clone() |
|
| 202 } |
|
| 203 |
|
| 204 #[inline] |
|
| 205 fn cow<'b>(self) -> MyCow<'b, X> where Self : 'b, X : Clone { |
|
| 206 EitherDecomp::Borrowed(self) |
|
| 207 } |
|
| 208 |
|
| 209 #[inline] |
|
| 210 fn ref_instance(&self) -> <BasicDecomposition as Decomposition<X>>::Reference<'_> { |
|
| 211 *self |
|
| 212 } |
|
| 213 } |
|
| 214 |
|
| 215 impl<'a, X : Space + Clone> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
|
| 216 |
|
| 217 #[inline] |
|
| 218 fn decompose<'b>(self) -> <BasicDecomposition as Decomposition<X>>::Decomposition<'b> |
|
| 219 where Self : 'b, X : 'b |
|
| 220 { |
|
| 221 self |
|
| 222 } |
|
| 223 |
|
| 224 #[inline] |
|
| 225 fn own(self) -> X { |
|
| 226 match self { |
|
| 227 MyCow::Borrowed(a) => a.own(), |
|
| 228 MyCow::Owned(b) => b.own() |
|
| 229 } |
|
| 230 } |
|
| 231 |
|
| 232 #[inline] |
|
| 233 fn cow<'b>(self) -> MyCow<'b, X> where Self : 'b { |
|
| 234 match self { |
|
| 235 MyCow::Borrowed(a) => a.cow(), |
|
| 236 MyCow::Owned(b) => b.cow() |
|
| 237 } |
|
| 238 } |
|
| 239 |
|
| 240 #[inline] |
|
| 241 fn ref_instance(&self) -> <BasicDecomposition as Decomposition<X>>::Reference<'_> { |
|
| 242 match self { |
|
| 243 MyCow::Borrowed(a) => a, |
|
| 244 MyCow::Owned(b) => &b, |
|
| 245 } |
|
| 246 } |
|
| 247 } |
503 } |
| 248 |
504 |
| 249 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
505 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
| 250 pub trait DecompositionMut<X : Space> : Sized { |
506 pub trait DecompositionMut<X: Space>: Sized { |
| 251 type ReferenceMut<'b> : InstanceMut<X, Self> where X : 'b; |
507 type ReferenceMut<'b>: InstanceMut<X, Self> |
| 252 } |
508 where |
| 253 |
509 X: 'b; |
| |
510 } |
| 254 |
511 |
| 255 /// Helper trait for functions to work with mutable references. |
512 /// Helper trait for functions to work with mutable references. |
| 256 pub trait InstanceMut<X : Space , D = <X as Space>::Decomp> : Sized where D : DecompositionMut<X> { |
513 pub trait InstanceMut<X: Space, D = <X as Space>::Decomp>: Sized |
| |
514 where |
| |
515 D: DecompositionMut<X>, |
| |
516 { |
| 257 /// Returns a mutable decomposition of self. |
517 /// Returns a mutable decomposition of self. |
| 258 fn ref_instance_mut(&mut self) -> D::ReferenceMut<'_>; |
518 fn ref_instance_mut(&mut self) -> D::ReferenceMut<'_>; |
| 259 } |
519 } |
| 260 |
520 |
| 261 impl<X : Space> DecompositionMut<X> for BasicDecomposition { |
521 impl<X: Space> DecompositionMut<X> for BasicDecomposition { |
| 262 type ReferenceMut<'b> = &'b mut X where X : 'b; |
522 type ReferenceMut<'b> |
| |
523 = &'b mut X |
| |
524 where |
| |
525 X: 'b; |
| 263 } |
526 } |
| 264 |
527 |
| 265 /// This impl may seem pointless, but allows throwaway mutable scratch variables |
528 /// This impl may seem pointless, but allows throwaway mutable scratch variables |
| 266 impl<'a, X : Space> InstanceMut<X, BasicDecomposition> for X { |
529 impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for X { |
| 267 #[inline] |
530 #[inline] |
| 268 fn ref_instance_mut(&mut self) |
531 fn ref_instance_mut( |
| 269 -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> |
532 &mut self, |
| 270 { |
533 ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
| 271 self |
534 self |
| 272 } |
535 } |
| 273 } |
536 } |
| 274 |
537 |
| 275 impl<'a, X : Space> InstanceMut<X, BasicDecomposition> for &'a mut X { |
538 impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for &'a mut X { |
| 276 #[inline] |
539 #[inline] |
| 277 fn ref_instance_mut(&mut self) |
540 fn ref_instance_mut( |
| 278 -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> |
541 &mut self, |
| 279 { |
542 ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
| 280 self |
543 self |
| 281 } |
544 } |
| 282 } |
545 } |