comparison rust/hg-cpython/src/dirstate/dirstate_map.rs @ 43422:b9f791090211

rust-cpython: rename PyLeakedRef to PyLeaked This series will make PyLeaked* behave more like a Python iterator, which means mutation of the owner object will be allowed and the leaked reference (i.e. the iterator) will be invalidated instead. I'll add PyLeakedRef/PyLeakedRefMut structs which will represent a "borrowed" state, and prevent the underlying value from being mutably borrowed while the leaked reference is in use: let shared = self.inner_shared(py); let leaked = shared.leak_immutable(); { let leaked_ref: PyLeakedRef<_> = leaked.borrow(py); shared.borrow_mut(); // panics since the underlying value is borrowed } shared.borrow_mut(); // allowed The relation between PyLeaked* structs is quite similar to RefCell/Ref/RefMut, but the implementation can't be reused because the borrowing state will have to be shared across objects having no lifetime relation. PyLeaked isn't named as PyLeakedCell since it isn't actually a cell in that leaked.borrow_mut() will require &mut self.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 12 Oct 2019 19:10:51 +0900
parents f8c114f20d2d
children 8418b77132c1
comparison
equal deleted inserted replaced
43305:d782cce137fd 43422:b9f791090211
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::{PyLeakedRef, 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,
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 PyLeakedRef<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 PyLeakedRef<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]> {