1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright © 2021 Anand Beh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use ;
use ;
use *;
use ;
use Ordering;
use ;
/// A type which can be either an immutable reference, or an owned value.
/// RefOrOwned requires sized types. For unsized types, use `RefOrBox` instead.
///
/// One prominent use case for `RefOrOwned` is in function return types,
/// which lets implementations be free to decide whether to return owned or borrowed values.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefOrOwned;
/// struct MyStruct {}
///
/// fn func(my_struct: &MyStruct) -> RefOrOwned<'_, MyStruct> {
/// RefOrOwned::Borrowed(my_struct)
/// }
/// ```
///
/// The type implements `Deref` for `T`, allowing one to use it where
/// `&T` would be required. It also implements `From<&T>` and `From<T>`,
/// which enables ergonomic use in function parameters.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefOrOwned;
///
/// struct MyStruct {}
/// impl MyStruct {
/// fn my_func(&self) -> u8 {
/// 2
/// }
/// }
///
/// fn run_func<'t, T>(my_struct: T) -> u8
/// where T: Into<RefOrOwned<'t, MyStruct>> {
///
/// let my_struct = my_struct.into();
/// // my_struct now has type RefOrOwned<'t, MyStruct>
/// my_struct.my_func()
/// }
/// ```
ref_or_owned_impls!;
/// A type which can be either a mutable reference, or an owned value.
/// RefMutOrOwned requires sized types. For unsized types, use `RefMutOrBox` instead.
///
/// One prominent use case for `RefMutOrOwned` is in function return types,
/// which lets implementations be free to decide whether to return owned or borrowed values.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefMutOrOwned;
/// struct MyStruct {}
///
/// fn func(my_struct: &mut MyStruct) -> RefMutOrOwned<'_, MyStruct> {
/// RefMutOrOwned::Borrowed(my_struct)
/// }
/// ```
///
/// The type implements `Deref` and `DerefMut` for `T`, allowing one to use it where
/// `&mut T` would be required. It also implements `From<&mut T>` and `From<T>`,
/// which enables ergonomic use in function parameters.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefMutOrOwned;
///
/// struct MyStruct {}
/// impl MyStruct {
/// fn my_func(&mut self) -> u8 {
/// 2
/// }
/// }
///
/// fn run_func<'t, T>(my_struct: T) -> u8
/// where T: Into<RefMutOrOwned<'t, MyStruct>> {
///
/// let mut my_struct = my_struct.into();
/// // my_struct now has type RefMutOrOwned<'t, MyStruct>
/// my_struct.my_func()
/// }
/// ```
ref_or_owned_impls!;
/// A type which can be either an immutable reference, or an owned boxed value.
/// Box is used for the owned variant because this type is primarily intended for
/// use with unsized types, most particularly trait objects. For sized types,
/// it is strongly suggested to use `RefOrOwned` instead.
///
/// One prominent use case for `RefOrBox` is in function return types,
/// which lets implementations be free to decide whether to return owned or borrowed values.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefOrBox;
/// trait MyTrait {}
///
/// fn func<'a>(my_trait: &'a dyn MyTrait) -> RefOrBox<'a, dyn MyTrait> {
/// RefOrBox::Borrowed(my_trait)
/// }
/// ```
///
/// The type implements `Deref` for `T`, allowing one to use it where
/// `&T` would be required. It also implements `From<&T>` and `From<Box<T>>`,
/// which enables ergonomic use in function parameters.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefOrBox;
///
/// trait MyTrait {
/// fn my_func(&self) -> u8;
/// }
///
/// fn run_func<'t, T>(my_trait: T) -> u8
/// where T: Into<RefOrBox<'t, dyn MyTrait>> {
///
/// let my_trait = my_trait.into();
/// // my_trait now has type RefOrBox<'t, dyn MyTrait>
/// my_trait.my_func()
/// }
/// ```
ref_or_box_impls!;
/// A type which can be either a mutable reference, or an owned boxed value.
/// Box is used for the owned variant because this type is primarily intended for
/// use with unsized types, most particularly trait objects. For sized types,
/// it is strongly suggested to use `RefMutOrOwned` instead.
///
/// One prominent use case for `RefMutOrBox` is in function return types,
/// which lets implementations be free to decide whether to return owned or borrowed values.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefMutOrBox;
/// trait MyTrait {}
///
/// fn func<'a>(my_trait: &'a mut dyn MyTrait) -> RefMutOrBox<'a, dyn MyTrait> {
/// RefMutOrBox::Borrowed(my_trait)
/// }
/// ```
///
/// The type implements `Deref` and `DerefMut` for `T`, allowing one to use it where
/// `&mut T` would be required. It also implements `From<&mut T>` and `From<Box<T>>`,
/// which enables ergonomic use in function parameters.
///
/// ```rust
/// # use polymorph::ref_or_owned::RefMutOrBox;
///
/// trait MyTrait {
/// fn my_func(&mut self) -> u8;
/// }
///
/// fn run_func<'t, T>(my_trait: T) -> u8
/// where T: Into<RefMutOrBox<'t, dyn MyTrait>> {
///
/// let mut my_trait = my_trait.into();
/// // my_trait now has type RefMutOrBox<'t, dyn MyTrait>
/// my_trait.my_func()
/// }
/// ```
ref_or_box_impls!;