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
281
282
283
284
285
286
287
288
289
290
291
//! The `once` module provides the wrapper type [`Lease`] that allows extending
//! the lifetime of references to cover the whole remaining time of the
//! referenced value.
//!
//! This effectively allows a weaker form of pinning and changes mutable borrows
//! to ownership semantics without having to move the value.
//!
//! Credit goes to **Lukas Markeffsky** for the idea and proof-of-concept using
//! the underlying mechanism of using an inner lifetime to have the
//! borrow-checker enforce pinning and ownership semantics for mutable borrows.
use ;
/// A "one-shot" reference wrapper that pins borrows in place once they have
/// been passed into a function as an argument. For `mut` borrows, this changes
/// them to behave similarly to an owned value, preventing subsequent borrows
/// once they have been consumed in a function.
///
/// `Lease` is a lightweight wrapper around a value of type `T` that is
/// associated with a lifetime `'p`. Once borrowed and moved into a function
/// (by passing it as a parameter), it can no longer be moved or exclusively
/// borrowed again in the original scope, mimicking ownership behavior. This
/// helps prevent multiple borrows after a `Lease` borrow is consumed.
///
/// ## Examples
/// The purpose of this type is to enable the following pattern:
///
/// ```compile_fail,E0499
/// # use odem_rs_core::ptr::Lease;
/// fn indefinitely_borrow<'p,T>(value: &'p mut Lease<'p,T>) {
/// // ...
/// }
///
/// struct S;
/// let mut value = Lease::new(S);
/// indefinitely_borrow(&mut value);
/// indefinitely_borrow(&mut value); // error: value is mutably borrowed
/// ```
///
/// It also works with shared references, effectively preventing the value to
/// be moved after its first borrow:
///
/// ```compile_fail,E0505
/// # use odem_rs_core::ptr::Lease;
/// fn indefinitely_borrow<'p,T>(value: &'p Lease<'p,T>) {
/// // ...
/// }
///
/// struct S;
/// let mut value = Lease::new(S);
/// indefinitely_borrow(&value);
/// indefinitely_borrow(&value); // allowed
/// std::mem::forget(value); // error: value is still borrowed
/// ```
///
/// There are type-aliases for these two use-cases to help phrase them more
/// succinctly:
///
/// ```
/// # use odem_rs_core::ptr::{Lease, LeasedRef, LeasedMut};
/// fn indefinitely_borrow_ref<T>(value: LeasedRef<T>) {
/// // same semantics as before but inferred lifetime
/// }
///
/// fn indefinitely_borrow_mut<T>(value: LeasedMut<T>) {
/// // same semantics as before but inferred lifetime
/// }
/// ```
///
/// This is *almost* as good as pinning the value but doesn't guarantee that
/// `drop` is executed, which pinning does:
///
/// ```
/// # use {odem_rs_core::ptr::{Lease, LeasedRef}, core::mem::ManuallyDrop};
/// fn indefinitely_borrow<T>(value: LeasedRef<T>) {
/// // ...
/// }
///
/// struct S;
/// impl Drop for S {
/// fn drop(&mut self) {
/// assert!(false, "drop doesn't run");
/// }
/// }
///
/// let mut value = ManuallyDrop::new(Lease::new(S));
/// indefinitely_borrow(&value); // borrowed forever
/// // no `drop` is run due to the use of `ManuallyDrop`
/// ```
///
/// Pinning a `Lease` ensures that the destructor is run by the [drop guarantee].
/// This allows use of the following pattern:
///
/// ```compile_fail,E0499
/// # use {core::pin::{Pin, pin}, odem_rs_core::ptr::{Lease, LeasedMut}};
/// fn pinned_borrow<T>(value: Pin<LeasedMut<T>>) {
/// // ...
/// }
///
/// struct S;
/// impl Drop for S {
/// fn drop(&mut self) {
/// println!("drop runs");
/// }
/// }
///
/// let mut value = pin!(Lease::new(S));
/// pinned_borrow(value.as_mut());
/// pinned_borrow(value.as_mut()); // error: value is mutably borrowed
/// // `drop` runs because `Pin<&mut T>` is different from `Pin<&mut ManuallyDrop<T>>`
/// ```
///
/// # How it works
///
/// To the borrow-checker, `Lease<'p,T>` stores a reference to `T` with an
/// invariant `'p` lifetime. This makes `'p` invariant over `Lease` per
/// [this table] and has the following two consequences:
///
/// 1. `Lease` cannot implement `Drop` because `Drop::drop` takes a `mut`
/// reference that definitionally cannot coexist with another active
/// reference to one of its members.
/// 2. Any reference - shared or exclusive - borrows the `Lease` indefinitely,
/// which enforces the intended "one-shot" borrow semantics.
///
/// The second point is a consequence of the invariance of `'p` by the
/// following argument:
///
/// 1. Choosing a `'p` that lives longer than the thing it's referencing leads
/// to a dangling reference, because `&'long Lease<'short, T>` cannot exist.
/// 2. Choosing a `'p` that lives shorter, as in `&'short Lease<'long, T>` can
/// only exist if `'long` can be coerced to `'short` in order to arrive at
/// the `&'short Lease<'short, T>` required by the function signature.
/// This is only allowed if `Lease<'p, T>` is covariant over `'p`.
///
/// `'p` being invariant over `Lease<'p, T>` disallows exactly that, forcing the
/// borrow-checker to select the shorter of the two lifetimes for both the outer
/// and the inner `'p`. This however forces the user to borrow the `Lease`
/// **exactly** for the duration of its lifetime, since its lifetime shortens
/// with `'p`.
///
/// Credit goes to **Lukas Markeffsky** for the idea and proof-of-concept using
/// the underlying mechanism of using an inner lifetime to have the
/// borrow-checker enforce pinning and ownership semantics for mutable borrows.
///
/// [drop guarantee]: core::pin
/// [this table]: https://doc.rust-lang.org/nomicon/subtyping.html#variance
/// A type alias representing a shared reference to a [`Lease`].
///
/// # Overview
///
/// `LeasedRef<'p,T>` is `&'p Lease<'p, T>` and cannot be moved after being passed
/// into a function.
///
/// # Example
///
/// ```compile_fail,E0505
/// # use odem_rs_core::ptr::{Lease, LeasedRef};
/// fn ref_it<T>(val: LeasedRef<T>) {
/// // ...
/// }
///
/// struct S;
/// let x = Lease::new(S);
/// ref_it(&x);
/// ref_it(&x); // allowed as often as you want
/// std::mem::forget(x); // error: value is still borrowed
/// ```
pub type LeasedRef<'p, T> = &'p ;
/// A type alias representing a mutable reference to a [`Lease`].
///
/// # Overview
///
/// `LeasedMut<'p,T>` is `&'p mut Lease<'p, T>` and cannot be moved or accessed
/// (other than during `drop`) after being passed into a function, as if
/// ownership has been transferred. The resulting value still lives in the scope
/// of the function caller, i.e. it outlives the function scope.
///
/// # Example
///
/// ```compile_fail
/// # use odem_rs_core::ptr::{Lease, LeasedMut};
/// fn ref_it<T>(val: LeasedMut<T>) {
/// // ...
/// }
///
/// struct S;
/// let mut x = Lease::new(S);
/// ref_it(&mut x);
/// ref_it(&mut x); // Error: value is still mutably borrowed
/// std::mem::forget(x); // error: value is still borrowed
/// ```
pub type LeasedMut<'p, T> = &'p mut ;