src/instance.rs

branch
dev
changeset 166
20fa28637737
parent 165
478c23ce7cef
child 168
93daa824c04a
equal deleted inserted replaced
165:478c23ce7cef 166:20fa28637737
36 36
37 /// Returns an owned instance or a reference to one. 37 /// Returns an owned instance or a reference to one.
38 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> 38 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
39 where 39 where
40 Self: 'b; 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;
41 } 46 }
42 47
43 impl<'a, X: Ownable> Ownable for &'a X { 48 impl<'a, X: Ownable> Ownable for &'a X {
44 type OwnedVariant = X::OwnedVariant; 49 type OwnedVariant = X::OwnedVariant;
45 50
46 #[inline] 51 #[inline]
47 fn into_owned(self) -> Self::OwnedVariant { 52 fn into_owned(self) -> Self::OwnedVariant {
48 self.clone_owned() 53 X::clone_owned(self)
49 } 54 }
50 55
51 #[inline] 56 #[inline]
52 fn clone_owned(&self) -> Self::OwnedVariant { 57 fn clone_owned(&self) -> Self::OwnedVariant {
53 (*self).into_owned() 58 X::clone_owned(self)
54 } 59 }
55 60
56 #[inline] 61 #[inline]
57 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> 62 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
58 where 63 where
59 Self: 'b, 64 Self: 'b,
60 { 65 {
61 todo!(); 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)
62 } 74 }
63 } 75 }
64 76
65 impl<'a, X: Ownable> Ownable for &'a mut X { 77 impl<'a, X: Ownable> Ownable for &'a mut X {
66 type OwnedVariant = X::OwnedVariant; 78 type OwnedVariant = X::OwnedVariant;
67 79
68 #[inline] 80 #[inline]
69 fn into_owned(self) -> Self::OwnedVariant { 81 fn into_owned(self) -> Self::OwnedVariant {
70 self.clone_owned() 82 X::clone_owned(self)
71 } 83 }
72 84
73 #[inline] 85 #[inline]
74 fn clone_owned(&self) -> Self::OwnedVariant { 86 fn clone_owned(&self) -> Self::OwnedVariant {
75 (&**self).into_owned() 87 X::clone_owned(self)
76 } 88 }
77 89
78 #[inline] 90 #[inline]
79 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> 91 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
80 where 92 where
81 Self: 'b, 93 Self: 'b,
82 { 94 {
83 todo!(); 95 X::ref_cow_owned(self)
84 } 96 }
85 } 97
86 98 fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
87 impl<'a, X: Ownable> Ownable for MyCow<'a, X> { 99 where
88 type OwnedVariant = X::OwnedVariant; 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;
89 112
90 #[inline] 113 #[inline]
91 fn into_owned(self) -> Self::OwnedVariant { 114 fn into_owned(self) -> Self::OwnedVariant {
92 match self { 115 match self {
93 EitherDecomp::Owned(x) => x.into_owned(), 116 EitherDecomp::Owned(a) => A::into_owned(a),
94 EitherDecomp::Borrowed(x) => x.into_owned(), 117 EitherDecomp::Borrowed(b) => B::into_owned(b),
95 } 118 }
96 } 119 }
97 120
98 #[inline] 121 #[inline]
99 fn clone_owned(&self) -> Self::OwnedVariant { 122 fn clone_owned(&self) -> Self::OwnedVariant {
100 match self { 123 match self {
101 EitherDecomp::Owned(x) => x.into_owned(), 124 EitherDecomp::Owned(a) => A::clone_owned(a),
102 EitherDecomp::Borrowed(x) => x.into_owned(), 125 EitherDecomp::Borrowed(b) => B::clone_owned(b),
103 } 126 }
104 } 127 }
105 128
106 #[inline] 129 #[inline]
107 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> 130 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant>
108 where 131 where
109 Self: 'b, 132 A: 'b,
110 { 133 B: 'b,
111 todo!(); 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))
112 } 213 }
113 } 214 }
114 215
115 /// Trait for abitrary mathematical spaces. 216 /// Trait for abitrary mathematical spaces.
116 pub trait Space: Ownable<OwnedVariant = Self::Principal> + Sized { 217 pub trait Space: Ownable<OwnedVariant = Self::Principal> + Sized {
159 *self 260 *self
160 } 261 }
161 262
162 #[inline] 263 #[inline]
163 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b { 264 fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> where Self : 'b {
164 todo!(); 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)
165 } 271 }
166 } 272 }
167 }; 273 };
168 } 274 }
169 275

mercurial