src/instance.rs

branch
dev
changeset 136
22fd33834ab7
parent 133
2b13f8a0c8ba
child 137
d5dfcb6abcf5
equal deleted inserted replaced
133:2b13f8a0c8ba 136:22fd33834ab7
60 i8 i16 i32 i64 i128 isize 60 i8 i16 i32 i64 i128 isize
61 f32 f64); 61 f32 f64);
62 62
63 /// Marker type for decompositions to be used with [`Instance`]. 63 /// Marker type for decompositions to be used with [`Instance`].
64 pub trait Decomposition<X: Space>: Sized { 64 pub trait Decomposition<X: Space>: Sized {
65 /// Possibly owned form of the decomposition 65 /// The composition
66 type Decomposition<'b>: Instance<X, Self> 66 type Decomposition<'b>: Instance<X, Self>
67 where 67 where
68 X: 'b; 68 X: 'b;
69 /// Unlikely owned form of the decomposition.
70 /// Type for a lightweight intermediate conversion that does not own the original variable.
71 /// Usually this is just a reference, but may also be a lightweight structure that
72 /// contains references; see the implementation for [`crate::direct_product::Pair`].
73 type Reference<'b>: Instance<X, Self> + Copy
74 where
75 X: 'b;
76
77 /// Left the lightweight reference type into a full decomposition type.
78 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>;
79 } 69 }
80 70
81 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned 71 /// Most common [`Decomposition`] (into `Either<X, &'b X>`) that allows working with owned
82 /// values and all sorts of references. 72 /// values and all sorts of references.
83 #[derive(Copy, Clone, Debug)] 73 #[derive(Copy, Clone, Debug)]
86 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition { 76 impl<X: Space + Clone> Decomposition<X> for BasicDecomposition {
87 type Decomposition<'b> 77 type Decomposition<'b>
88 = MyCow<'b, X> 78 = MyCow<'b, X>
89 where 79 where
90 X: 'b; 80 X: 'b;
91 type Reference<'b>
92 = &'b X
93 where
94 X: 'b;
95
96 #[inline]
97 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> {
98 MyCow::Borrowed(r)
99 }
100 } 81 }
101 82
102 /// Helper trait for functions to work with either owned values or references to either the 83 /// Helper trait for functions to work with either owned values or references to either the
103 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this 84 /// “principal type” `X` or types some present a subset of `X`. In the latter sense, this
104 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`]. 85 /// generalises [`std::borrow::ToOwned`], [`std::borrow::Borrow`], and [`std::borrow::Cow`].
115 X: 'b, 96 X: 'b,
116 Self: 'b; 97 Self: 'b;
117 98
118 /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result. 99 /// Does a light decomposition of self `decomposer`, and evaluates `f` on the result.
119 /// Does not consume self. 100 /// Does not consume self.
120 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(D::Reference<'b>) -> R) -> R 101 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(D::Decomposition<'b>) -> R) -> R
121 where 102 where
122 X: 'b, 103 X: 'b,
123 Self: 'b; 104 Self: 'b;
124 105
125 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary. 106 /// Returns an owned instance of `X`, cloning or converting non-true instances when necessary.
173 { 154 {
174 f(MyCow::Owned(self)) 155 f(MyCow::Owned(self))
175 } 156 }
176 157
177 #[inline] 158 #[inline]
178 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R 159 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R
179 where 160 where
180 X: 'b, 161 X: 'b,
181 Self: 'b, 162 Self: 'b,
182 { 163 {
183 f(self) 164 f(MyCow::Borrowed(&self))
184 } 165 }
185 166
186 #[inline] 167 #[inline]
187 fn own(self) -> X { 168 fn own(self) -> X {
188 self 169 self
206 { 187 {
207 f(MyCow::Borrowed(self)) 188 f(MyCow::Borrowed(self))
208 } 189 }
209 190
210 #[inline] 191 #[inline]
211 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R 192 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R
212 where 193 where
213 X: 'b, 194 X: 'b,
214 Self: 'b, 195 Self: 'b,
215 { 196 {
216 f(*self) 197 f(MyCow::Borrowed(self))
217 } 198 }
218 199
219 #[inline] 200 #[inline]
220 fn own(self) -> X { 201 fn own(self) -> X {
221 self.clone() 202 self.clone()
239 { 220 {
240 f(EitherDecomp::Borrowed(self)) 221 f(EitherDecomp::Borrowed(self))
241 } 222 }
242 223
243 #[inline] 224 #[inline]
244 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R 225 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R
245 where 226 where
246 X: 'b, 227 X: 'b,
247 Self: 'b, 228 Self: 'b,
248 { 229 {
249 f(*self) 230 f(EitherDecomp::Borrowed(self))
250 } 231 }
251 232
252 #[inline] 233 #[inline]
253 fn own(self) -> X { 234 fn own(self) -> X {
254 self.clone() 235 self.clone()
273 { 254 {
274 f(self) 255 f(self)
275 } 256 }
276 257
277 #[inline] 258 #[inline]
278 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(&'b X) -> R) -> R 259 fn eval_ref_decompose<'b, R>(&'b self, f: impl FnOnce(MyCow<'b, X>) -> R) -> R
279 where 260 where
280 X: 'b, 261 X: 'b,
281 Self: 'b, 262 Self: 'b,
282 { 263 {
283 match self { 264 match self {
284 MyCow::Borrowed(a) => f(a), 265 MyCow::Borrowed(a) => f(MyCow::Borrowed(a)),
285 MyCow::Owned(b) => f(&b), 266 MyCow::Owned(b) => f(MyCow::Borrowed(&b)),
286 } 267 }
287 } 268 }
288 269
289 #[inline] 270 #[inline]
290 fn own(self) -> X { 271 fn own(self) -> X {

mercurial