##// END OF EJS Templates
rust: implement vcsgraph::RankedGraph for Index...
pacien -
r49708:4346be45 default
parent child Browse files
Show More
@@ -1,205 +1,219 b''
1 1 // cindex.rs
2 2 //
3 3 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings to use the Index defined by the parsers C extension
9 9 //!
10 10 //! Ideally, we should use an Index entirely implemented in Rust,
11 11 //! but this will take some time to get there.
12 12
13 13 use cpython::{
14 14 exc::ImportError, exc::TypeError, ObjectProtocol, PyClone, PyErr,
15 15 PyObject, PyResult, PyTuple, Python, PythonObject,
16 16 };
17 17 use hg::revlog::{Node, RevlogIndex};
18 18 use hg::{Graph, GraphError, Revision, WORKING_DIRECTORY_REVISION};
19 19 use libc::{c_int, ssize_t};
20 20
21 21 const REVLOG_CABI_VERSION: c_int = 3;
22 22
23 23 #[repr(C)]
24 24 pub struct Revlog_CAPI {
25 25 abi_version: c_int,
26 26 index_length:
27 27 unsafe extern "C" fn(index: *mut revlog_capi::RawPyObject) -> ssize_t,
28 28 index_node: unsafe extern "C" fn(
29 29 index: *mut revlog_capi::RawPyObject,
30 30 rev: ssize_t,
31 31 ) -> *const Node,
32 32 fast_rank: unsafe extern "C" fn(
33 33 index: *mut revlog_capi::RawPyObject,
34 34 rev: ssize_t,
35 35 ) -> ssize_t,
36 36 index_parents: unsafe extern "C" fn(
37 37 index: *mut revlog_capi::RawPyObject,
38 38 rev: c_int,
39 39 ps: *mut [c_int; 2],
40 40 ) -> c_int,
41 41 }
42 42
43 43 py_capsule!(
44 44 from mercurial.cext.parsers import revlog_CAPI
45 45 as revlog_capi for Revlog_CAPI);
46 46
47 47 /// A `Graph` backed up by objects and functions from revlog.c
48 48 ///
49 49 /// This implementation of the `Graph` trait, relies on (pointers to)
50 50 /// - the C index object (`index` member)
51 51 /// - the `index_get_parents()` function (`parents` member)
52 52 ///
53 53 /// # Safety
54 54 ///
55 55 /// The C index itself is mutable, and this Rust exposition is **not
56 56 /// protected by the GIL**, meaning that this construct isn't safe with respect
57 57 /// to Python threads.
58 58 ///
59 59 /// All callers of this `Index` must acquire the GIL and must not release it
60 60 /// while working.
61 61 ///
62 62 /// # TODO find a solution to make it GIL safe again.
63 63 ///
64 64 /// This is non trivial, and can wait until we have a clearer picture with
65 65 /// more Rust Mercurial constructs.
66 66 ///
67 67 /// One possibility would be to a `GILProtectedIndex` wrapper enclosing
68 68 /// a `Python<'p>` marker and have it be the one implementing the
69 69 /// `Graph` trait, but this would mean the `Graph` implementor would become
70 70 /// likely to change between subsequent method invocations of the `hg-core`
71 71 /// objects (a serious change of the `hg-core` API):
72 72 /// either exposing ways to mutate the `Graph`, or making it a non persistent
73 73 /// parameter in the relevant methods that need one.
74 74 ///
75 75 /// Another possibility would be to introduce an abstract lock handle into
76 76 /// the core API, that would be tied to `GILGuard` / `Python<'p>`
77 77 /// in the case of the `cpython` crate bindings yet could leave room for other
78 78 /// mechanisms in other contexts.
79 79 pub struct Index {
80 80 index: PyObject,
81 81 capi: &'static Revlog_CAPI,
82 82 }
83 83
84 84 impl Index {
85 85 pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
86 86 let capi = unsafe { revlog_capi::retrieve(py)? };
87 87 if capi.abi_version != REVLOG_CABI_VERSION {
88 88 return Err(PyErr::new::<ImportError, _>(
89 89 py,
90 90 format!(
91 91 "ABI version mismatch: the C ABI revlog version {} \
92 92 does not match the {} expected by Rust hg-cpython",
93 93 capi.abi_version, REVLOG_CABI_VERSION
94 94 ),
95 95 ));
96 96 }
97 97 let compat: u64 = index.getattr(py, "rust_ext_compat")?.extract(py)?;
98 98 if compat == 0 {
99 99 return Err(PyErr::new::<TypeError, _>(
100 100 py,
101 101 "index object not compatible with Rust",
102 102 ));
103 103 }
104 104 Ok(Index { index, capi })
105 105 }
106 106
107 107 /// return a reference to the CPython Index object in this Struct
108 108 pub fn inner(&self) -> &PyObject {
109 109 &self.index
110 110 }
111 111
112 112 pub fn append(&mut self, py: Python, tup: PyTuple) -> PyResult<PyObject> {
113 113 self.index.call_method(
114 114 py,
115 115 "append",
116 116 PyTuple::new(py, &[tup.into_object()]),
117 117 None,
118 118 )
119 119 }
120 120 }
121 121
122 122 impl Clone for Index {
123 123 fn clone(&self) -> Self {
124 124 let guard = Python::acquire_gil();
125 125 Index {
126 126 index: self.index.clone_ref(guard.python()),
127 127 capi: self.capi,
128 128 }
129 129 }
130 130 }
131 131
132 132 impl PyClone for Index {
133 133 fn clone_ref(&self, py: Python) -> Self {
134 134 Index {
135 135 index: self.index.clone_ref(py),
136 136 capi: self.capi,
137 137 }
138 138 }
139 139 }
140 140
141 141 impl Graph for Index {
142 142 /// wrap a call to the C extern parents function
143 143 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
144 144 if rev == WORKING_DIRECTORY_REVISION {
145 145 return Err(GraphError::WorkingDirectoryUnsupported);
146 146 }
147 147 let mut res: [c_int; 2] = [0; 2];
148 148 let code = unsafe {
149 149 (self.capi.index_parents)(
150 150 self.index.as_ptr(),
151 151 rev as c_int,
152 152 &mut res as *mut [c_int; 2],
153 153 )
154 154 };
155 155 match code {
156 156 0 => Ok(res),
157 157 _ => Err(GraphError::ParentOutOfRange(rev)),
158 158 }
159 159 }
160 160 }
161 161
162 162 impl vcsgraph::graph::Graph for Index {
163 163 fn parents(
164 164 &self,
165 165 rev: Revision,
166 166 ) -> Result<vcsgraph::graph::Parents, vcsgraph::graph::GraphReadError>
167 167 {
168 168 match Graph::parents(self, rev) {
169 169 Ok(parents) => Ok(vcsgraph::graph::Parents(parents)),
170 170 Err(GraphError::ParentOutOfRange(rev)) => {
171 171 Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev))
172 172 }
173 173 Err(GraphError::WorkingDirectoryUnsupported) => Err(
174 174 vcsgraph::graph::GraphReadError::WorkingDirectoryUnsupported,
175 175 ),
176 176 }
177 177 }
178 178 }
179 179
180 impl vcsgraph::graph::RankedGraph for Index {
181 fn rank(
182 &self,
183 rev: Revision,
184 ) -> Result<vcsgraph::graph::Rank, vcsgraph::graph::GraphReadError> {
185 match unsafe {
186 (self.capi.fast_rank)(self.index.as_ptr(), rev as ssize_t)
187 } {
188 -1 => Err(vcsgraph::graph::GraphReadError::InconsistentGraphData),
189 rank => Ok(rank as usize),
190 }
191 }
192 }
193
180 194 impl RevlogIndex for Index {
181 195 /// Note C return type is Py_ssize_t (hence signed), but we shall
182 196 /// force it to unsigned, because it's a length
183 197 fn len(&self) -> usize {
184 198 unsafe { (self.capi.index_length)(self.index.as_ptr()) as usize }
185 199 }
186 200
187 201 fn node(&self, rev: Revision) -> Option<&Node> {
188 202 let raw = unsafe {
189 203 (self.capi.index_node)(self.index.as_ptr(), rev as ssize_t)
190 204 };
191 205 if raw.is_null() {
192 206 None
193 207 } else {
194 208 // TODO it would be much better for the C layer to give us
195 209 // a length, since the hash length will change in the near
196 210 // future, but that's probably out of scope for the nodemap
197 211 // patch series.
198 212 //
199 213 // The root of that unsafety relies in the signature of
200 214 // `capi.index_node()` itself: returning a `Node` pointer
201 215 // whereas it's a `char *` in the C counterpart.
202 216 Some(unsafe { &*raw })
203 217 }
204 218 }
205 219 }
General Comments 0
You need to be logged in to leave comments. Login now