annotate rust/hg-cpython/src/dirstate/dirs_multiset.rs @ 42849:8db8fa1de2ef

rust-cpython: introduce restricted variant of RefCell This should catch invalid borrow_mut() calls. Still the ref-sharing interface is unsafe.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 01 Sep 2019 17:37:30 +0900
parents 2e1f74cc3350
children 8f549c46bc64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42746
b3518b0baa47 rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
1 // dirs_multiset.rs
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
2 //
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
4 //
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7
42746
b3518b0baa47 rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
8 //! Bindings for the `hg::dirstate::dirs_multiset` file provided by the
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9 //! `hg-core` package.
42746
b3518b0baa47 rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
10
b3518b0baa47 rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
11 use std::cell::RefCell;
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
12 use std::convert::TryInto;
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
13
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
14 use cpython::{
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
15 exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
16 Python,
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
17 };
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
18
42849
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
19 use crate::dirstate::extract_dirstate;
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
20 use crate::ref_sharing::{PySharedRefCell, PySharedState};
42802
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42752
diff changeset
21 use hg::{DirsMultiset, DirstateMapError, DirstateParseError, EntryState};
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
23 py_class!(pub class Dirs |py| {
42849
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
24 data inner: PySharedRefCell<DirsMultiset>;
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
25 data py_shared_state: PySharedState;
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
26
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
27 // `map` is either a `dict` or a flat iterator (usually a `set`, sometimes
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
28 // a `list`)
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
29 def __new__(
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
30 _cls,
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
31 map: PyObject,
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
32 skip: Option<PyObject> = None
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
33 ) -> PyResult<Self> {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
34 let mut skip_state: Option<EntryState> = None;
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
35 if let Some(skip) = skip {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
36 skip_state = Some(
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
37 skip.extract::<PyBytes>(py)?.data(py)[0]
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
38 .try_into()
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
39 .map_err(|e: DirstateParseError| {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
40 PyErr::new::<exc::ValueError, _>(py, e.to_string())
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
41 })?,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
42 );
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
43 }
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42746
diff changeset
44 let inner = if let Ok(map) = map.cast_as::<PyDict>(py) {
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42746
diff changeset
45 let dirstate = extract_dirstate(py, &map)?;
42802
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42752
diff changeset
46 DirsMultiset::from_dirstate(&dirstate, skip_state)
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
47 } else {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
48 let map: Result<Vec<Vec<u8>>, PyErr> = map
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
49 .iter(py)?
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
50 .map(|o| Ok(o?.extract::<PyBytes>(py)?.data(py).to_owned()))
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
51 .collect();
42802
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42752
diff changeset
52 DirsMultiset::from_manifest(&map?)
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42746
diff changeset
53 };
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
54
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
55 Self::create_instance(
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
56 py,
42849
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
57 PySharedRefCell::new(inner),
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
58 PySharedState::default()
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
59 )
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
60 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
61
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
62 def addpath(&self, path: PyObject) -> PyResult<PyObject> {
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
63 self.borrow_mut(py)?.add_path(
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
64 path.extract::<PyBytes>(py)?.data(py),
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
65 );
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
66 Ok(py.None())
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
67 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
68
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
69 def delpath(&self, path: PyObject) -> PyResult<PyObject> {
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
70 self.borrow_mut(py)?.delete_path(
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
71 path.extract::<PyBytes>(py)?.data(py),
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
72 )
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
73 .and(Ok(py.None()))
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
74 .or_else(|e| {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
75 match e {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
76 DirstateMapError::PathNotFound(_p) => {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
77 Err(PyErr::new::<exc::ValueError, _>(
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
78 py,
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
79 "expected a value, found none".to_string(),
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
80 ))
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
81 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
82 DirstateMapError::EmptyPath => {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
83 Ok(py.None())
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
84 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
85 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
86 })
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
87 }
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
88 def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> {
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
89 DirsMultisetKeysIterator::create_instance(
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
90 py,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
91 RefCell::new(Some(DirsMultisetLeakedRef::new(py, &self))),
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
92 RefCell::new(Box::new(self.leak_immutable(py)?.iter())),
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
93 )
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
94 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
95
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
96 def __contains__(&self, item: PyObject) -> PyResult<bool> {
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
97 Ok(self
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
98 .inner(py)
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
99 .borrow()
42750
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42749
diff changeset
100 .contains(item.extract::<PyBytes>(py)?.data(py).as_ref()))
42537
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
101 }
ce94f9622acd rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents: 42408
diff changeset
102 });
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
103
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
104 py_shared_ref!(Dirs, DirsMultiset, inner, DirsMultisetLeakedRef,);
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
105
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
106 impl Dirs {
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
107 pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
42849
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
108 Self::create_instance(
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
109 py,
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
110 PySharedRefCell::new(d),
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
111 PySharedState::default(),
8db8fa1de2ef rust-cpython: introduce restricted variant of RefCell
Yuya Nishihara <yuya@tcha.org>
parents: 42802
diff changeset
112 )
42752
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
113 }
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
114
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
115 fn translate_key(py: Python, res: &Vec<u8>) -> PyResult<Option<PyBytes>> {
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
116 Ok(Some(PyBytes::new(py, res)))
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
117 }
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
118 }
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
119
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
120 py_shared_sequence_iterator!(
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
121 DirsMultisetKeysIterator,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
122 DirsMultisetLeakedRef,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
123 Vec<u8>,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
124 Dirs::translate_key,
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
125 Option<PyBytes>
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42750
diff changeset
126 );