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