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