##// END OF EJS Templates
rust-cpython: make PyLeakedRef operations relatively safe...
Yuya Nishihara -
r43579:ffc1fbd7 default
parent child Browse files
Show More
@@ -1,120 +1,117 b''
1 1 // copymap.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for `hg::dirstate::dirstate_map::CopyMap` provided by the
9 9 //! `hg-core` package.
10 10
11 11 use cpython::{PyBytes, PyClone, PyDict, PyObject, PyResult, Python};
12 12 use std::cell::RefCell;
13 13
14 14 use crate::dirstate::dirstate_map::DirstateMap;
15 15 use crate::ref_sharing::PyLeakedRef;
16 use hg::DirstateMap as RustDirstateMap;
17 16 use hg::{utils::hg_path::HgPathBuf, CopyMapIter};
18 17
19 18 py_class!(pub class CopyMap |py| {
20 19 data dirstate_map: DirstateMap;
21 20
22 21 def __getitem__(&self, key: PyObject) -> PyResult<PyBytes> {
23 22 (*self.dirstate_map(py)).copymapgetitem(py, key)
24 23 }
25 24
26 25 def __len__(&self) -> PyResult<usize> {
27 26 self.dirstate_map(py).copymaplen(py)
28 27 }
29 28
30 29 def __contains__(&self, key: PyObject) -> PyResult<bool> {
31 30 self.dirstate_map(py).copymapcontains(py, key)
32 31 }
33 32
34 33 def get(
35 34 &self,
36 35 key: PyObject,
37 36 default: Option<PyObject> = None
38 37 ) -> PyResult<Option<PyObject>> {
39 38 self.dirstate_map(py).copymapget(py, key, default)
40 39 }
41 40
42 41 def pop(
43 42 &self,
44 43 key: PyObject,
45 44 default: Option<PyObject> = None
46 45 ) -> PyResult<Option<PyObject>> {
47 46 self.dirstate_map(py).copymappop(py, key, default)
48 47 }
49 48
50 49 def __iter__(&self) -> PyResult<CopyMapKeysIterator> {
51 50 self.dirstate_map(py).copymapiter(py)
52 51 }
53 52
54 53 // Python's `dict()` builtin works with either a subclass of dict
55 54 // or an abstract mapping. Said mapping needs to implement `__getitem__`
56 55 // and `keys`.
57 56 def keys(&self) -> PyResult<CopyMapKeysIterator> {
58 57 self.dirstate_map(py).copymapiter(py)
59 58 }
60 59
61 60 def items(&self) -> PyResult<CopyMapItemsIterator> {
62 61 self.dirstate_map(py).copymapitemsiter(py)
63 62 }
64 63
65 64 def iteritems(&self) -> PyResult<CopyMapItemsIterator> {
66 65 self.dirstate_map(py).copymapitemsiter(py)
67 66 }
68 67
69 68 def __setitem__(
70 69 &self,
71 70 key: PyObject,
72 71 item: PyObject
73 72 ) -> PyResult<()> {
74 73 self.dirstate_map(py).copymapsetitem(py, key, item)?;
75 74 Ok(())
76 75 }
77 76
78 77 def copy(&self) -> PyResult<PyDict> {
79 78 self.dirstate_map(py).copymapcopy(py)
80 79 }
81 80
82 81 });
83 82
84 83 impl CopyMap {
85 84 pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
86 85 Self::create_instance(py, dm)
87 86 }
88 87 fn translate_key(
89 88 py: Python,
90 89 res: (&HgPathBuf, &HgPathBuf),
91 90 ) -> PyResult<Option<PyBytes>> {
92 91 Ok(Some(PyBytes::new(py, res.0.as_ref())))
93 92 }
94 93 fn translate_key_value(
95 94 py: Python,
96 95 res: (&HgPathBuf, &HgPathBuf),
97 96 ) -> PyResult<Option<(PyBytes, PyBytes)>> {
98 97 let (k, v) = res;
99 98 Ok(Some((
100 99 PyBytes::new(py, k.as_ref()),
101 100 PyBytes::new(py, v.as_ref()),
102 101 )))
103 102 }
104 103 }
105 104
106 105 py_shared_iterator!(
107 106 CopyMapKeysIterator,
108 PyLeakedRef<&'static RustDirstateMap>,
109 CopyMapIter<'static>,
107 PyLeakedRef<CopyMapIter<'static>>,
110 108 CopyMap::translate_key,
111 109 Option<PyBytes>
112 110 );
113 111
114 112 py_shared_iterator!(
115 113 CopyMapItemsIterator,
116 PyLeakedRef<&'static RustDirstateMap>,
117 CopyMapIter<'static>,
114 PyLeakedRef<CopyMapIter<'static>>,
118 115 CopyMap::translate_key_value,
119 116 Option<(PyBytes, PyBytes)>
120 117 );
@@ -1,133 +1,129 b''
1 1 // dirs_multiset.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for the `hg::dirstate::dirs_multiset` file provided by the
9 9 //! `hg-core` package.
10 10
11 11 use std::cell::RefCell;
12 12 use std::convert::TryInto;
13 13
14 14 use cpython::{
15 15 exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult,
16 16 Python,
17 17 };
18 18
19 19 use crate::dirstate::extract_dirstate;
20 20 use crate::ref_sharing::{PyLeakedRef, PySharedRefCell};
21 21 use hg::{
22 22 utils::hg_path::{HgPath, HgPathBuf},
23 23 DirsMultiset, DirsMultisetIter, DirstateMapError, DirstateParseError,
24 24 EntryState,
25 25 };
26 26
27 27 py_class!(pub class Dirs |py| {
28 28 data inner: PySharedRefCell<DirsMultiset>;
29 29
30 30 // `map` is either a `dict` or a flat iterator (usually a `set`, sometimes
31 31 // a `list`)
32 32 def __new__(
33 33 _cls,
34 34 map: PyObject,
35 35 skip: Option<PyObject> = None
36 36 ) -> PyResult<Self> {
37 37 let mut skip_state: Option<EntryState> = None;
38 38 if let Some(skip) = skip {
39 39 skip_state = Some(
40 40 skip.extract::<PyBytes>(py)?.data(py)[0]
41 41 .try_into()
42 42 .map_err(|e: DirstateParseError| {
43 43 PyErr::new::<exc::ValueError, _>(py, e.to_string())
44 44 })?,
45 45 );
46 46 }
47 47 let inner = if let Ok(map) = map.cast_as::<PyDict>(py) {
48 48 let dirstate = extract_dirstate(py, &map)?;
49 49 DirsMultiset::from_dirstate(&dirstate, skip_state)
50 50 } else {
51 51 let map: Result<Vec<HgPathBuf>, PyErr> = map
52 52 .iter(py)?
53 53 .map(|o| {
54 54 Ok(HgPathBuf::from_bytes(
55 55 o?.extract::<PyBytes>(py)?.data(py),
56 56 ))
57 57 })
58 58 .collect();
59 59 DirsMultiset::from_manifest(&map?)
60 60 };
61 61
62 62 Self::create_instance(
63 63 py,
64 64 PySharedRefCell::new(inner),
65 65 )
66 66 }
67 67
68 68 def addpath(&self, path: PyObject) -> PyResult<PyObject> {
69 69 self.inner_shared(py).borrow_mut()?.add_path(
70 70 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
71 71 );
72 72 Ok(py.None())
73 73 }
74 74
75 75 def delpath(&self, path: PyObject) -> PyResult<PyObject> {
76 76 self.inner_shared(py).borrow_mut()?.delete_path(
77 77 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
78 78 )
79 79 .and(Ok(py.None()))
80 80 .or_else(|e| {
81 81 match e {
82 82 DirstateMapError::PathNotFound(_p) => {
83 83 Err(PyErr::new::<exc::ValueError, _>(
84 84 py,
85 85 "expected a value, found none".to_string(),
86 86 ))
87 87 }
88 88 DirstateMapError::EmptyPath => {
89 89 Ok(py.None())
90 90 }
91 91 }
92 92 })
93 93 }
94 94 def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> {
95 let mut leak_handle =
96 unsafe { self.inner_shared(py).leak_immutable()? };
97 let leaked_ref = leak_handle.data.take().unwrap();
95 let leaked_ref = self.inner_shared(py).leak_immutable()?;
98 96 DirsMultisetKeysIterator::from_inner(
99 97 py,
100 leak_handle,
101 leaked_ref.iter(),
98 unsafe { leaked_ref.map(py, |o| o.iter()) },
102 99 )
103 100 }
104 101
105 102 def __contains__(&self, item: PyObject) -> PyResult<bool> {
106 103 Ok(self.inner(py).borrow().contains(HgPath::new(
107 104 item.extract::<PyBytes>(py)?.data(py).as_ref(),
108 105 )))
109 106 }
110 107 });
111 108
112 109 py_shared_ref!(Dirs, DirsMultiset, inner, inner_shared);
113 110
114 111 impl Dirs {
115 112 pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
116 113 Self::create_instance(py, PySharedRefCell::new(d))
117 114 }
118 115
119 116 fn translate_key(
120 117 py: Python,
121 118 res: &HgPathBuf,
122 119 ) -> PyResult<Option<PyBytes>> {
123 120 Ok(Some(PyBytes::new(py, res.as_ref())))
124 121 }
125 122 }
126 123
127 124 py_shared_iterator!(
128 125 DirsMultisetKeysIterator,
129 PyLeakedRef<&'static DirsMultiset>,
130 DirsMultisetIter<'static>,
126 PyLeakedRef<DirsMultisetIter<'static>>,
131 127 Dirs::translate_key,
132 128 Option<PyBytes>
133 129 );
@@ -1,521 +1,504 b''
1 1 // dirstate_map.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 9 //! `hg-core` package.
10 10
11 11 use std::cell::{Ref, RefCell};
12 12 use std::convert::TryInto;
13 13 use std::time::Duration;
14 14
15 15 use cpython::{
16 16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
17 17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
18 18 };
19 19
20 20 use crate::{
21 21 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
22 22 dirstate::{dirs_multiset::Dirs, make_dirstate_tuple},
23 23 ref_sharing::{PyLeakedRef, PySharedRefCell},
24 24 };
25 25 use hg::{
26 26 utils::hg_path::{HgPath, HgPathBuf},
27 27 DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
28 28 DirstateParents, DirstateParseError, EntryState, StateMapIter,
29 29 PARENT_SIZE,
30 30 };
31 31
32 32 // TODO
33 33 // This object needs to share references to multiple members of its Rust
34 34 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
35 35 // Right now `CopyMap` is done, but it needs to have an explicit reference
36 36 // to `RustDirstateMap` which itself needs to have an encapsulation for
37 37 // every method in `CopyMap` (copymapcopy, etc.).
38 38 // This is ugly and hard to maintain.
39 39 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
40 40 // `py_class!` is already implemented and does not mention
41 41 // `RustDirstateMap`, rightfully so.
42 42 // All attributes also have to have a separate refcount data attribute for
43 43 // leaks, with all methods that go along for reference sharing.
44 44 py_class!(pub class DirstateMap |py| {
45 45 data inner: PySharedRefCell<RustDirstateMap>;
46 46
47 47 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
48 48 let inner = RustDirstateMap::default();
49 49 Self::create_instance(
50 50 py,
51 51 PySharedRefCell::new(inner),
52 52 )
53 53 }
54 54
55 55 def clear(&self) -> PyResult<PyObject> {
56 56 self.inner_shared(py).borrow_mut()?.clear();
57 57 Ok(py.None())
58 58 }
59 59
60 60 def get(
61 61 &self,
62 62 key: PyObject,
63 63 default: Option<PyObject> = None
64 64 ) -> PyResult<Option<PyObject>> {
65 65 let key = key.extract::<PyBytes>(py)?;
66 66 match self.inner(py).borrow().get(HgPath::new(key.data(py))) {
67 67 Some(entry) => {
68 68 Ok(Some(make_dirstate_tuple(py, entry)?))
69 69 },
70 70 None => Ok(default)
71 71 }
72 72 }
73 73
74 74 def addfile(
75 75 &self,
76 76 f: PyObject,
77 77 oldstate: PyObject,
78 78 state: PyObject,
79 79 mode: PyObject,
80 80 size: PyObject,
81 81 mtime: PyObject
82 82 ) -> PyResult<PyObject> {
83 83 self.inner_shared(py).borrow_mut()?.add_file(
84 84 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
85 85 oldstate.extract::<PyBytes>(py)?.data(py)[0]
86 86 .try_into()
87 87 .map_err(|e: DirstateParseError| {
88 88 PyErr::new::<exc::ValueError, _>(py, e.to_string())
89 89 })?,
90 90 DirstateEntry {
91 91 state: state.extract::<PyBytes>(py)?.data(py)[0]
92 92 .try_into()
93 93 .map_err(|e: DirstateParseError| {
94 94 PyErr::new::<exc::ValueError, _>(py, e.to_string())
95 95 })?,
96 96 mode: mode.extract(py)?,
97 97 size: size.extract(py)?,
98 98 mtime: mtime.extract(py)?,
99 99 },
100 100 );
101 101 Ok(py.None())
102 102 }
103 103
104 104 def removefile(
105 105 &self,
106 106 f: PyObject,
107 107 oldstate: PyObject,
108 108 size: PyObject
109 109 ) -> PyResult<PyObject> {
110 110 self.inner_shared(py).borrow_mut()?
111 111 .remove_file(
112 112 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
113 113 oldstate.extract::<PyBytes>(py)?.data(py)[0]
114 114 .try_into()
115 115 .map_err(|e: DirstateParseError| {
116 116 PyErr::new::<exc::ValueError, _>(py, e.to_string())
117 117 })?,
118 118 size.extract(py)?,
119 119 )
120 120 .or_else(|_| {
121 121 Err(PyErr::new::<exc::OSError, _>(
122 122 py,
123 123 "Dirstate error".to_string(),
124 124 ))
125 125 })?;
126 126 Ok(py.None())
127 127 }
128 128
129 129 def dropfile(
130 130 &self,
131 131 f: PyObject,
132 132 oldstate: PyObject
133 133 ) -> PyResult<PyBool> {
134 134 self.inner_shared(py).borrow_mut()?
135 135 .drop_file(
136 136 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
137 137 oldstate.extract::<PyBytes>(py)?.data(py)[0]
138 138 .try_into()
139 139 .map_err(|e: DirstateParseError| {
140 140 PyErr::new::<exc::ValueError, _>(py, e.to_string())
141 141 })?,
142 142 )
143 143 .and_then(|b| Ok(b.to_py_object(py)))
144 144 .or_else(|_| {
145 145 Err(PyErr::new::<exc::OSError, _>(
146 146 py,
147 147 "Dirstate error".to_string(),
148 148 ))
149 149 })
150 150 }
151 151
152 152 def clearambiguoustimes(
153 153 &self,
154 154 files: PyObject,
155 155 now: PyObject
156 156 ) -> PyResult<PyObject> {
157 157 let files: PyResult<Vec<HgPathBuf>> = files
158 158 .iter(py)?
159 159 .map(|filename| {
160 160 Ok(HgPathBuf::from_bytes(
161 161 filename?.extract::<PyBytes>(py)?.data(py),
162 162 ))
163 163 })
164 164 .collect();
165 165 self.inner_shared(py).borrow_mut()?
166 166 .clear_ambiguous_times(files?, now.extract(py)?);
167 167 Ok(py.None())
168 168 }
169 169
170 170 // TODO share the reference
171 171 def nonnormalentries(&self) -> PyResult<PyObject> {
172 172 let (non_normal, other_parent) =
173 173 self.inner(py).borrow().non_normal_other_parent_entries();
174 174
175 175 let locals = PyDict::new(py);
176 176 locals.set_item(
177 177 py,
178 178 "non_normal",
179 179 non_normal
180 180 .iter()
181 181 .map(|v| PyBytes::new(py, v.as_ref()))
182 182 .collect::<Vec<PyBytes>>()
183 183 .to_py_object(py),
184 184 )?;
185 185 locals.set_item(
186 186 py,
187 187 "other_parent",
188 188 other_parent
189 189 .iter()
190 190 .map(|v| PyBytes::new(py, v.as_ref()))
191 191 .collect::<Vec<PyBytes>>()
192 192 .to_py_object(py),
193 193 )?;
194 194
195 195 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
196 196 }
197 197
198 198 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
199 199 let d = d.extract::<PyBytes>(py)?;
200 200 Ok(self.inner_shared(py).borrow_mut()?
201 201 .has_tracked_dir(HgPath::new(d.data(py)))
202 202 .to_py_object(py))
203 203 }
204 204
205 205 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
206 206 let d = d.extract::<PyBytes>(py)?;
207 207 Ok(self.inner_shared(py).borrow_mut()?
208 208 .has_dir(HgPath::new(d.data(py)))
209 209 .to_py_object(py))
210 210 }
211 211
212 212 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
213 213 self.inner_shared(py).borrow_mut()?
214 214 .parents(st.extract::<PyBytes>(py)?.data(py))
215 215 .and_then(|d| {
216 216 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
217 217 .to_py_object(py))
218 218 })
219 219 .or_else(|_| {
220 220 Err(PyErr::new::<exc::OSError, _>(
221 221 py,
222 222 "Dirstate error".to_string(),
223 223 ))
224 224 })
225 225 }
226 226
227 227 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
228 228 let p1 = extract_node_id(py, &p1)?;
229 229 let p2 = extract_node_id(py, &p2)?;
230 230
231 231 self.inner_shared(py).borrow_mut()?
232 232 .set_parents(&DirstateParents { p1, p2 });
233 233 Ok(py.None())
234 234 }
235 235
236 236 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
237 237 match self.inner_shared(py).borrow_mut()?
238 238 .read(st.extract::<PyBytes>(py)?.data(py))
239 239 {
240 240 Ok(Some(parents)) => Ok(Some(
241 241 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
242 242 .to_py_object(py)
243 243 .into_object(),
244 244 )),
245 245 Ok(None) => Ok(Some(py.None())),
246 246 Err(_) => Err(PyErr::new::<exc::OSError, _>(
247 247 py,
248 248 "Dirstate error".to_string(),
249 249 )),
250 250 }
251 251 }
252 252 def write(
253 253 &self,
254 254 p1: PyObject,
255 255 p2: PyObject,
256 256 now: PyObject
257 257 ) -> PyResult<PyBytes> {
258 258 let now = Duration::new(now.extract(py)?, 0);
259 259 let parents = DirstateParents {
260 260 p1: extract_node_id(py, &p1)?,
261 261 p2: extract_node_id(py, &p2)?,
262 262 };
263 263
264 264 match self.inner_shared(py).borrow_mut()?.pack(parents, now) {
265 265 Ok(packed) => Ok(PyBytes::new(py, &packed)),
266 266 Err(_) => Err(PyErr::new::<exc::OSError, _>(
267 267 py,
268 268 "Dirstate error".to_string(),
269 269 )),
270 270 }
271 271 }
272 272
273 273 def filefoldmapasdict(&self) -> PyResult<PyDict> {
274 274 let dict = PyDict::new(py);
275 275 for (key, value) in
276 276 self.inner_shared(py).borrow_mut()?.build_file_fold_map().iter()
277 277 {
278 278 dict.set_item(py, key.as_ref().to_vec(), value.as_ref().to_vec())?;
279 279 }
280 280 Ok(dict)
281 281 }
282 282
283 283 def __len__(&self) -> PyResult<usize> {
284 284 Ok(self.inner(py).borrow().len())
285 285 }
286 286
287 287 def __contains__(&self, key: PyObject) -> PyResult<bool> {
288 288 let key = key.extract::<PyBytes>(py)?;
289 289 Ok(self.inner(py).borrow().contains_key(HgPath::new(key.data(py))))
290 290 }
291 291
292 292 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
293 293 let key = key.extract::<PyBytes>(py)?;
294 294 let key = HgPath::new(key.data(py));
295 295 match self.inner(py).borrow().get(key) {
296 296 Some(entry) => {
297 297 Ok(make_dirstate_tuple(py, entry)?)
298 298 },
299 299 None => Err(PyErr::new::<exc::KeyError, _>(
300 300 py,
301 301 String::from_utf8_lossy(key.as_bytes()),
302 302 )),
303 303 }
304 304 }
305 305
306 306 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
307 let mut leak_handle =
308 unsafe { self.inner_shared(py).leak_immutable()? };
309 let leaked_ref = leak_handle.data.take().unwrap();
307 let leaked_ref = self.inner_shared(py).leak_immutable()?;
310 308 DirstateMapKeysIterator::from_inner(
311 309 py,
312 leak_handle,
313 leaked_ref.iter(),
310 unsafe { leaked_ref.map(py, |o| o.iter()) },
314 311 )
315 312 }
316 313
317 314 def items(&self) -> PyResult<DirstateMapItemsIterator> {
318 let mut leak_handle =
319 unsafe { self.inner_shared(py).leak_immutable()? };
320 let leaked_ref = leak_handle.data.take().unwrap();
315 let leaked_ref = self.inner_shared(py).leak_immutable()?;
321 316 DirstateMapItemsIterator::from_inner(
322 317 py,
323 leak_handle,
324 leaked_ref.iter(),
318 unsafe { leaked_ref.map(py, |o| o.iter()) },
325 319 )
326 320 }
327 321
328 322 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
329 let mut leak_handle =
330 unsafe { self.inner_shared(py).leak_immutable()? };
331 let leaked_ref = leak_handle.data.take().unwrap();
323 let leaked_ref = self.inner_shared(py).leak_immutable()?;
332 324 DirstateMapKeysIterator::from_inner(
333 325 py,
334 leak_handle,
335 leaked_ref.iter(),
326 unsafe { leaked_ref.map(py, |o| o.iter()) },
336 327 )
337 328 }
338 329
339 330 def getdirs(&self) -> PyResult<Dirs> {
340 331 // TODO don't copy, share the reference
341 332 self.inner_shared(py).borrow_mut()?.set_dirs();
342 333 Dirs::from_inner(
343 334 py,
344 335 DirsMultiset::from_dirstate(
345 336 &self.inner(py).borrow(),
346 337 Some(EntryState::Removed),
347 338 ),
348 339 )
349 340 }
350 341 def getalldirs(&self) -> PyResult<Dirs> {
351 342 // TODO don't copy, share the reference
352 343 self.inner_shared(py).borrow_mut()?.set_all_dirs();
353 344 Dirs::from_inner(
354 345 py,
355 346 DirsMultiset::from_dirstate(
356 347 &self.inner(py).borrow(),
357 348 None,
358 349 ),
359 350 )
360 351 }
361 352
362 353 // TODO all copymap* methods, see docstring above
363 354 def copymapcopy(&self) -> PyResult<PyDict> {
364 355 let dict = PyDict::new(py);
365 356 for (key, value) in self.inner(py).borrow().copy_map.iter() {
366 357 dict.set_item(
367 358 py,
368 359 PyBytes::new(py, key.as_ref()),
369 360 PyBytes::new(py, value.as_ref()),
370 361 )?;
371 362 }
372 363 Ok(dict)
373 364 }
374 365
375 366 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
376 367 let key = key.extract::<PyBytes>(py)?;
377 368 match self.inner(py).borrow().copy_map.get(HgPath::new(key.data(py))) {
378 369 Some(copy) => Ok(PyBytes::new(py, copy.as_ref())),
379 370 None => Err(PyErr::new::<exc::KeyError, _>(
380 371 py,
381 372 String::from_utf8_lossy(key.data(py)),
382 373 )),
383 374 }
384 375 }
385 376 def copymap(&self) -> PyResult<CopyMap> {
386 377 CopyMap::from_inner(py, self.clone_ref(py))
387 378 }
388 379
389 380 def copymaplen(&self) -> PyResult<usize> {
390 381 Ok(self.inner(py).borrow().copy_map.len())
391 382 }
392 383 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
393 384 let key = key.extract::<PyBytes>(py)?;
394 385 Ok(self
395 386 .inner(py)
396 387 .borrow()
397 388 .copy_map
398 389 .contains_key(HgPath::new(key.data(py))))
399 390 }
400 391 def copymapget(
401 392 &self,
402 393 key: PyObject,
403 394 default: Option<PyObject>
404 395 ) -> PyResult<Option<PyObject>> {
405 396 let key = key.extract::<PyBytes>(py)?;
406 397 match self
407 398 .inner(py)
408 399 .borrow()
409 400 .copy_map
410 401 .get(HgPath::new(key.data(py)))
411 402 {
412 403 Some(copy) => Ok(Some(
413 404 PyBytes::new(py, copy.as_ref()).into_object(),
414 405 )),
415 406 None => Ok(default),
416 407 }
417 408 }
418 409 def copymapsetitem(
419 410 &self,
420 411 key: PyObject,
421 412 value: PyObject
422 413 ) -> PyResult<PyObject> {
423 414 let key = key.extract::<PyBytes>(py)?;
424 415 let value = value.extract::<PyBytes>(py)?;
425 416 self.inner_shared(py).borrow_mut()?.copy_map.insert(
426 417 HgPathBuf::from_bytes(key.data(py)),
427 418 HgPathBuf::from_bytes(value.data(py)),
428 419 );
429 420 Ok(py.None())
430 421 }
431 422 def copymappop(
432 423 &self,
433 424 key: PyObject,
434 425 default: Option<PyObject>
435 426 ) -> PyResult<Option<PyObject>> {
436 427 let key = key.extract::<PyBytes>(py)?;
437 428 match self
438 429 .inner_shared(py)
439 430 .borrow_mut()?
440 431 .copy_map
441 432 .remove(HgPath::new(key.data(py)))
442 433 {
443 434 Some(_) => Ok(None),
444 435 None => Ok(default),
445 436 }
446 437 }
447 438
448 439 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
449 let mut leak_handle =
450 unsafe { self.inner_shared(py).leak_immutable()? };
451 let leaked_ref = leak_handle.data.take().unwrap();
440 let leaked_ref = self.inner_shared(py).leak_immutable()?;
452 441 CopyMapKeysIterator::from_inner(
453 442 py,
454 leak_handle,
455 leaked_ref.copy_map.iter(),
443 unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
456 444 )
457 445 }
458 446
459 447 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
460 let mut leak_handle =
461 unsafe { self.inner_shared(py).leak_immutable()? };
462 let leaked_ref = leak_handle.data.take().unwrap();
448 let leaked_ref = self.inner_shared(py).leak_immutable()?;
463 449 CopyMapItemsIterator::from_inner(
464 450 py,
465 leak_handle,
466 leaked_ref.copy_map.iter(),
451 unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
467 452 )
468 453 }
469 454
470 455 });
471 456
472 457 impl DirstateMap {
473 458 pub fn get_inner<'a>(
474 459 &'a self,
475 460 py: Python<'a>,
476 461 ) -> Ref<'a, RustDirstateMap> {
477 462 self.inner_shared(py).borrow()
478 463 }
479 464 fn translate_key(
480 465 py: Python,
481 466 res: (&HgPathBuf, &DirstateEntry),
482 467 ) -> PyResult<Option<PyBytes>> {
483 468 Ok(Some(PyBytes::new(py, res.0.as_ref())))
484 469 }
485 470 fn translate_key_value(
486 471 py: Python,
487 472 res: (&HgPathBuf, &DirstateEntry),
488 473 ) -> PyResult<Option<(PyBytes, PyObject)>> {
489 474 let (f, entry) = res;
490 475 Ok(Some((
491 476 PyBytes::new(py, f.as_ref()),
492 477 make_dirstate_tuple(py, entry)?,
493 478 )))
494 479 }
495 480 }
496 481
497 482 py_shared_ref!(DirstateMap, RustDirstateMap, inner, inner_shared);
498 483
499 484 py_shared_iterator!(
500 485 DirstateMapKeysIterator,
501 PyLeakedRef<&'static RustDirstateMap>,
502 StateMapIter<'static>,
486 PyLeakedRef<StateMapIter<'static>>,
503 487 DirstateMap::translate_key,
504 488 Option<PyBytes>
505 489 );
506 490
507 491 py_shared_iterator!(
508 492 DirstateMapItemsIterator,
509 PyLeakedRef<&'static RustDirstateMap>,
510 StateMapIter<'static>,
493 PyLeakedRef<StateMapIter<'static>>,
511 494 DirstateMap::translate_key_value,
512 495 Option<(PyBytes, PyObject)>
513 496 );
514 497
515 498 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
516 499 let bytes = obj.extract::<PyBytes>(py)?;
517 500 match bytes.data(py).try_into() {
518 501 Ok(s) => Ok(s),
519 502 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
520 503 }
521 504 }
@@ -1,469 +1,500 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::{PyClone, PyObject, PyResult, Python};
27 27 use std::cell::{Cell, Ref, RefCell, RefMut};
28 28
29 29 /// Manages the shared state between Python and Rust
30 30 #[derive(Debug, Default)]
31 31 pub struct PySharedState {
32 32 leak_count: Cell<usize>,
33 33 mutably_borrowed: Cell<bool>,
34 34 }
35 35
36 36 // &PySharedState can be Send because any access to inner cells is
37 37 // synchronized by the GIL.
38 38 unsafe impl Sync for PySharedState {}
39 39
40 40 impl PySharedState {
41 41 pub fn borrow_mut<'a, T>(
42 42 &'a self,
43 43 py: Python<'a>,
44 44 pyrefmut: RefMut<'a, T>,
45 45 ) -> PyResult<PyRefMut<'a, T>> {
46 46 if self.mutably_borrowed.get() {
47 47 return Err(AlreadyBorrowed::new(
48 48 py,
49 49 "Cannot borrow mutably while there exists another \
50 50 mutable reference in a Python object",
51 51 ));
52 52 }
53 53 match self.leak_count.get() {
54 54 0 => {
55 55 self.mutably_borrowed.replace(true);
56 56 Ok(PyRefMut::new(py, pyrefmut, self))
57 57 }
58 58 // TODO
59 59 // For now, this works differently than Python references
60 60 // in the case of iterators.
61 61 // Python does not complain when the data an iterator
62 62 // points to is modified if the iterator is never used
63 63 // afterwards.
64 64 // Here, we are stricter than this by refusing to give a
65 65 // mutable reference if it is already borrowed.
66 66 // While the additional safety might be argued for, it
67 67 // breaks valid programming patterns in Python and we need
68 68 // to fix this issue down the line.
69 69 _ => Err(AlreadyBorrowed::new(
70 70 py,
71 71 "Cannot borrow mutably while there are \
72 72 immutable references in Python objects",
73 73 )),
74 74 }
75 75 }
76 76
77 77 /// Return a reference to the wrapped data and its state with an
78 78 /// artificial static lifetime.
79 79 /// We need to be protected by the GIL for thread-safety.
80 80 ///
81 81 /// # Safety
82 82 ///
83 83 /// This is highly unsafe since the lifetime of the given data can be
84 84 /// extended. Do not call this function directly.
85 85 pub unsafe fn leak_immutable<T>(
86 86 &self,
87 87 py: Python,
88 88 data: &PySharedRefCell<T>,
89 89 ) -> PyResult<(&'static T, &'static PySharedState)> {
90 90 if self.mutably_borrowed.get() {
91 91 return Err(AlreadyBorrowed::new(
92 92 py,
93 93 "Cannot borrow immutably while there is a \
94 94 mutable reference in Python objects",
95 95 ));
96 96 }
97 97 // TODO: it's weird that self is data.py_shared_state. Maybe we
98 98 // can move stuff to PySharedRefCell?
99 99 let ptr = data.as_ptr();
100 100 let state_ptr: *const PySharedState = &data.py_shared_state;
101 101 self.leak_count.replace(self.leak_count.get() + 1);
102 102 Ok((&*ptr, &*state_ptr))
103 103 }
104 104
105 105 /// # Safety
106 106 ///
107 107 /// It's unsafe to update the reference count without knowing the
108 108 /// reference is deleted. Do not call this function directly.
109 109 pub unsafe fn decrease_leak_count(&self, _py: Python, mutable: bool) {
110 110 if mutable {
111 111 assert_eq!(self.leak_count.get(), 0);
112 112 assert!(self.mutably_borrowed.get());
113 113 self.mutably_borrowed.replace(false);
114 114 } else {
115 115 let count = self.leak_count.get();
116 116 assert!(count > 0);
117 117 self.leak_count.replace(count - 1);
118 118 }
119 119 }
120 120 }
121 121
122 122 /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`.
123 123 ///
124 124 /// Only immutable operation is allowed through this interface.
125 125 #[derive(Debug)]
126 126 pub struct PySharedRefCell<T> {
127 127 inner: RefCell<T>,
128 128 py_shared_state: PySharedState,
129 129 }
130 130
131 131 impl<T> PySharedRefCell<T> {
132 132 pub fn new(value: T) -> PySharedRefCell<T> {
133 133 Self {
134 134 inner: RefCell::new(value),
135 135 py_shared_state: PySharedState::default(),
136 136 }
137 137 }
138 138
139 139 pub fn borrow(&self) -> Ref<T> {
140 140 // py_shared_state isn't involved since
141 141 // - inner.borrow() would fail if self is mutably borrowed,
142 142 // - and inner.borrow_mut() would fail while self is borrowed.
143 143 self.inner.borrow()
144 144 }
145 145
146 146 pub fn as_ptr(&self) -> *mut T {
147 147 self.inner.as_ptr()
148 148 }
149 149
150 150 // TODO: maybe this should be named as try_borrow_mut(), and use
151 151 // inner.try_borrow_mut(). The current implementation panics if
152 152 // self.inner has been borrowed, but returns error if py_shared_state
153 153 // refuses to borrow.
154 154 pub fn borrow_mut<'a>(
155 155 &'a self,
156 156 py: Python<'a>,
157 157 ) -> PyResult<PyRefMut<'a, T>> {
158 158 self.py_shared_state.borrow_mut(py, self.inner.borrow_mut())
159 159 }
160 160 }
161 161
162 162 /// Sharable data member of type `T` borrowed from the `PyObject`.
163 163 pub struct PySharedRef<'a, T> {
164 164 py: Python<'a>,
165 165 owner: &'a PyObject,
166 166 data: &'a PySharedRefCell<T>,
167 167 }
168 168
169 169 impl<'a, T> PySharedRef<'a, T> {
170 170 /// # Safety
171 171 ///
172 172 /// The `data` must be owned by the `owner`. Otherwise, the leak count
173 173 /// would get wrong.
174 174 pub unsafe fn new(
175 175 py: Python<'a>,
176 176 owner: &'a PyObject,
177 177 data: &'a PySharedRefCell<T>,
178 178 ) -> Self {
179 179 Self { py, owner, data }
180 180 }
181 181
182 182 pub fn borrow(&self) -> Ref<'a, T> {
183 183 self.data.borrow()
184 184 }
185 185
186 186 pub fn borrow_mut(&self) -> PyResult<PyRefMut<'a, T>> {
187 187 self.data.borrow_mut(self.py)
188 188 }
189 189
190 /// Returns a leaked reference temporarily held by its management object.
191 ///
192 /// # Safety
193 ///
194 /// It's up to you to make sure that the management object lives
195 /// longer than the leaked reference. Otherwise, you'll get a
196 /// dangling reference.
197 pub unsafe fn leak_immutable(&self) -> PyResult<PyLeakedRef<&'static T>> {
198 let (static_ref, static_state_ref) = self
199 .data
200 .py_shared_state
201 .leak_immutable(self.py, self.data)?;
190 /// Returns a leaked reference.
191 pub fn leak_immutable(&self) -> PyResult<PyLeakedRef<&'static T>> {
192 let state = &self.data.py_shared_state;
193 unsafe {
194 let (static_ref, static_state_ref) =
195 state.leak_immutable(self.py, self.data)?;
202 196 Ok(PyLeakedRef::new(
203 197 self.py,
204 198 self.owner,
205 199 static_ref,
206 200 static_state_ref,
207 201 ))
208 202 }
209 203 }
204 }
210 205
211 206 /// Holds a mutable reference to data shared between Python and Rust.
212 207 pub struct PyRefMut<'a, T> {
213 208 inner: RefMut<'a, T>,
214 209 py_shared_state: &'a PySharedState,
215 210 }
216 211
217 212 impl<'a, T> PyRefMut<'a, T> {
218 213 // Must be constructed by PySharedState after checking its leak_count.
219 214 // Otherwise, drop() would incorrectly update the state.
220 215 fn new(
221 216 _py: Python<'a>,
222 217 inner: RefMut<'a, T>,
223 218 py_shared_state: &'a PySharedState,
224 219 ) -> Self {
225 220 Self {
226 221 inner,
227 222 py_shared_state,
228 223 }
229 224 }
230 225 }
231 226
232 227 impl<'a, T> std::ops::Deref for PyRefMut<'a, T> {
233 228 type Target = RefMut<'a, T>;
234 229
235 230 fn deref(&self) -> &Self::Target {
236 231 &self.inner
237 232 }
238 233 }
239 234 impl<'a, T> std::ops::DerefMut for PyRefMut<'a, T> {
240 235 fn deref_mut(&mut self) -> &mut Self::Target {
241 236 &mut self.inner
242 237 }
243 238 }
244 239
245 240 impl<'a, T> Drop for PyRefMut<'a, T> {
246 241 fn drop(&mut self) {
247 242 let gil = Python::acquire_gil();
248 243 let py = gil.python();
249 244 unsafe {
250 245 self.py_shared_state.decrease_leak_count(py, true);
251 246 }
252 247 }
253 248 }
254 249
255 250 /// Allows a `py_class!` generated struct to share references to one of its
256 251 /// data members with Python.
257 252 ///
258 253 /// # Warning
259 254 ///
260 255 /// TODO allow Python container types: for now, integration with the garbage
261 256 /// collector does not extend to Rust structs holding references to Python
262 257 /// objects. Should the need surface, `__traverse__` and `__clear__` will
263 258 /// need to be written as per the `rust-cpython` docs on GC integration.
264 259 ///
265 260 /// # Parameters
266 261 ///
267 262 /// * `$name` is the same identifier used in for `py_class!` macro call.
268 263 /// * `$inner_struct` is the identifier of the underlying Rust struct
269 264 /// * `$data_member` is the identifier of the data member of `$inner_struct`
270 265 /// that will be shared.
271 266 /// * `$shared_accessor` is the function name to be generated, which allows
272 267 /// safe access to the data member.
273 268 ///
274 269 /// # Safety
275 270 ///
276 271 /// `$data_member` must persist while the `$name` object is alive. In other
277 272 /// words, it must be an accessor to a data field of the Python object.
278 273 ///
279 274 /// # Example
280 275 ///
281 276 /// ```
282 277 /// struct MyStruct {
283 278 /// inner: Vec<u32>;
284 279 /// }
285 280 ///
286 281 /// py_class!(pub class MyType |py| {
287 282 /// data inner: PySharedRefCell<MyStruct>;
288 283 /// });
289 284 ///
290 285 /// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
291 286 /// ```
292 287 macro_rules! py_shared_ref {
293 288 (
294 289 $name: ident,
295 290 $inner_struct: ident,
296 291 $data_member: ident,
297 292 $shared_accessor: ident
298 293 ) => {
299 294 impl $name {
300 295 /// Returns a safe reference to the shared `$data_member`.
301 296 ///
302 297 /// This function guarantees that `PySharedRef` is created with
303 298 /// the valid `self` and `self.$data_member(py)` pair.
304 299 fn $shared_accessor<'a>(
305 300 &'a self,
306 301 py: Python<'a>,
307 302 ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
308 303 use cpython::PythonObject;
309 304 use $crate::ref_sharing::PySharedRef;
310 305 let owner = self.as_object();
311 306 let data = self.$data_member(py);
312 307 unsafe { PySharedRef::new(py, owner, data) }
313 308 }
314 309 }
315 310 };
316 311 }
317 312
318 313 /// Manage immutable references to `PyObject` leaked into Python iterators.
319 ///
320 /// In truth, this does not represent leaked references themselves;
321 /// it is instead useful alongside them to manage them.
322 314 pub struct PyLeakedRef<T> {
323 _inner: PyObject,
324 pub data: Option<T>, // TODO: remove pub
315 inner: PyObject,
316 data: Option<T>,
325 317 py_shared_state: &'static PySharedState,
326 318 }
327 319
320 // DO NOT implement Deref for PyLeakedRef<T>! Dereferencing PyLeakedRef
321 // without taking Python GIL wouldn't be safe.
322
328 323 impl<T> PyLeakedRef<T> {
329 324 /// # Safety
330 325 ///
331 326 /// The `py_shared_state` must be owned by the `inner` Python object.
332 327 // Marked as unsafe so client code wouldn't construct PyLeakedRef
333 328 // struct by mistake. Its drop() is unsafe.
334 329 pub unsafe fn new(
335 330 py: Python,
336 331 inner: &PyObject,
337 332 data: T,
338 333 py_shared_state: &'static PySharedState,
339 334 ) -> Self {
340 335 Self {
341 _inner: inner.clone_ref(py),
336 inner: inner.clone_ref(py),
342 337 data: Some(data),
343 338 py_shared_state,
344 339 }
345 340 }
341
342 /// Returns an immutable reference to the inner value.
343 pub fn get_ref<'a>(&'a self, _py: Python<'a>) -> &'a T {
344 self.data.as_ref().unwrap()
345 }
346
347 /// Returns a mutable reference to the inner value.
348 ///
349 /// Typically `T` is an iterator. If `T` is an immutable reference,
350 /// `get_mut()` is useless since the inner value can't be mutated.
351 pub fn get_mut<'a>(&'a mut self, _py: Python<'a>) -> &'a mut T {
352 self.data.as_mut().unwrap()
353 }
354
355 /// Converts the inner value by the given function.
356 ///
357 /// Typically `T` is a static reference to a container, and `U` is an
358 /// iterator of that container.
359 ///
360 /// # Safety
361 ///
362 /// The lifetime of the object passed in to the function `f` is cheated.
363 /// It's typically a static reference, but is valid only while the
364 /// corresponding `PyLeakedRef` is alive. Do not copy it out of the
365 /// function call.
366 pub unsafe fn map<U>(
367 mut self,
368 py: Python,
369 f: impl FnOnce(T) -> U,
370 ) -> PyLeakedRef<U> {
371 // f() could make the self.data outlive. That's why map() is unsafe.
372 // In order to make this function safe, maybe we'll need a way to
373 // temporarily restrict the lifetime of self.data and translate the
374 // returned object back to Something<'static>.
375 let new_data = f(self.data.take().unwrap());
376 PyLeakedRef {
377 inner: self.inner.clone_ref(py),
378 data: Some(new_data),
379 py_shared_state: self.py_shared_state,
380 }
381 }
346 382 }
347 383
348 384 impl<T> Drop for PyLeakedRef<T> {
349 385 fn drop(&mut self) {
350 386 // py_shared_state should be alive since we do have
351 387 // a Python reference to the owner object. Taking GIL makes
352 388 // sure that the state is only accessed by this thread.
353 389 let gil = Python::acquire_gil();
354 390 let py = gil.python();
391 if self.data.is_none() {
392 return; // moved to another PyLeakedRef
393 }
355 394 unsafe {
356 395 self.py_shared_state.decrease_leak_count(py, false);
357 396 }
358 397 }
359 398 }
360 399
361 400 /// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
362 401 ///
363 402 /// TODO: this is a bit awkward to use, and a better (more complicated)
364 403 /// procedural macro would simplify the interface a lot.
365 404 ///
366 405 /// # Parameters
367 406 ///
368 407 /// * `$name` is the identifier to give to the resulting Rust struct.
369 408 /// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call.
370 409 /// * `$iterator_type` is the type of the Rust iterator.
371 410 /// * `$success_func` is a function for processing the Rust `(key, value)`
372 411 /// tuple on iteration success, turning it into something Python understands.
373 412 /// * `$success_func` is the return type of `$success_func`
374 413 ///
375 414 /// # Example
376 415 ///
377 416 /// ```
378 417 /// struct MyStruct {
379 418 /// inner: HashMap<Vec<u8>, Vec<u8>>;
380 419 /// }
381 420 ///
382 421 /// py_class!(pub class MyType |py| {
383 422 /// data inner: PySharedRefCell<MyStruct>;
384 423 ///
385 424 /// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
386 /// let mut leak_handle =
387 /// unsafe { self.inner_shared(py).leak_immutable()? };
388 /// let leaked_ref = leak_handle.data.take().unwrap();
425 /// let leaked_ref = self.inner_shared(py).leak_immutable()?;
389 426 /// MyTypeItemsIterator::from_inner(
390 427 /// py,
391 /// leak_handle,
392 /// leaked_ref.iter(),
428 /// unsafe { leaked_ref.map(py, |o| o.iter()) },
393 429 /// )
394 430 /// }
395 431 /// });
396 432 ///
397 433 /// impl MyType {
398 434 /// fn translate_key_value(
399 435 /// py: Python,
400 436 /// res: (&Vec<u8>, &Vec<u8>),
401 437 /// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
402 438 /// let (f, entry) = res;
403 439 /// Ok(Some((
404 440 /// PyBytes::new(py, f),
405 441 /// PyBytes::new(py, entry),
406 442 /// )))
407 443 /// }
408 444 /// }
409 445 ///
410 446 /// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef);
411 447 ///
412 448 /// py_shared_iterator!(
413 449 /// MyTypeItemsIterator,
414 /// PyLeakedRef<&'static MyStruct>,
415 /// HashMap<'static, Vec<u8>, Vec<u8>>,
450 /// PyLeakedRef<HashMap<'static, Vec<u8>, Vec<u8>>>,
416 451 /// MyType::translate_key_value,
417 452 /// Option<(PyBytes, PyBytes)>
418 453 /// );
419 454 /// ```
420 455 macro_rules! py_shared_iterator {
421 456 (
422 457 $name: ident,
423 458 $leaked: ty,
424 $iterator_type: ty,
425 459 $success_func: expr,
426 460 $success_type: ty
427 461 ) => {
428 462 py_class!(pub class $name |py| {
429 463 data inner: RefCell<Option<$leaked>>;
430 data it: RefCell<$iterator_type>;
431 464
432 465 def __next__(&self) -> PyResult<$success_type> {
433 466 let mut inner_opt = self.inner(py).borrow_mut();
434 if inner_opt.is_some() {
435 match self.it(py).borrow_mut().next() {
467 if let Some(leaked) = inner_opt.as_mut() {
468 match leaked.get_mut(py).next() {
436 469 None => {
437 470 // replace Some(inner) by None, drop $leaked
438 471 inner_opt.take();
439 472 Ok(None)
440 473 }
441 474 Some(res) => {
442 475 $success_func(py, res)
443 476 }
444 477 }
445 478 } else {
446 479 Ok(None)
447 480 }
448 481 }
449 482
450 483 def __iter__(&self) -> PyResult<Self> {
451 484 Ok(self.clone_ref(py))
452 485 }
453 486 });
454 487
455 488 impl $name {
456 489 pub fn from_inner(
457 490 py: Python,
458 491 leaked: $leaked,
459 it: $iterator_type
460 492 ) -> PyResult<Self> {
461 493 Self::create_instance(
462 494 py,
463 495 RefCell::new(Some(leaked)),
464 RefCell::new(it)
465 496 )
466 497 }
467 498 }
468 499 };
469 500 }
General Comments 0
You need to be logged in to leave comments. Login now