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
//! This crate provides api to solve borrow checker errors caused by limitation of current rust borrow checked.
//! There exists next version of rust borrow checker called Polonius that is supposed to remove such limitations,
//! but it is permanently unstable with no current plans for stabilization.
//! And quite often the only way to work around those limitations on stable rust without some
//! kind of regression is with unsafe code. But it is annoying to be required to use unsafe for
//! such simple things, and it is too easy to get wrong,
//! not to mention it is just delayed bomb waiting for some uncareful refactoring to release it.
//!
//! All functionality is provided via `PoloniusExt` extension trait.
//! It has 3 methods:
//! - `try_get_with`/`try_get_mut_with` work for simple cases where you need to return
//! shared/mutable reference respectively. It should just work and in most cases that is enough.
//! But sometimes you need to return not a reference but some another type that contains a reference.
//! Thats when you need
//! - `try_get_with2` It allows you to return any type with reference inside,
//! but due to rust type inference bugs around HRTBs it is a bit annoying to use.
//! See its docs for more details.
//!
//! As far as author knows it allows to solve any king of borrow checker issues that would be solved by Polonius.
//! Although you still need to be confident enough in Rust to know that your code
//! is actually correct and just not accepted by current borrow checker.
//! So although with this crate you do not actually need Polonius anymore,
//! it is still a nice thing to have in general.
//!
//! The code compiles since Rust 1.0 but
//! but `try_get_with2` actually works only since Rust 1.41.
//!
//! And here is a real example that you couldn't work around without significant performance regression or unsafe.
//! ```rust,compile_fail
//! # use std::collections::LinkedList;
//! trait LinkedListExt<T> {
//! fn find_or_create<F, C>(&mut self, predicate: F, create: C) -> &mut T
//! where
//! F: FnMut(&T) -> bool,
//! C: FnMut() -> T;
//! }
//!
//! impl<T: 'static> LinkedListExt<T> for LinkedList<T> {
//! fn find_or_create<F, C>(&mut self, mut predicate: F, mut create: C) -> &mut T
//! where
//! F: FnMut(&T) -> bool,
//! C: FnMut() -> T,
//! {
//! if let Some(x) = self.iter_mut().find(|e| predicate(&*e)){
//! return x;
//! }
//! self.push_back(create());
//! self.back_mut().unwrap()
//! }
//! }
//! ```
//! Now with this crate you just wrap two branches into a closures then call `try_get_mut_with` and voila - it works
//! ```rust
//! # use std::collections::LinkedList;
//! # use polonius_workaround::PoloniusExt;
//! # trait LinkedListExt<T> {
//! # fn find_or_create<F, C>(&mut self, predicate: F, create: C) -> &mut T
//! # where
//! # F: FnMut(&T) -> bool,
//! # C: FnMut() -> T;
//! # }
//! impl<T: 'static> LinkedListExt<T> for LinkedList<T> {
//! fn find_or_create<F, C>(&mut self, mut predicate: F, mut create: C) -> &mut T
//! where
//! F: FnMut(&T) -> bool,
//! C: FnMut() -> T,
//! {
//! self.try_get_mut_with(|x| x.iter_mut().find(|e| predicate(&*e)))
//! .unwrap_or_else(|x| {
//! x.push_back(create());
//! x.back_mut().unwrap()
//! })
//! }
//! }
//! ```
/// Extension trait to implement described functionality.
///
/// The general idea is to store mutable reference value in raw pointer while the closure executes.
/// If the closure returned `Some` then we just forget that pointer and everything goes as usual,
/// the mere existence of raw pointer can't break anything.
/// If the closure returned `None` we know that any borrow derived from `& mut Self` can't be alive
/// at that point so we can soundly restore the original `&mut Self`.
/// Helper trait for `PoloniusExt::try_get_with2`