Mercurial > public > mercurial-scm > hg-stable
comparison rust/hg-cpython/src/dirstate/dirstate_map.rs @ 44278:2a24ead003f0
rust-cpython: add panicking version of borrow_mut() and use it
The original borrow_mut() is renamed to try_borrow_mut().
Since leak_immutable() no longer incref the borrow count, the caller should
know if the underlying value is borrowed or not. No Python world is involved.
That's why we can simply use the panicking borrow_mut().
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 12 Oct 2019 23:34:05 +0900 |
parents | bc7d8f45c3b6 |
children | 281642cd1d04 |
comparison
equal
deleted
inserted
replaced
44277:a7f8160cc4e4 | 44278:2a24ead003f0 |
---|---|
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, |
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()) |
106 &self, | 106 &self, |
107 f: PyObject, | 107 f: PyObject, |
108 oldstate: PyObject, | 108 oldstate: PyObject, |
109 size: PyObject | 109 size: PyObject |
110 ) -> PyResult<PyObject> { | 110 ) -> PyResult<PyObject> { |
111 self.inner_shared(py).borrow_mut()? | 111 self.inner_shared(py).borrow_mut() |
112 .remove_file( | 112 .remove_file( |
113 HgPath::new(f.extract::<PyBytes>(py)?.data(py)), | 113 HgPath::new(f.extract::<PyBytes>(py)?.data(py)), |
114 oldstate.extract::<PyBytes>(py)?.data(py)[0] | 114 oldstate.extract::<PyBytes>(py)?.data(py)[0] |
115 .try_into() | 115 .try_into() |
116 .map_err(|e: DirstateParseError| { | 116 .map_err(|e: DirstateParseError| { |
130 def dropfile( | 130 def dropfile( |
131 &self, | 131 &self, |
132 f: PyObject, | 132 f: PyObject, |
133 oldstate: PyObject | 133 oldstate: PyObject |
134 ) -> PyResult<PyBool> { | 134 ) -> PyResult<PyBool> { |
135 self.inner_shared(py).borrow_mut()? | 135 self.inner_shared(py).borrow_mut() |
136 .drop_file( | 136 .drop_file( |
137 HgPath::new(f.extract::<PyBytes>(py)?.data(py)), | 137 HgPath::new(f.extract::<PyBytes>(py)?.data(py)), |
138 oldstate.extract::<PyBytes>(py)?.data(py)[0] | 138 oldstate.extract::<PyBytes>(py)?.data(py)[0] |
139 .try_into() | 139 .try_into() |
140 .map_err(|e: DirstateParseError| { | 140 .map_err(|e: DirstateParseError| { |
161 Ok(HgPathBuf::from_bytes( | 161 Ok(HgPathBuf::from_bytes( |
162 filename?.extract::<PyBytes>(py)?.data(py), | 162 filename?.extract::<PyBytes>(py)?.data(py), |
163 )) | 163 )) |
164 }) | 164 }) |
165 .collect(); | 165 .collect(); |
166 self.inner_shared(py).borrow_mut()? | 166 self.inner_shared(py).borrow_mut() |
167 .clear_ambiguous_times(files?, now.extract(py)?); | 167 .clear_ambiguous_times(files?, now.extract(py)?); |
168 Ok(py.None()) | 168 Ok(py.None()) |
169 } | 169 } |
170 | 170 |
171 // TODO share the reference | 171 // TODO share the reference |
196 py.eval("set(non_normal), set(other_parent)", None, Some(&locals)) | 196 py.eval("set(non_normal), set(other_parent)", None, Some(&locals)) |
197 } | 197 } |
198 | 198 |
199 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> { | 199 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> { |
200 let d = d.extract::<PyBytes>(py)?; | 200 let d = d.extract::<PyBytes>(py)?; |
201 Ok(self.inner_shared(py).borrow_mut()? | 201 Ok(self.inner_shared(py).borrow_mut() |
202 .has_tracked_dir(HgPath::new(d.data(py))) | 202 .has_tracked_dir(HgPath::new(d.data(py))) |
203 .map_err(|e| { | 203 .map_err(|e| { |
204 PyErr::new::<exc::ValueError, _>(py, e.to_string()) | 204 PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
205 })? | 205 })? |
206 .to_py_object(py)) | 206 .to_py_object(py)) |
207 } | 207 } |
208 | 208 |
209 def hasdir(&self, d: PyObject) -> PyResult<PyBool> { | 209 def hasdir(&self, d: PyObject) -> PyResult<PyBool> { |
210 let d = d.extract::<PyBytes>(py)?; | 210 let d = d.extract::<PyBytes>(py)?; |
211 Ok(self.inner_shared(py).borrow_mut()? | 211 Ok(self.inner_shared(py).borrow_mut() |
212 .has_dir(HgPath::new(d.data(py))) | 212 .has_dir(HgPath::new(d.data(py))) |
213 .map_err(|e| { | 213 .map_err(|e| { |
214 PyErr::new::<exc::ValueError, _>(py, e.to_string()) | 214 PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
215 })? | 215 })? |
216 .to_py_object(py)) | 216 .to_py_object(py)) |
217 } | 217 } |
218 | 218 |
219 def parents(&self, st: PyObject) -> PyResult<PyTuple> { | 219 def parents(&self, st: PyObject) -> PyResult<PyTuple> { |
220 self.inner_shared(py).borrow_mut()? | 220 self.inner_shared(py).borrow_mut() |
221 .parents(st.extract::<PyBytes>(py)?.data(py)) | 221 .parents(st.extract::<PyBytes>(py)?.data(py)) |
222 .and_then(|d| { | 222 .and_then(|d| { |
223 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2)) | 223 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2)) |
224 .to_py_object(py)) | 224 .to_py_object(py)) |
225 }) | 225 }) |
233 | 233 |
234 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> { | 234 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> { |
235 let p1 = extract_node_id(py, &p1)?; | 235 let p1 = extract_node_id(py, &p1)?; |
236 let p2 = extract_node_id(py, &p2)?; | 236 let p2 = extract_node_id(py, &p2)?; |
237 | 237 |
238 self.inner_shared(py).borrow_mut()? | 238 self.inner_shared(py).borrow_mut() |
239 .set_parents(&DirstateParents { p1, p2 }); | 239 .set_parents(&DirstateParents { p1, p2 }); |
240 Ok(py.None()) | 240 Ok(py.None()) |
241 } | 241 } |
242 | 242 |
243 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> { | 243 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> { |
244 match self.inner_shared(py).borrow_mut()? | 244 match self.inner_shared(py).borrow_mut() |
245 .read(st.extract::<PyBytes>(py)?.data(py)) | 245 .read(st.extract::<PyBytes>(py)?.data(py)) |
246 { | 246 { |
247 Ok(Some(parents)) => Ok(Some( | 247 Ok(Some(parents)) => Ok(Some( |
248 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2)) | 248 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2)) |
249 .to_py_object(py) | 249 .to_py_object(py) |
266 let parents = DirstateParents { | 266 let parents = DirstateParents { |
267 p1: extract_node_id(py, &p1)?, | 267 p1: extract_node_id(py, &p1)?, |
268 p2: extract_node_id(py, &p2)?, | 268 p2: extract_node_id(py, &p2)?, |
269 }; | 269 }; |
270 | 270 |
271 match self.inner_shared(py).borrow_mut()?.pack(parents, now) { | 271 match self.inner_shared(py).borrow_mut().pack(parents, now) { |
272 Ok(packed) => Ok(PyBytes::new(py, &packed)), | 272 Ok(packed) => Ok(PyBytes::new(py, &packed)), |
273 Err(_) => Err(PyErr::new::<exc::OSError, _>( | 273 Err(_) => Err(PyErr::new::<exc::OSError, _>( |
274 py, | 274 py, |
275 "Dirstate error".to_string(), | 275 "Dirstate error".to_string(), |
276 )), | 276 )), |
278 } | 278 } |
279 | 279 |
280 def filefoldmapasdict(&self) -> PyResult<PyDict> { | 280 def filefoldmapasdict(&self) -> PyResult<PyDict> { |
281 let dict = PyDict::new(py); | 281 let dict = PyDict::new(py); |
282 for (key, value) in | 282 for (key, value) in |
283 self.inner_shared(py).borrow_mut()?.build_file_fold_map().iter() | 283 self.inner_shared(py).borrow_mut().build_file_fold_map().iter() |
284 { | 284 { |
285 dict.set_item(py, key.as_ref().to_vec(), value.as_ref().to_vec())?; | 285 dict.set_item(py, key.as_ref().to_vec(), value.as_ref().to_vec())?; |
286 } | 286 } |
287 Ok(dict) | 287 Ok(dict) |
288 } | 288 } |
334 ) | 334 ) |
335 } | 335 } |
336 | 336 |
337 def getdirs(&self) -> PyResult<Dirs> { | 337 def getdirs(&self) -> PyResult<Dirs> { |
338 // TODO don't copy, share the reference | 338 // TODO don't copy, share the reference |
339 self.inner_shared(py).borrow_mut()?.set_dirs() | 339 self.inner_shared(py).borrow_mut().set_dirs() |
340 .map_err(|e| { | 340 .map_err(|e| { |
341 PyErr::new::<exc::ValueError, _>(py, e.to_string()) | 341 PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
342 })?; | 342 })?; |
343 Dirs::from_inner( | 343 Dirs::from_inner( |
344 py, | 344 py, |
351 })?, | 351 })?, |
352 ) | 352 ) |
353 } | 353 } |
354 def getalldirs(&self) -> PyResult<Dirs> { | 354 def getalldirs(&self) -> PyResult<Dirs> { |
355 // TODO don't copy, share the reference | 355 // TODO don't copy, share the reference |
356 self.inner_shared(py).borrow_mut()?.set_all_dirs() | 356 self.inner_shared(py).borrow_mut().set_all_dirs() |
357 .map_err(|e| { | 357 .map_err(|e| { |
358 PyErr::new::<exc::ValueError, _>(py, e.to_string()) | 358 PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
359 })?; | 359 })?; |
360 Dirs::from_inner( | 360 Dirs::from_inner( |
361 py, | 361 py, |
429 key: PyObject, | 429 key: PyObject, |
430 value: PyObject | 430 value: PyObject |
431 ) -> PyResult<PyObject> { | 431 ) -> PyResult<PyObject> { |
432 let key = key.extract::<PyBytes>(py)?; | 432 let key = key.extract::<PyBytes>(py)?; |
433 let value = value.extract::<PyBytes>(py)?; | 433 let value = value.extract::<PyBytes>(py)?; |
434 self.inner_shared(py).borrow_mut()?.copy_map.insert( | 434 self.inner_shared(py).borrow_mut().copy_map.insert( |
435 HgPathBuf::from_bytes(key.data(py)), | 435 HgPathBuf::from_bytes(key.data(py)), |
436 HgPathBuf::from_bytes(value.data(py)), | 436 HgPathBuf::from_bytes(value.data(py)), |
437 ); | 437 ); |
438 Ok(py.None()) | 438 Ok(py.None()) |
439 } | 439 } |
443 default: Option<PyObject> | 443 default: Option<PyObject> |
444 ) -> PyResult<Option<PyObject>> { | 444 ) -> PyResult<Option<PyObject>> { |
445 let key = key.extract::<PyBytes>(py)?; | 445 let key = key.extract::<PyBytes>(py)?; |
446 match self | 446 match self |
447 .inner_shared(py) | 447 .inner_shared(py) |
448 .borrow_mut()? | 448 .borrow_mut() |
449 .copy_map | 449 .copy_map |
450 .remove(HgPath::new(key.data(py))) | 450 .remove(HgPath::new(key.data(py))) |
451 { | 451 { |
452 Some(_) => Ok(None), | 452 Some(_) => Ok(None), |
453 None => Ok(default), | 453 None => Ok(default), |