Show More
@@ -1,717 +1,673 b'' | |||
|
1 | 1 | // ref_sharing.rs |
|
2 | 2 | // |
|
3 | 3 | // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> |
|
4 | 4 | // |
|
5 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6 | 6 | // of this software and associated documentation files (the "Software"), to |
|
7 | 7 | // deal in the Software without restriction, including without limitation the |
|
8 | 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
9 | 9 | // sell copies of the Software, and to permit persons to whom the Software is |
|
10 | 10 | // furnished to do so, subject to the following conditions: |
|
11 | 11 | // |
|
12 | 12 | // The above copyright notice and this permission notice shall be included in |
|
13 | 13 | // all copies or substantial portions of the Software. |
|
14 | 14 | // |
|
15 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16 | 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17 | 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18 | 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19 | 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20 | 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
21 | 21 | // IN THE SOFTWARE. |
|
22 | 22 | |
|
23 | 23 | //! Macros for use in the `hg-cpython` bridge library. |
|
24 | 24 | |
|
25 | 25 | use crate::exceptions::AlreadyBorrowed; |
|
26 | 26 | use cpython::{exc, PyClone, PyErr, PyObject, PyResult, Python}; |
|
27 |
use std::cell::{ |
|
|
27 | use std::cell::{Ref, RefCell, RefMut}; | |
|
28 | 28 | use std::ops::{Deref, DerefMut}; |
|
29 | 29 | use std::sync::atomic::{AtomicUsize, Ordering}; |
|
30 | 30 | |
|
31 | 31 | /// Manages the shared state between Python and Rust |
|
32 | 32 | /// |
|
33 | 33 | /// `PySharedState` is owned by `PySharedRefCell`, and is shared across its |
|
34 | 34 | /// derived references. The consistency of these references are guaranteed |
|
35 | 35 | /// as follows: |
|
36 | 36 | /// |
|
37 | 37 | /// - The immutability of `py_class!` object fields. Any mutation of |
|
38 | 38 | /// `PySharedRefCell` is allowed only through its `borrow_mut()`. |
|
39 | 39 | /// - The `py: Python<'_>` token, which makes sure that any data access is |
|
40 | 40 | /// synchronized by the GIL. |
|
41 | 41 | /// - The underlying `RefCell`, which prevents `PySharedRefCell` data from |
|
42 | 42 | /// being directly borrowed or leaked while it is mutably borrowed. |
|
43 | 43 | /// - The `borrow_count`, which is the number of references borrowed from |
|
44 | 44 | /// `PyLeaked`. Just like `RefCell`, mutation is prohibited while `PyLeaked` |
|
45 | 45 | /// is borrowed. |
|
46 | 46 | /// - The `generation` counter, which increments on `borrow_mut()`. `PyLeaked` |
|
47 | 47 | /// reference is valid only if the `current_generation()` equals to the |
|
48 | 48 | /// `generation` at the time of `leak_immutable()`. |
|
49 | 49 | #[derive(Debug, Default)] |
|
50 | 50 | struct PySharedState { |
|
51 | mutably_borrowed: Cell<bool>, | |
|
52 | 51 | // The counter variable could be Cell<usize> since any operation on |
|
53 | 52 | // PySharedState is synchronized by the GIL, but being "atomic" makes |
|
54 | 53 | // PySharedState inherently Sync. The ordering requirement doesn't |
|
55 | 54 | // matter thanks to the GIL. |
|
56 | 55 | borrow_count: AtomicUsize, |
|
57 | 56 | generation: AtomicUsize, |
|
58 | 57 | } |
|
59 | 58 | |
|
60 | // &PySharedState can be Send because any access to inner cells is | |
|
61 | // synchronized by the GIL. | |
|
62 | unsafe impl Sync for PySharedState {} | |
|
63 | ||
|
64 | 59 | impl PySharedState { |
|
65 | 60 | fn borrow_mut<'a, T>( |
|
66 | 61 | &'a self, |
|
67 | 62 | py: Python<'a>, |
|
68 | 63 | pyrefmut: RefMut<'a, T>, |
|
69 | 64 | ) -> PyResult<PyRefMut<'a, T>> { |
|
70 | if self.mutably_borrowed.get() { | |
|
71 | return Err(AlreadyBorrowed::new( | |
|
72 | py, | |
|
73 | "Cannot borrow mutably while there exists another \ | |
|
74 | mutable reference in a Python object", | |
|
75 | )); | |
|
76 | } | |
|
77 | 65 | match self.current_borrow_count(py) { |
|
78 | 66 | 0 => { |
|
79 | self.mutably_borrowed.replace(true); | |
|
80 | 67 | // Note that this wraps around to the same value if mutably |
|
81 | 68 | // borrowed more than usize::MAX times, which wouldn't happen |
|
82 | 69 | // in practice. |
|
83 | 70 | self.generation.fetch_add(1, Ordering::Relaxed); |
|
84 | 71 | Ok(PyRefMut::new(py, pyrefmut, self)) |
|
85 | 72 | } |
|
86 | 73 | _ => Err(AlreadyBorrowed::new( |
|
87 | 74 | py, |
|
88 | 75 | "Cannot borrow mutably while immutably borrowed", |
|
89 | 76 | )), |
|
90 | 77 | } |
|
91 | 78 | } |
|
92 | 79 | |
|
93 | 80 | /// Return a reference to the wrapped data and its state with an |
|
94 | 81 | /// artificial static lifetime. |
|
95 | 82 | /// We need to be protected by the GIL for thread-safety. |
|
96 | 83 | /// |
|
97 | 84 | /// # Safety |
|
98 | 85 | /// |
|
99 | 86 | /// This is highly unsafe since the lifetime of the given data can be |
|
100 | 87 | /// extended. Do not call this function directly. |
|
101 | 88 | unsafe fn leak_immutable<T>( |
|
102 | 89 | &self, |
|
103 | py: Python, | |
|
90 | _py: Python, | |
|
104 | 91 | data: Ref<T>, |
|
105 | 92 | ) -> PyResult<(&'static T, &'static PySharedState)> { |
|
106 | if self.mutably_borrowed.get() { | |
|
107 | return Err(AlreadyBorrowed::new( | |
|
108 | py, | |
|
109 | "Cannot borrow immutably while there is a \ | |
|
110 | mutable reference in Python objects", | |
|
111 | )); | |
|
112 | } | |
|
113 | 93 | let ptr: *const T = &*data; |
|
114 | 94 | let state_ptr: *const PySharedState = self; |
|
115 | 95 | Ok((&*ptr, &*state_ptr)) |
|
116 | 96 | } |
|
117 | 97 | |
|
118 | 98 | fn current_borrow_count(&self, _py: Python) -> usize { |
|
119 | 99 | self.borrow_count.load(Ordering::Relaxed) |
|
120 | 100 | } |
|
121 | 101 | |
|
122 | 102 | fn increase_borrow_count(&self, _py: Python) { |
|
123 | 103 | // Note that this wraps around if there are more than usize::MAX |
|
124 | 104 | // borrowed references, which shouldn't happen due to memory limit. |
|
125 | 105 | self.borrow_count.fetch_add(1, Ordering::Relaxed); |
|
126 | 106 | } |
|
127 | 107 | |
|
128 | 108 | fn decrease_borrow_count(&self, _py: Python) { |
|
129 | 109 | let prev_count = self.borrow_count.fetch_sub(1, Ordering::Relaxed); |
|
130 | 110 | assert!(prev_count > 0); |
|
131 | 111 | } |
|
132 | 112 | |
|
133 | /// # Safety | |
|
134 | /// | |
|
135 | /// It's up to you to make sure the reference is about to be deleted | |
|
136 | /// when updating the leak count. | |
|
137 | fn decrease_leak_count(&self, py: Python, mutable: bool) { | |
|
138 | if mutable { | |
|
139 | assert_eq!(self.current_borrow_count(py), 0); | |
|
140 | assert!(self.mutably_borrowed.get()); | |
|
141 | self.mutably_borrowed.replace(false); | |
|
142 | } else { | |
|
143 | unimplemented!(); | |
|
144 | } | |
|
145 | } | |
|
146 | ||
|
147 | 113 | fn current_generation(&self, _py: Python) -> usize { |
|
148 | 114 | self.generation.load(Ordering::Relaxed) |
|
149 | 115 | } |
|
150 | 116 | } |
|
151 | 117 | |
|
152 | 118 | /// Helper to keep the borrow count updated while the shared object is |
|
153 | 119 | /// immutably borrowed without using the `RefCell` interface. |
|
154 | 120 | struct BorrowPyShared<'a> { |
|
155 | 121 | py: Python<'a>, |
|
156 | 122 | py_shared_state: &'a PySharedState, |
|
157 | 123 | } |
|
158 | 124 | |
|
159 | 125 | impl<'a> BorrowPyShared<'a> { |
|
160 | 126 | fn new( |
|
161 | 127 | py: Python<'a>, |
|
162 | 128 | py_shared_state: &'a PySharedState, |
|
163 | 129 | ) -> BorrowPyShared<'a> { |
|
164 | 130 | py_shared_state.increase_borrow_count(py); |
|
165 | 131 | BorrowPyShared { |
|
166 | 132 | py, |
|
167 | 133 | py_shared_state, |
|
168 | 134 | } |
|
169 | 135 | } |
|
170 | 136 | } |
|
171 | 137 | |
|
172 | 138 | impl Drop for BorrowPyShared<'_> { |
|
173 | 139 | fn drop(&mut self) { |
|
174 | 140 | self.py_shared_state.decrease_borrow_count(self.py); |
|
175 | 141 | } |
|
176 | 142 | } |
|
177 | 143 | |
|
178 | 144 | /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`. |
|
179 | 145 | /// |
|
180 | 146 | /// This object can be stored in a `py_class!` object as a data field. Any |
|
181 | 147 | /// operation is allowed through the `PySharedRef` interface. |
|
182 | 148 | #[derive(Debug)] |
|
183 | 149 | pub struct PySharedRefCell<T> { |
|
184 | 150 | inner: RefCell<T>, |
|
185 | 151 | py_shared_state: PySharedState, |
|
186 | 152 | } |
|
187 | 153 | |
|
188 | 154 | impl<T> PySharedRefCell<T> { |
|
189 | 155 | pub fn new(value: T) -> PySharedRefCell<T> { |
|
190 | 156 | Self { |
|
191 | 157 | inner: RefCell::new(value), |
|
192 | 158 | py_shared_state: PySharedState::default(), |
|
193 | 159 | } |
|
194 | 160 | } |
|
195 | 161 | |
|
196 | 162 | fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> { |
|
197 | 163 | // py_shared_state isn't involved since |
|
198 | 164 | // - inner.borrow() would fail if self is mutably borrowed, |
|
199 | 165 | // - and inner.borrow_mut() would fail while self is borrowed. |
|
200 | 166 | self.inner.borrow() |
|
201 | 167 | } |
|
202 | 168 | |
|
203 | 169 | // TODO: maybe this should be named as try_borrow_mut(), and use |
|
204 | 170 | // inner.try_borrow_mut(). The current implementation panics if |
|
205 | 171 | // self.inner has been borrowed, but returns error if py_shared_state |
|
206 | 172 | // refuses to borrow. |
|
207 | 173 | fn borrow_mut<'a>(&'a self, py: Python<'a>) -> PyResult<PyRefMut<'a, T>> { |
|
208 | 174 | self.py_shared_state.borrow_mut(py, self.inner.borrow_mut()) |
|
209 | 175 | } |
|
210 | 176 | } |
|
211 | 177 | |
|
212 | 178 | /// Sharable data member of type `T` borrowed from the `PyObject`. |
|
213 | 179 | pub struct PySharedRef<'a, T> { |
|
214 | 180 | py: Python<'a>, |
|
215 | 181 | owner: &'a PyObject, |
|
216 | 182 | data: &'a PySharedRefCell<T>, |
|
217 | 183 | } |
|
218 | 184 | |
|
219 | 185 | impl<'a, T> PySharedRef<'a, T> { |
|
220 | 186 | /// # Safety |
|
221 | 187 | /// |
|
222 | 188 | /// The `data` must be owned by the `owner`. Otherwise, the leak count |
|
223 | 189 | /// would get wrong. |
|
224 | 190 | pub unsafe fn new( |
|
225 | 191 | py: Python<'a>, |
|
226 | 192 | owner: &'a PyObject, |
|
227 | 193 | data: &'a PySharedRefCell<T>, |
|
228 | 194 | ) -> Self { |
|
229 | 195 | Self { py, owner, data } |
|
230 | 196 | } |
|
231 | 197 | |
|
232 | 198 | pub fn borrow(&self) -> Ref<'a, T> { |
|
233 | 199 | self.data.borrow(self.py) |
|
234 | 200 | } |
|
235 | 201 | |
|
236 | 202 | pub fn borrow_mut(&self) -> PyResult<PyRefMut<'a, T>> { |
|
237 | 203 | self.data.borrow_mut(self.py) |
|
238 | 204 | } |
|
239 | 205 | |
|
240 | 206 | /// Returns a leaked reference. |
|
241 | 207 | /// |
|
242 | 208 | /// # Panics |
|
243 | 209 | /// |
|
244 | 210 | /// Panics if this is mutably borrowed. |
|
245 | 211 | pub fn leak_immutable(&self) -> PyResult<PyLeaked<&'static T>> { |
|
246 | 212 | let state = &self.data.py_shared_state; |
|
247 | 213 | // make sure self.data isn't mutably borrowed; otherwise the |
|
248 | 214 | // generation number can't be trusted. |
|
249 | 215 | let data_ref = self.borrow(); |
|
250 | 216 | unsafe { |
|
251 | 217 | let (static_ref, static_state_ref) = |
|
252 | 218 | state.leak_immutable(self.py, data_ref)?; |
|
253 | 219 | Ok(PyLeaked::new( |
|
254 | 220 | self.py, |
|
255 | 221 | self.owner, |
|
256 | 222 | static_ref, |
|
257 | 223 | static_state_ref, |
|
258 | 224 | )) |
|
259 | 225 | } |
|
260 | 226 | } |
|
261 | 227 | } |
|
262 | 228 | |
|
263 | 229 | /// Holds a mutable reference to data shared between Python and Rust. |
|
264 | 230 | pub struct PyRefMut<'a, T> { |
|
265 | py: Python<'a>, | |
|
266 | 231 | inner: RefMut<'a, T>, |
|
267 | py_shared_state: &'a PySharedState, | |
|
268 | 232 | } |
|
269 | 233 | |
|
270 | 234 | impl<'a, T> PyRefMut<'a, T> { |
|
271 | 235 | // Must be constructed by PySharedState after checking its leak_count. |
|
272 | 236 | // Otherwise, drop() would incorrectly update the state. |
|
273 | 237 | fn new( |
|
274 | py: Python<'a>, | |
|
238 | _py: Python<'a>, | |
|
275 | 239 | inner: RefMut<'a, T>, |
|
276 | py_shared_state: &'a PySharedState, | |
|
240 | _py_shared_state: &'a PySharedState, | |
|
277 | 241 | ) -> Self { |
|
278 | 242 | Self { |
|
279 | py, | |
|
280 | 243 | inner, |
|
281 | py_shared_state, | |
|
282 | 244 | } |
|
283 | 245 | } |
|
284 | 246 | } |
|
285 | 247 | |
|
286 | 248 | impl<'a, T> std::ops::Deref for PyRefMut<'a, T> { |
|
287 | 249 | type Target = RefMut<'a, T>; |
|
288 | 250 | |
|
289 | 251 | fn deref(&self) -> &Self::Target { |
|
290 | 252 | &self.inner |
|
291 | 253 | } |
|
292 | 254 | } |
|
293 | 255 | impl<'a, T> std::ops::DerefMut for PyRefMut<'a, T> { |
|
294 | 256 | fn deref_mut(&mut self) -> &mut Self::Target { |
|
295 | 257 | &mut self.inner |
|
296 | 258 | } |
|
297 | 259 | } |
|
298 | 260 | |
|
299 | impl<'a, T> Drop for PyRefMut<'a, T> { | |
|
300 | fn drop(&mut self) { | |
|
301 | self.py_shared_state.decrease_leak_count(self.py, true); | |
|
302 | } | |
|
303 | } | |
|
304 | ||
|
305 | 261 | /// Allows a `py_class!` generated struct to share references to one of its |
|
306 | 262 | /// data members with Python. |
|
307 | 263 | /// |
|
308 | 264 | /// # Parameters |
|
309 | 265 | /// |
|
310 | 266 | /// * `$name` is the same identifier used in for `py_class!` macro call. |
|
311 | 267 | /// * `$inner_struct` is the identifier of the underlying Rust struct |
|
312 | 268 | /// * `$data_member` is the identifier of the data member of `$inner_struct` |
|
313 | 269 | /// that will be shared. |
|
314 | 270 | /// * `$shared_accessor` is the function name to be generated, which allows |
|
315 | 271 | /// safe access to the data member. |
|
316 | 272 | /// |
|
317 | 273 | /// # Safety |
|
318 | 274 | /// |
|
319 | 275 | /// `$data_member` must persist while the `$name` object is alive. In other |
|
320 | 276 | /// words, it must be an accessor to a data field of the Python object. |
|
321 | 277 | /// |
|
322 | 278 | /// # Example |
|
323 | 279 | /// |
|
324 | 280 | /// ``` |
|
325 | 281 | /// struct MyStruct { |
|
326 | 282 | /// inner: Vec<u32>; |
|
327 | 283 | /// } |
|
328 | 284 | /// |
|
329 | 285 | /// py_class!(pub class MyType |py| { |
|
330 | 286 | /// data inner: PySharedRefCell<MyStruct>; |
|
331 | 287 | /// }); |
|
332 | 288 | /// |
|
333 | 289 | /// py_shared_ref!(MyType, MyStruct, inner, inner_shared); |
|
334 | 290 | /// ``` |
|
335 | 291 | macro_rules! py_shared_ref { |
|
336 | 292 | ( |
|
337 | 293 | $name: ident, |
|
338 | 294 | $inner_struct: ident, |
|
339 | 295 | $data_member: ident, |
|
340 | 296 | $shared_accessor: ident |
|
341 | 297 | ) => { |
|
342 | 298 | impl $name { |
|
343 | 299 | /// Returns a safe reference to the shared `$data_member`. |
|
344 | 300 | /// |
|
345 | 301 | /// This function guarantees that `PySharedRef` is created with |
|
346 | 302 | /// the valid `self` and `self.$data_member(py)` pair. |
|
347 | 303 | fn $shared_accessor<'a>( |
|
348 | 304 | &'a self, |
|
349 | 305 | py: Python<'a>, |
|
350 | 306 | ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> { |
|
351 | 307 | use cpython::PythonObject; |
|
352 | 308 | use $crate::ref_sharing::PySharedRef; |
|
353 | 309 | let owner = self.as_object(); |
|
354 | 310 | let data = self.$data_member(py); |
|
355 | 311 | unsafe { PySharedRef::new(py, owner, data) } |
|
356 | 312 | } |
|
357 | 313 | } |
|
358 | 314 | }; |
|
359 | 315 | } |
|
360 | 316 | |
|
361 | 317 | /// Manage immutable references to `PyObject` leaked into Python iterators. |
|
362 | 318 | /// |
|
363 | 319 | /// This reference will be invalidated once the original value is mutably |
|
364 | 320 | /// borrowed. |
|
365 | 321 | pub struct PyLeaked<T> { |
|
366 | 322 | inner: PyObject, |
|
367 | 323 | data: Option<T>, |
|
368 | 324 | py_shared_state: &'static PySharedState, |
|
369 | 325 | /// Generation counter of data `T` captured when PyLeaked is created. |
|
370 | 326 | generation: usize, |
|
371 | 327 | } |
|
372 | 328 | |
|
373 | 329 | // DO NOT implement Deref for PyLeaked<T>! Dereferencing PyLeaked |
|
374 | 330 | // without taking Python GIL wouldn't be safe. Also, the underling reference |
|
375 | 331 | // is invalid if generation != py_shared_state.generation. |
|
376 | 332 | |
|
377 | 333 | impl<T> PyLeaked<T> { |
|
378 | 334 | /// # Safety |
|
379 | 335 | /// |
|
380 | 336 | /// The `py_shared_state` must be owned by the `inner` Python object. |
|
381 | 337 | fn new( |
|
382 | 338 | py: Python, |
|
383 | 339 | inner: &PyObject, |
|
384 | 340 | data: T, |
|
385 | 341 | py_shared_state: &'static PySharedState, |
|
386 | 342 | ) -> Self { |
|
387 | 343 | Self { |
|
388 | 344 | inner: inner.clone_ref(py), |
|
389 | 345 | data: Some(data), |
|
390 | 346 | py_shared_state, |
|
391 | 347 | generation: py_shared_state.current_generation(py), |
|
392 | 348 | } |
|
393 | 349 | } |
|
394 | 350 | |
|
395 | 351 | /// Immutably borrows the wrapped value. |
|
396 | 352 | /// |
|
397 | 353 | /// Borrowing fails if the underlying reference has been invalidated. |
|
398 | 354 | pub fn try_borrow<'a>( |
|
399 | 355 | &'a self, |
|
400 | 356 | py: Python<'a>, |
|
401 | 357 | ) -> PyResult<PyLeakedRef<'a, T>> { |
|
402 | 358 | self.validate_generation(py)?; |
|
403 | 359 | Ok(PyLeakedRef { |
|
404 | 360 | _borrow: BorrowPyShared::new(py, self.py_shared_state), |
|
405 | 361 | data: self.data.as_ref().unwrap(), |
|
406 | 362 | }) |
|
407 | 363 | } |
|
408 | 364 | |
|
409 | 365 | /// Mutably borrows the wrapped value. |
|
410 | 366 | /// |
|
411 | 367 | /// Borrowing fails if the underlying reference has been invalidated. |
|
412 | 368 | /// |
|
413 | 369 | /// Typically `T` is an iterator. If `T` is an immutable reference, |
|
414 | 370 | /// `get_mut()` is useless since the inner value can't be mutated. |
|
415 | 371 | pub fn try_borrow_mut<'a>( |
|
416 | 372 | &'a mut self, |
|
417 | 373 | py: Python<'a>, |
|
418 | 374 | ) -> PyResult<PyLeakedRefMut<'a, T>> { |
|
419 | 375 | self.validate_generation(py)?; |
|
420 | 376 | Ok(PyLeakedRefMut { |
|
421 | 377 | _borrow: BorrowPyShared::new(py, self.py_shared_state), |
|
422 | 378 | data: self.data.as_mut().unwrap(), |
|
423 | 379 | }) |
|
424 | 380 | } |
|
425 | 381 | |
|
426 | 382 | /// Converts the inner value by the given function. |
|
427 | 383 | /// |
|
428 | 384 | /// Typically `T` is a static reference to a container, and `U` is an |
|
429 | 385 | /// iterator of that container. |
|
430 | 386 | /// |
|
431 | 387 | /// # Panics |
|
432 | 388 | /// |
|
433 | 389 | /// Panics if the underlying reference has been invalidated. |
|
434 | 390 | /// |
|
435 | 391 | /// This is typically called immediately after the `PyLeaked` is obtained. |
|
436 | 392 | /// In which case, the reference must be valid and no panic would occur. |
|
437 | 393 | /// |
|
438 | 394 | /// # Safety |
|
439 | 395 | /// |
|
440 | 396 | /// The lifetime of the object passed in to the function `f` is cheated. |
|
441 | 397 | /// It's typically a static reference, but is valid only while the |
|
442 | 398 | /// corresponding `PyLeaked` is alive. Do not copy it out of the |
|
443 | 399 | /// function call. |
|
444 | 400 | pub unsafe fn map<U>( |
|
445 | 401 | mut self, |
|
446 | 402 | py: Python, |
|
447 | 403 | f: impl FnOnce(T) -> U, |
|
448 | 404 | ) -> PyLeaked<U> { |
|
449 | 405 | // Needs to test the generation value to make sure self.data reference |
|
450 | 406 | // is still intact. |
|
451 | 407 | self.validate_generation(py) |
|
452 | 408 | .expect("map() over invalidated leaked reference"); |
|
453 | 409 | |
|
454 | 410 | // f() could make the self.data outlive. That's why map() is unsafe. |
|
455 | 411 | // In order to make this function safe, maybe we'll need a way to |
|
456 | 412 | // temporarily restrict the lifetime of self.data and translate the |
|
457 | 413 | // returned object back to Something<'static>. |
|
458 | 414 | let new_data = f(self.data.take().unwrap()); |
|
459 | 415 | PyLeaked { |
|
460 | 416 | inner: self.inner.clone_ref(py), |
|
461 | 417 | data: Some(new_data), |
|
462 | 418 | py_shared_state: self.py_shared_state, |
|
463 | 419 | generation: self.generation, |
|
464 | 420 | } |
|
465 | 421 | } |
|
466 | 422 | |
|
467 | 423 | fn validate_generation(&self, py: Python) -> PyResult<()> { |
|
468 | 424 | if self.py_shared_state.current_generation(py) == self.generation { |
|
469 | 425 | Ok(()) |
|
470 | 426 | } else { |
|
471 | 427 | Err(PyErr::new::<exc::RuntimeError, _>( |
|
472 | 428 | py, |
|
473 | 429 | "Cannot access to leaked reference after mutation", |
|
474 | 430 | )) |
|
475 | 431 | } |
|
476 | 432 | } |
|
477 | 433 | } |
|
478 | 434 | |
|
479 | 435 | /// Immutably borrowed reference to a leaked value. |
|
480 | 436 | pub struct PyLeakedRef<'a, T> { |
|
481 | 437 | _borrow: BorrowPyShared<'a>, |
|
482 | 438 | data: &'a T, |
|
483 | 439 | } |
|
484 | 440 | |
|
485 | 441 | impl<T> Deref for PyLeakedRef<'_, T> { |
|
486 | 442 | type Target = T; |
|
487 | 443 | |
|
488 | 444 | fn deref(&self) -> &T { |
|
489 | 445 | self.data |
|
490 | 446 | } |
|
491 | 447 | } |
|
492 | 448 | |
|
493 | 449 | /// Mutably borrowed reference to a leaked value. |
|
494 | 450 | pub struct PyLeakedRefMut<'a, T> { |
|
495 | 451 | _borrow: BorrowPyShared<'a>, |
|
496 | 452 | data: &'a mut T, |
|
497 | 453 | } |
|
498 | 454 | |
|
499 | 455 | impl<T> Deref for PyLeakedRefMut<'_, T> { |
|
500 | 456 | type Target = T; |
|
501 | 457 | |
|
502 | 458 | fn deref(&self) -> &T { |
|
503 | 459 | self.data |
|
504 | 460 | } |
|
505 | 461 | } |
|
506 | 462 | |
|
507 | 463 | impl<T> DerefMut for PyLeakedRefMut<'_, T> { |
|
508 | 464 | fn deref_mut(&mut self) -> &mut T { |
|
509 | 465 | self.data |
|
510 | 466 | } |
|
511 | 467 | } |
|
512 | 468 | |
|
513 | 469 | /// Defines a `py_class!` that acts as a Python iterator over a Rust iterator. |
|
514 | 470 | /// |
|
515 | 471 | /// TODO: this is a bit awkward to use, and a better (more complicated) |
|
516 | 472 | /// procedural macro would simplify the interface a lot. |
|
517 | 473 | /// |
|
518 | 474 | /// # Parameters |
|
519 | 475 | /// |
|
520 | 476 | /// * `$name` is the identifier to give to the resulting Rust struct. |
|
521 | 477 | /// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call. |
|
522 | 478 | /// * `$iterator_type` is the type of the Rust iterator. |
|
523 | 479 | /// * `$success_func` is a function for processing the Rust `(key, value)` |
|
524 | 480 | /// tuple on iteration success, turning it into something Python understands. |
|
525 | 481 | /// * `$success_func` is the return type of `$success_func` |
|
526 | 482 | /// |
|
527 | 483 | /// # Example |
|
528 | 484 | /// |
|
529 | 485 | /// ``` |
|
530 | 486 | /// struct MyStruct { |
|
531 | 487 | /// inner: HashMap<Vec<u8>, Vec<u8>>; |
|
532 | 488 | /// } |
|
533 | 489 | /// |
|
534 | 490 | /// py_class!(pub class MyType |py| { |
|
535 | 491 | /// data inner: PySharedRefCell<MyStruct>; |
|
536 | 492 | /// |
|
537 | 493 | /// def __iter__(&self) -> PyResult<MyTypeItemsIterator> { |
|
538 | 494 | /// let leaked_ref = self.inner_shared(py).leak_immutable()?; |
|
539 | 495 | /// MyTypeItemsIterator::from_inner( |
|
540 | 496 | /// py, |
|
541 | 497 | /// unsafe { leaked_ref.map(py, |o| o.iter()) }, |
|
542 | 498 | /// ) |
|
543 | 499 | /// } |
|
544 | 500 | /// }); |
|
545 | 501 | /// |
|
546 | 502 | /// impl MyType { |
|
547 | 503 | /// fn translate_key_value( |
|
548 | 504 | /// py: Python, |
|
549 | 505 | /// res: (&Vec<u8>, &Vec<u8>), |
|
550 | 506 | /// ) -> PyResult<Option<(PyBytes, PyBytes)>> { |
|
551 | 507 | /// let (f, entry) = res; |
|
552 | 508 | /// Ok(Some(( |
|
553 | 509 | /// PyBytes::new(py, f), |
|
554 | 510 | /// PyBytes::new(py, entry), |
|
555 | 511 | /// ))) |
|
556 | 512 | /// } |
|
557 | 513 | /// } |
|
558 | 514 | /// |
|
559 | 515 | /// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef); |
|
560 | 516 | /// |
|
561 | 517 | /// py_shared_iterator!( |
|
562 | 518 | /// MyTypeItemsIterator, |
|
563 | 519 | /// PyLeaked<HashMap<'static, Vec<u8>, Vec<u8>>>, |
|
564 | 520 | /// MyType::translate_key_value, |
|
565 | 521 | /// Option<(PyBytes, PyBytes)> |
|
566 | 522 | /// ); |
|
567 | 523 | /// ``` |
|
568 | 524 | macro_rules! py_shared_iterator { |
|
569 | 525 | ( |
|
570 | 526 | $name: ident, |
|
571 | 527 | $leaked: ty, |
|
572 | 528 | $success_func: expr, |
|
573 | 529 | $success_type: ty |
|
574 | 530 | ) => { |
|
575 | 531 | py_class!(pub class $name |py| { |
|
576 | 532 | data inner: RefCell<$leaked>; |
|
577 | 533 | |
|
578 | 534 | def __next__(&self) -> PyResult<$success_type> { |
|
579 | 535 | let mut leaked = self.inner(py).borrow_mut(); |
|
580 | 536 | let mut iter = leaked.try_borrow_mut(py)?; |
|
581 | 537 | match iter.next() { |
|
582 | 538 | None => Ok(None), |
|
583 | 539 | Some(res) => $success_func(py, res), |
|
584 | 540 | } |
|
585 | 541 | } |
|
586 | 542 | |
|
587 | 543 | def __iter__(&self) -> PyResult<Self> { |
|
588 | 544 | Ok(self.clone_ref(py)) |
|
589 | 545 | } |
|
590 | 546 | }); |
|
591 | 547 | |
|
592 | 548 | impl $name { |
|
593 | 549 | pub fn from_inner( |
|
594 | 550 | py: Python, |
|
595 | 551 | leaked: $leaked, |
|
596 | 552 | ) -> PyResult<Self> { |
|
597 | 553 | Self::create_instance( |
|
598 | 554 | py, |
|
599 | 555 | RefCell::new(leaked), |
|
600 | 556 | ) |
|
601 | 557 | } |
|
602 | 558 | } |
|
603 | 559 | }; |
|
604 | 560 | } |
|
605 | 561 | |
|
606 | 562 | #[cfg(test)] |
|
607 | 563 | #[cfg(any(feature = "python27-bin", feature = "python3-bin"))] |
|
608 | 564 | mod test { |
|
609 | 565 | use super::*; |
|
610 | 566 | use cpython::{GILGuard, Python}; |
|
611 | 567 | |
|
612 | 568 | py_class!(class Owner |py| { |
|
613 | 569 | data string: PySharedRefCell<String>; |
|
614 | 570 | }); |
|
615 | 571 | py_shared_ref!(Owner, String, string, string_shared); |
|
616 | 572 | |
|
617 | 573 | fn prepare_env() -> (GILGuard, Owner) { |
|
618 | 574 | let gil = Python::acquire_gil(); |
|
619 | 575 | let py = gil.python(); |
|
620 | 576 | let owner = |
|
621 | 577 | Owner::create_instance(py, PySharedRefCell::new("new".to_owned())) |
|
622 | 578 | .unwrap(); |
|
623 | 579 | (gil, owner) |
|
624 | 580 | } |
|
625 | 581 | |
|
626 | 582 | #[test] |
|
627 | 583 | fn test_leaked_borrow() { |
|
628 | 584 | let (gil, owner) = prepare_env(); |
|
629 | 585 | let py = gil.python(); |
|
630 | 586 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
631 | 587 | let leaked_ref = leaked.try_borrow(py).unwrap(); |
|
632 | 588 | assert_eq!(*leaked_ref, "new"); |
|
633 | 589 | } |
|
634 | 590 | |
|
635 | 591 | #[test] |
|
636 | 592 | fn test_leaked_borrow_mut() { |
|
637 | 593 | let (gil, owner) = prepare_env(); |
|
638 | 594 | let py = gil.python(); |
|
639 | 595 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
640 | 596 | let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; |
|
641 | 597 | let mut leaked_ref = leaked_iter.try_borrow_mut(py).unwrap(); |
|
642 | 598 | assert_eq!(leaked_ref.next(), Some('n')); |
|
643 | 599 | assert_eq!(leaked_ref.next(), Some('e')); |
|
644 | 600 | assert_eq!(leaked_ref.next(), Some('w')); |
|
645 | 601 | assert_eq!(leaked_ref.next(), None); |
|
646 | 602 | } |
|
647 | 603 | |
|
648 | 604 | #[test] |
|
649 | 605 | fn test_leaked_borrow_after_mut() { |
|
650 | 606 | let (gil, owner) = prepare_env(); |
|
651 | 607 | let py = gil.python(); |
|
652 | 608 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
653 | 609 | owner.string_shared(py).borrow_mut().unwrap().clear(); |
|
654 | 610 | assert!(leaked.try_borrow(py).is_err()); |
|
655 | 611 | } |
|
656 | 612 | |
|
657 | 613 | #[test] |
|
658 | 614 | fn test_leaked_borrow_mut_after_mut() { |
|
659 | 615 | let (gil, owner) = prepare_env(); |
|
660 | 616 | let py = gil.python(); |
|
661 | 617 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
662 | 618 | let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; |
|
663 | 619 | owner.string_shared(py).borrow_mut().unwrap().clear(); |
|
664 | 620 | assert!(leaked_iter.try_borrow_mut(py).is_err()); |
|
665 | 621 | } |
|
666 | 622 | |
|
667 | 623 | #[test] |
|
668 | 624 | #[should_panic(expected = "map() over invalidated leaked reference")] |
|
669 | 625 | fn test_leaked_map_after_mut() { |
|
670 | 626 | let (gil, owner) = prepare_env(); |
|
671 | 627 | let py = gil.python(); |
|
672 | 628 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
673 | 629 | owner.string_shared(py).borrow_mut().unwrap().clear(); |
|
674 | 630 | let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; |
|
675 | 631 | } |
|
676 | 632 | |
|
677 | 633 | #[test] |
|
678 | 634 | fn test_borrow_mut_while_leaked_ref() { |
|
679 | 635 | let (gil, owner) = prepare_env(); |
|
680 | 636 | let py = gil.python(); |
|
681 | 637 | assert!(owner.string_shared(py).borrow_mut().is_ok()); |
|
682 | 638 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
683 | 639 | { |
|
684 | 640 | let _leaked_ref = leaked.try_borrow(py).unwrap(); |
|
685 | 641 | assert!(owner.string_shared(py).borrow_mut().is_err()); |
|
686 | 642 | { |
|
687 | 643 | let _leaked_ref2 = leaked.try_borrow(py).unwrap(); |
|
688 | 644 | assert!(owner.string_shared(py).borrow_mut().is_err()); |
|
689 | 645 | } |
|
690 | 646 | assert!(owner.string_shared(py).borrow_mut().is_err()); |
|
691 | 647 | } |
|
692 | 648 | assert!(owner.string_shared(py).borrow_mut().is_ok()); |
|
693 | 649 | } |
|
694 | 650 | |
|
695 | 651 | #[test] |
|
696 | 652 | fn test_borrow_mut_while_leaked_ref_mut() { |
|
697 | 653 | let (gil, owner) = prepare_env(); |
|
698 | 654 | let py = gil.python(); |
|
699 | 655 | assert!(owner.string_shared(py).borrow_mut().is_ok()); |
|
700 | 656 | let leaked = owner.string_shared(py).leak_immutable().unwrap(); |
|
701 | 657 | let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; |
|
702 | 658 | { |
|
703 | 659 | let _leaked_ref = leaked_iter.try_borrow_mut(py).unwrap(); |
|
704 | 660 | assert!(owner.string_shared(py).borrow_mut().is_err()); |
|
705 | 661 | } |
|
706 | 662 | assert!(owner.string_shared(py).borrow_mut().is_ok()); |
|
707 | 663 | } |
|
708 | 664 | |
|
709 | 665 | #[test] |
|
710 | 666 | #[should_panic(expected = "mutably borrowed")] |
|
711 | 667 | fn test_leak_while_borrow_mut() { |
|
712 | 668 | let (gil, owner) = prepare_env(); |
|
713 | 669 | let py = gil.python(); |
|
714 | 670 | let _mut_ref = owner.string_shared(py).borrow_mut(); |
|
715 | 671 | let _ = owner.string_shared(py).leak_immutable(); |
|
716 | 672 | } |
|
717 | 673 | } |
General Comments 0
You need to be logged in to leave comments.
Login now