| 31 /// avoiding cloning when possible. |
31 /// avoiding cloning when possible. |
| 32 fn into_owned(self) -> Self::OwnedVariant; |
32 fn into_owned(self) -> Self::OwnedVariant; |
| 33 |
33 |
| 34 /// Returns an owned instance of a reference. |
34 /// Returns an owned instance of a reference. |
| 35 fn clone_owned(&self) -> Self::OwnedVariant; |
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; |
| 36 } |
41 } |
| 37 |
42 |
| 38 impl<'a, X: Ownable> Ownable for &'a X { |
43 impl<'a, X: Ownable> Ownable for &'a X { |
| 39 type OwnedVariant = X::OwnedVariant; |
44 type OwnedVariant = X::OwnedVariant; |
| 40 |
45 |
| 41 #[inline] |
46 #[inline] |
| 42 /// Returns an owned instance. |
|
| 43 fn into_owned(self) -> Self::OwnedVariant { |
47 fn into_owned(self) -> Self::OwnedVariant { |
| 44 self.clone_owned() |
48 self.clone_owned() |
| 45 } |
49 } |
| 46 |
50 |
| 47 #[inline] |
51 #[inline] |
| 48 /// Returns an owned instance. |
|
| 49 fn clone_owned(&self) -> Self::OwnedVariant { |
52 fn clone_owned(&self) -> Self::OwnedVariant { |
| 50 (*self).into_owned() |
53 (*self).into_owned() |
| 51 } |
54 } |
| |
55 |
| |
56 #[inline] |
| |
57 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
58 where |
| |
59 Self: 'b, |
| |
60 { |
| |
61 todo!(); |
| |
62 } |
| 52 } |
63 } |
| 53 |
64 |
| 54 impl<'a, X: Ownable> Ownable for &'a mut X { |
65 impl<'a, X: Ownable> Ownable for &'a mut X { |
| 55 type OwnedVariant = X::OwnedVariant; |
66 type OwnedVariant = X::OwnedVariant; |
| 56 |
67 |
| 57 #[inline] |
68 #[inline] |
| 58 /// Returns an owned instance. |
|
| 59 fn into_owned(self) -> Self::OwnedVariant { |
69 fn into_owned(self) -> Self::OwnedVariant { |
| 60 self.clone_owned() |
70 self.clone_owned() |
| 61 } |
71 } |
| 62 |
72 |
| 63 #[inline] |
73 #[inline] |
| 64 /// Returns an owned instance. |
|
| 65 fn clone_owned(&self) -> Self::OwnedVariant { |
74 fn clone_owned(&self) -> Self::OwnedVariant { |
| 66 (&**self).into_owned() |
75 (&**self).into_owned() |
| 67 } |
76 } |
| |
77 |
| |
78 #[inline] |
| |
79 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
80 where |
| |
81 Self: 'b, |
| |
82 { |
| |
83 todo!(); |
| |
84 } |
| 68 } |
85 } |
| 69 |
86 |
| 70 impl<'a, X: Ownable> Ownable for MyCow<'a, X> { |
87 impl<'a, X: Ownable> Ownable for MyCow<'a, X> { |
| 71 type OwnedVariant = X::OwnedVariant; |
88 type OwnedVariant = X::OwnedVariant; |
| 72 |
89 |
| 73 #[inline] |
90 #[inline] |
| 74 /// Returns an owned instance. |
|
| 75 fn into_owned(self) -> Self::OwnedVariant { |
91 fn into_owned(self) -> Self::OwnedVariant { |
| 76 match self { |
92 match self { |
| 77 EitherDecomp::Owned(x) => x.into_owned(), |
93 EitherDecomp::Owned(x) => x.into_owned(), |
| 78 EitherDecomp::Borrowed(x) => x.into_owned(), |
94 EitherDecomp::Borrowed(x) => x.into_owned(), |
| 79 } |
95 } |
| 80 } |
96 } |
| 81 |
97 |
| 82 #[inline] |
98 #[inline] |
| 83 /// Returns an owned instance. |
|
| 84 fn clone_owned(&self) -> Self::OwnedVariant { |
99 fn clone_owned(&self) -> Self::OwnedVariant { |
| 85 match self { |
100 match self { |
| 86 EitherDecomp::Owned(x) => x.into_owned(), |
101 EitherDecomp::Owned(x) => x.into_owned(), |
| 87 EitherDecomp::Borrowed(x) => x.into_owned(), |
102 EitherDecomp::Borrowed(x) => x.into_owned(), |
| 88 } |
103 } |
| |
104 } |
| |
105 |
| |
106 #[inline] |
| |
107 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> |
| |
108 where |
| |
109 Self: 'b, |
| |
110 { |
| |
111 todo!(); |
| 89 } |
112 } |
| 90 } |
113 } |
| 91 |
114 |
| 92 /// Trait for abitrary mathematical spaces. |
115 /// Trait for abitrary mathematical spaces. |
| 93 pub trait Space: Ownable<OwnedVariant = Self::OwnedSpace> + Sized { |
116 pub trait Space: Ownable<OwnedVariant = Self::OwnedSpace> + Sized { |
| 132 |
155 |
| 133 #[inline] |
156 #[inline] |
| 134 fn clone_owned(&self) -> Self::OwnedVariant { |
157 fn clone_owned(&self) -> Self::OwnedVariant { |
| 135 *self |
158 *self |
| 136 } |
159 } |
| |
160 |
| |
161 #[inline] |
| |
162 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { |
| |
163 todo!(); |
| |
164 } |
| 137 } |
165 } |
| 138 }; |
166 }; |
| 139 } |
167 } |
| 140 |
168 |
| 141 impl_basic_space!(u8 u16 u32 u64 u128 usize |
169 impl_basic_space!(u8 u16 u32 u64 u128 usize |
| 142 i8 i16 i32 i64 i128 isize |
170 i8 i16 i32 i64 i128 isize |
| 143 f32 f64); |
171 f32 f64); |
| 144 |
172 |
| 145 /// Marker type for decompositions to be used with [`Instance`]. |
173 /// Marker type for decompositions to be used with [`Instance`]. |
| 146 pub trait Decomposition<X: Space>: Sized { |
174 pub trait Decomposition<X: Space>: Sized { |
| 147 /// Owned instance |
|
| 148 type OwnedInstance: Instance<X, Self>; |
|
| 149 |
|
| 150 /// Possibly owned form of the decomposition |
175 /// Possibly owned form of the decomposition |
| 151 type Decomposition<'b>: Instance<X, Self> |
176 type Decomposition<'b>: Instance<X, Self> |
| 152 where |
177 where |
| 153 X: 'b; |
178 X: 'b; |
| 154 /// Unlikely owned form of the decomposition. |
179 /// Unlikely owned form of the decomposition. |
| 159 where |
184 where |
| 160 X: 'b; |
185 X: 'b; |
| 161 |
186 |
| 162 /// Lift the lightweight reference type into a full decomposition type. |
187 /// Lift the lightweight reference type into a full decomposition type. |
| 163 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; |
188 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; |
|
| 167 } |
189 } |
| 168 |
190 |
| 169 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
191 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
| 170 /// values and all sorts of references. |
192 /// values and all sorts of references. |
| 171 #[derive(Copy, Clone, Debug)] |
193 #[derive(Copy, Clone, Debug)] |
| 172 pub struct BasicDecomposition; |
194 pub struct BasicDecomposition; |
| 173 |
195 |
| 174 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
196 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { |
| 175 type OwnedInstance = X; |
|
| 176 |
|
| 177 type Decomposition<'b> |
197 type Decomposition<'b> |
| 178 = MyCow<'b, X> |
198 = MyCow<'b, X> |
| 179 where |
199 where |
| 180 X: 'b; |
200 X: 'b; |
| 181 type Reference<'b> |
201 type Reference<'b> |
| 213 where |
233 where |
| 214 X: 'b, |
234 X: 'b, |
| 215 Self: 'b; |
235 Self: 'b; |
| 216 |
236 |
| 217 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
237 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| 218 fn own(self) -> D::OwnedInstance; |
238 fn own(self) -> X::OwnedSpace { |
| |
239 self.into_owned() |
| |
240 } |
| 219 |
241 |
| 220 // ************** automatically implemented methods below from here ************** |
242 // ************** automatically implemented methods below from here ************** |
| 221 |
243 |
| 222 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
244 /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
| 223 /// |
245 /// |
| 224 /// Default implementation uses [`Self::own`]. Consumes the input. |
246 /// Default implementation uses [`Self::own`]. Consumes the input. |
| 225 fn cow<'b>(self) -> MyCow<'b, D::OwnedInstance> |
247 fn cow<'b>(self) -> MyCow<'b, X::OwnedSpace> |
| 226 where |
248 where |
| 227 Self: 'b, |
249 Self: 'b, |
| 228 { |
250 { |
| 229 MyCow::Owned(self.own()) |
251 self.cow_owned() |
| 230 } |
252 } |
| 231 |
253 |
| 232 #[inline] |
254 #[inline] |
| 233 /// Evaluates `f` on a reference to self. |
255 /// Evaluates `f` on a reference to self. |
| 234 /// |
256 /// |
| 235 /// Default implementation uses [`Self::cow`]. Consumes the input. |
257 /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 236 fn eval<'b, R>(self, f: impl FnOnce(&D::OwnedInstance) -> R) -> R |
258 fn eval<'b, R>(self, f: impl FnOnce(&X::OwnedSpace) -> R) -> R |
| 237 where |
259 where |
| 238 X: 'b, |
260 X: 'b, |
| 239 Self: 'b, |
261 Self: 'b, |
| 240 { |
262 { |
| 241 f(&*self.cow()) |
263 f(&*self.cow()) |
| 376 Self: 'b, |
359 Self: 'b, |
| 377 { |
360 { |
| 378 match self { |
361 match self { |
| 379 MyCow::Borrowed(a) => f(a), |
362 MyCow::Borrowed(a) => f(a), |
| 380 MyCow::Owned(b) => f(&b), |
363 MyCow::Owned(b) => f(&b), |
| 381 } |
|
| 382 } |
|
| 383 |
|
| 384 #[inline] |
|
| 385 fn own(self) -> X { |
|
| 386 match self { |
|
| 387 MyCow::Borrowed(a) => a.own(), |
|
| 388 MyCow::Owned(b) => b.own(), |
|
| 389 } |
|
| 390 } |
|
| 391 |
|
| 392 #[inline] |
|
| 393 fn cow<'b>(self) -> MyCow<'b, X> |
|
| 394 where |
|
| 395 Self: 'b, |
|
| 396 { |
|
| 397 match self { |
|
| 398 MyCow::Borrowed(a) => a.cow(), |
|
| 399 MyCow::Owned(b) => b.cow(), |
|
| 400 } |
364 } |
| 401 } |
365 } |
| 402 } |
366 } |
| 403 |
367 |
| 404 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
368 /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |