src/instance.rs

branch
dev
changeset 155
45d03cf92c23
parent 153
829c07ea584d
child 159
279b1f5b8608
equal deleted inserted replaced
154:03f34ba55685 155:45d03cf92c23
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,
279 { 291 {
280 MyCow::Owned(self) 292 MyCow::Owned(self)
281 } 293 }
282 } 294 }
283 295
284 impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a X { 296 impl<'a, X: Space + Clone> Instance<X, BasicDecomposition> for &'a X {
285 #[inline] 297 #[inline]
286 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R 298 fn eval_decompose<'b, R>(self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R
287 where 299 where
288 X: 'b, 300 X: 'b,
289 Self: 'b, 301 Self: 'b,
300 f(*self) 312 f(*self)
301 } 313 }
302 314
303 #[inline] 315 #[inline]
304 fn own(self) -> X { 316 fn own(self) -> X {
305 self.into_owned() 317 self.clone()
306 } 318 }
307 319
308 #[inline] 320 #[inline]
309 fn cow<'b>(self) -> MyCow<'b, X> 321 fn cow<'b>(self) -> MyCow<'b, X>
310 where 322 where
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,
333 f(*self) 345 f(*self)
334 } 346 }
335 347
336 #[inline] 348 #[inline]
337 fn own(self) -> X { 349 fn own(self) -> X {
338 self.into_owned() 350 self.clone()
339 } 351 }
340 352
341 #[inline] 353 #[inline]
342 fn cow<'b>(self) -> MyCow<'b, X> 354 fn cow<'b>(self) -> MyCow<'b, X>
343 where 355 where
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,

mercurial