Wed, 03 Sep 2025 21:03:47 -0500
No either
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | /*! |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | Helper traits to work with references or owned values of types and their subsets. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | */ |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | #[derive(Clone, Copy)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | pub enum EitherDecomp<A, B> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | Owned(A), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | Borrowed(B), |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | |
|
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
11 | /// A very basic implementation of [`std::borrow::Cow`] without a [`Clone`] trait dependency. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | pub type MyCow<'b, X> = EitherDecomp<X, &'b X>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | impl<'b, X> std::ops::Deref for MyCow<'b, X> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | type Target = X; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | fn deref(&self) -> &Self::Target { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | match self { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | EitherDecomp::Owned(x) => &x, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | EitherDecomp::Borrowed(x) => x, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | |
| 150 | 26 | /// Trait for ownable-by-consumption objects |
| 27 | pub trait Ownable { | |
| 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; | |
| 162 | 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; | |
| 166 | 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; | |
| 150 | 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 { | |
| 166 | 53 | X::clone_owned(self) |
| 150 | 54 | } |
| 55 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | #[inline] |
| 150 | 57 | fn clone_owned(&self) -> Self::OwnedVariant { |
| 166 | 58 | X::clone_owned(self) |
| 150 | 59 | } |
| 162 | 60 | |
| 61 | #[inline] | |
| 62 | fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> | |
| 63 | where | |
| 64 | Self: 'b, | |
| 65 | { | |
| 166 | 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) | |
| 162 | 74 | } |
| 150 | 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 { | |
| 166 | 82 | X::clone_owned(self) |
| 150 | 83 | } |
| 84 | ||
| 85 | #[inline] | |
| 86 | fn clone_owned(&self) -> Self::OwnedVariant { | |
| 166 | 87 | X::clone_owned(self) |
| 150 | 88 | } |
| 162 | 89 | |
| 90 | #[inline] | |
| 91 | fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> | |
| 92 | where | |
| 93 | Self: 'b, | |
| 94 | { | |
| 166 | 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) | |
| 162 | 103 | } |
| 150 | 104 | } |
| 105 | ||
| 166 | 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; | |
| 150 | 112 | |
| 113 | #[inline] | |
| 114 | fn into_owned(self) -> Self::OwnedVariant { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
115 | match self { |
| 166 | 116 | EitherDecomp::Owned(a) => A::into_owned(a), |
| 117 | EitherDecomp::Borrowed(b) => B::into_owned(b), | |
| 150 | 118 | } |
| 119 | } | |
| 120 | ||
| 121 | #[inline] | |
| 122 | fn clone_owned(&self) -> Self::OwnedVariant { | |
| 123 | match self { | |
| 166 | 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), | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
150 | } |
| 166 | 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 | } | |
| 162 | 199 | |
| 200 | #[inline] | |
| 201 | fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> | |
| 202 | where | |
| 203 | Self: 'b, | |
| 204 | { | |
| 166 | 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)) | |
| 162 | 213 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | /// Trait for abitrary mathematical spaces. |
| 164 | 217 | pub trait Space: Ownable<OwnedVariant = Self::Principal> + Sized { |
| 218 | /// Principal, typically owned realisation of the space. | |
| 219 | type Principal: ClosedSpace; | |
| 150 | 220 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
221 | /// Default decomposition for the space |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
222 | type Decomp: Decomposition<Self>; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
223 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
224 | |
| 150 | 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 | |
| 164 | 236 | pub trait ClosedSpace: Space<Principal = Self> + Owned + Instance<Self> {} |
| 237 | impl<X: Space<Principal = Self> + Owned + Instance<Self>> ClosedSpace for X {} | |
| 150 | 238 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
239 | #[macro_export] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
240 | macro_rules! impl_basic_space { |
| 150 | 241 | ($($type:ty)*) => { |
| 242 | $( $crate::impl_basic_space!($type where ); )* | |
| 243 | }; | |
| 244 | ($type:ty where $($where:tt)*) => { | |
| 245 | impl<$($where)*> $crate::instance::Space for $type { | |
| 164 | 246 | type Principal = Self; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
247 | type Decomp = $crate::instance::BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
248 | } |
| 150 | 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 | } | |
| 162 | 262 | |
| 263 | #[inline] | |
| 264 | fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { | |
| 166 | 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) | |
| 162 | 271 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
272 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
273 | }; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
274 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
275 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
276 | impl_basic_space!(u8 u16 u32 u64 u128 usize |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
277 | i8 i16 i32 i64 i128 isize |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
278 | f32 f64); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
279 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
280 | /// Marker type for decompositions to be used with [`Instance`]. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
281 | pub trait Decomposition<X: Space>: Sized { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
282 | /// Possibly owned form of the decomposition |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
283 | type Decomposition<'b>: Instance<X, Self> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
284 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
285 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
286 | /// Unlikely owned form of the decomposition. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
287 | /// Type for a lightweight intermediate conversion that does not own the original variable. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
288 | /// Usually this is just a reference, but may also be a lightweight structure that |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
289 | /// contains references; see the implementation for [`crate::direct_product::Pair`]. |
| 165 | 290 | type Reference<'b>: Instance<X, Self> + Copy |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
291 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
292 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
293 | |
| 155 | 294 | /// Lift the lightweight reference type into a full decomposition type. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
295 | fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
296 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
297 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
298 | /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
299 | /// values and all sorts of references. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
300 | #[derive(Copy, Clone, Debug)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
301 | pub struct BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
302 | |
| 163 | 303 | impl<X: Space> Decomposition<X> for BasicDecomposition { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
304 | type Decomposition<'b> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
305 | = MyCow<'b, X> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
306 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
307 | X: 'b; |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
308 | type Reference<'b> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
309 | = &'b X |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
310 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
311 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
312 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
313 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
314 | fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
315 | MyCow::Borrowed(r) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
316 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
317 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
318 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
319 | /// Helper trait for functions to work with either owned values or references to either the |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
320 | /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
321 | /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. |
| 88 | 322 | /// |
| 323 | /// This is used, for example, by [`crate::mapping::Mapping::apply`]. | |
| 168 | 324 | pub trait Instance<X, D = <X as Space>::Decomp>: Sized |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
325 | where |
| 150 | 326 | X: Space, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
327 | D: Decomposition<X>, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
328 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
329 | /// Decomposes self according to `decomposer`, and evaluate `f` on the result. |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
330 | /// Consumes self. |
| 171 | 331 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
332 | fn eval_decompose<'b, R>(self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
333 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
334 | X: 'b, |
| 171 | 335 | Self: 'b, |
| 336 | { | |
| 337 | f(self.decompose()) | |
| 338 | } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
339 | |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
340 | /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result. |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
341 | /// Does not consume self. |
| 171 | 342 | fn eval_ref<'b, R>(&'b self, f: impl FnOnce(D::Reference<'b>) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
343 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
344 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
345 | Self: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
346 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
347 | /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. |
| 168 | 348 | fn own(self) -> X::Principal; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
349 | |
| 171 | 350 | fn decompose<'b>(self) -> D::Decomposition<'b> |
| 351 | where | |
| 352 | Self: 'b; | |
| 353 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
354 | /// Returns an owned instance or reference to `X`, converting non-true instances when necessary. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
355 | /// |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
356 | /// Default implementation uses [`Self::own`]. Consumes the input. |
| 164 | 357 | fn cow<'b>(self) -> MyCow<'b, X::Principal> |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
358 | where |
| 168 | 359 | Self: 'b; |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
360 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
361 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
362 | /// Evaluates `f` on a reference to self. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
363 | /// |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
364 | /// Default implementation uses [`Self::cow`]. Consumes the input. |
| 164 | 365 | fn eval<'b, R>(self, f: impl FnOnce(&X::Principal) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
366 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
367 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
368 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
369 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
370 | f(&*self.cow()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
371 | } |
| 170 | 372 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
373 | |
| 170 | 374 | impl<X: Space> Instance<X, BasicDecomposition> for X { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
375 | #[inline] |
| 171 | 376 | fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
377 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
378 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
379 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
380 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
381 | f(self) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
382 | } |
| 168 | 383 | |
| 384 | #[inline] | |
| 385 | fn own(self) -> X::Principal { | |
| 386 | self.into_owned() | |
| 387 | } | |
| 388 | ||
| 389 | #[inline] | |
| 390 | fn cow<'b>(self) -> MyCow<'b, X::Principal> | |
| 391 | where | |
| 392 | Self: 'b, | |
| 393 | { | |
| 394 | self.cow_owned() | |
| 395 | } | |
| 171 | 396 | |
| 397 | #[inline] | |
| 398 | fn decompose<'b>(self) -> MyCow<'b, X> | |
| 399 | where | |
| 400 | Self: 'b, | |
| 401 | { | |
| 402 | MyCow::Owned(self) | |
| 403 | } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
404 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
405 | |
| 163 | 406 | impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a X { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
407 | #[inline] |
| 171 | 408 | fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
409 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
410 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
411 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
412 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
413 | f(*self) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
414 | } |
| 168 | 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 | } | |
| 171 | 428 | |
| 429 | #[inline] | |
| 430 | fn decompose<'b>(self) -> MyCow<'b, X> | |
| 431 | where | |
| 432 | Self: 'b, | |
| 433 | { | |
| 434 | MyCow::Borrowed(self) | |
| 435 | } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
436 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
437 | |
| 163 | 438 | impl<'a, X: Space> Instance<X, BasicDecomposition> for &'a mut X { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
439 | #[inline] |
| 171 | 440 | fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
441 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
442 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
443 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
444 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
445 | f(*self) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
446 | } |
| 168 | 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 | } | |
| 171 | 460 | |
| 461 | #[inline] | |
| 462 | fn decompose<'b>(self) -> MyCow<'b, X> | |
| 463 | where | |
| 464 | Self: 'b, | |
| 465 | { | |
| 466 | MyCow::Borrowed(self) | |
| 467 | } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
468 | } |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
469 | |
| 163 | 470 | impl<'a, X: Space> Instance<X, BasicDecomposition> for MyCow<'a, X> { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
471 | #[inline] |
| 171 | 472 | fn eval_ref<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
473 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
474 | X: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
475 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
476 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
477 | match self { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
478 | MyCow::Borrowed(a) => f(a), |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
479 | MyCow::Owned(b) => f(&b), |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
480 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
481 | } |
| 168 | 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 | } | |
| 171 | 495 | |
| 496 | #[inline] | |
| 497 | fn decompose<'b>(self) -> MyCow<'b, X> | |
| 498 | where | |
| 499 | Self: 'b, | |
| 500 | { | |
| 501 | self | |
| 502 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
503 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
504 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
505 | /// Marker type for mutable decompositions to be used with [`InstanceMut`]. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
506 | pub trait DecompositionMut<X: Space>: Sized { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
507 | type ReferenceMut<'b>: InstanceMut<X, Self> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
508 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
509 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
510 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
511 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
512 | /// Helper trait for functions to work with mutable references. |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
513 | pub trait InstanceMut<X: Space, D = <X as Space>::Decomp>: Sized |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
514 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
515 | D: DecompositionMut<X>, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
516 | { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
517 | /// Returns a mutable decomposition of self. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
518 | fn ref_instance_mut(&mut self) -> D::ReferenceMut<'_>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
519 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
520 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
521 | impl<X: Space> DecompositionMut<X> for BasicDecomposition { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
522 | type ReferenceMut<'b> |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
523 | = &'b mut X |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
524 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
525 | X: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
526 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
527 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
528 | /// This impl may seem pointless, but allows throwaway mutable scratch variables |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
529 | impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for X { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
530 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
531 | fn ref_instance_mut( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
532 | &mut self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
533 | ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
534 | self |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
535 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
536 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
537 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
538 | impl<'a, X: Space> InstanceMut<X, BasicDecomposition> for &'a mut X { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
539 | #[inline] |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
540 | fn ref_instance_mut( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
541 | &mut self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
88
diff
changeset
|
542 | ) -> <BasicDecomposition as DecompositionMut<X>>::ReferenceMut<'_> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
543 | self |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
544 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
545 | } |