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