author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
Thu, 01 Jun 2023 12:05:32 +0100 | |
changeset 50571 | 6a019a037085 |
parent 50569 | a8531bd9210b |
permissions | -rw-r--r-- |
42762
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
1 |
// dirs_multiset.rs |
42330
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 |
|
42762
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 |
42330
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. |
42762
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; |
42330
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
12 |
|
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
13 |
use cpython::{ |
48043
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
14 |
exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult, |
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
15 |
Python, UnsafePyLeaked, |
42330
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
16 |
}; |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
|
42893
706104dcb2c8
rust-cpython: replace dyn Iterator<..> of sequence with concrete type
Yuya Nishihara <yuya@tcha.org>
parents:
42857
diff
changeset
|
18 |
use hg::{ |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
19 |
utils::hg_path::{HgPath, HgPathBuf}, |
50569
a8531bd9210b
rust: remove an unused error variant DirstateMapError::EmptyPath
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49987
diff
changeset
|
20 |
DirsMultiset, DirsMultisetIter, |
42893
706104dcb2c8
rust-cpython: replace dyn Iterator<..> of sequence with concrete type
Yuya Nishihara <yuya@tcha.org>
parents:
42857
diff
changeset
|
21 |
}; |
42330
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
22 |
|
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
23 |
py_class!(pub class Dirs |py| { |
44296
bad4e7b361d2
rust-cpython: switch to upstreamed version of PySharedRefCell
Yuya Nishihara <yuya@tcha.org>
parents:
44295
diff
changeset
|
24 |
@shared data inner: DirsMultiset; |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
25 |
|
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
26 |
// `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:
42427
diff
changeset
|
27 |
// a `list`) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
28 |
def __new__( |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
29 |
_cls, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
30 |
map: PyObject, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
31 |
) -> PyResult<Self> { |
48043
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
32 |
let inner = if map.cast_as::<PyDict>(py).is_ok() { |
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
33 |
let err = "pathutil.dirs() with a dict should only be used by the Python dirstatemap \ |
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
34 |
and should not be used when Rust is enabled"; |
627cd8f33db0
rust: Remove support for passing a dict to the Rust pathutil.dirs()
Simon Sapin <simon.sapin@octobus.net>
parents:
47972
diff
changeset
|
35 |
return Err(PyErr::new::<exc::TypeError, _>(py, err.to_string())) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
36 |
} else { |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
37 |
let map: Result<Vec<HgPathBuf>, PyErr> = map |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
38 |
.iter(py)? |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
39 |
.map(|o| { |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
40 |
Ok(HgPathBuf::from_bytes( |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
41 |
o?.extract::<PyBytes>(py)?.data(py), |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
42 |
)) |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
43 |
}) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
44 |
.collect(); |
42818
2e1f74cc3350
rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents:
42768
diff
changeset
|
45 |
DirsMultiset::from_manifest(&map?) |
43874
bc7d8f45c3b6
rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents:
43808
diff
changeset
|
46 |
.map_err(|e| { |
bc7d8f45c3b6
rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents:
43808
diff
changeset
|
47 |
PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
bc7d8f45c3b6
rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents:
43808
diff
changeset
|
48 |
})? |
42764
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42762
diff
changeset
|
49 |
}; |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
50 |
|
44296
bad4e7b361d2
rust-cpython: switch to upstreamed version of PySharedRefCell
Yuya Nishihara <yuya@tcha.org>
parents:
44295
diff
changeset
|
51 |
Self::create_instance(py, inner) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
52 |
} |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
53 |
|
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
54 |
def addpath(&self, path: PyObject) -> PyResult<PyObject> { |
44295
281642cd1d04
rust-cpython: rename inner_shared() to inner()
Yuya Nishihara <yuya@tcha.org>
parents:
44278
diff
changeset
|
55 |
self.inner(py).borrow_mut().add_path( |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
56 |
HgPath::new(path.extract::<PyBytes>(py)?.data(py)), |
50571
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
57 |
).and(Ok(py.None())).map_err(|e| PyErr::new::<exc::ValueError, _>( |
43808
1fe2e574616e
rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents:
43482
diff
changeset
|
58 |
py, |
1fe2e574616e
rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents:
43482
diff
changeset
|
59 |
e.to_string(), |
50571
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
60 |
) |
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
61 |
) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
62 |
} |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
63 |
|
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
64 |
def delpath(&self, path: PyObject) -> PyResult<PyObject> { |
44295
281642cd1d04
rust-cpython: rename inner_shared() to inner()
Yuya Nishihara <yuya@tcha.org>
parents:
44278
diff
changeset
|
65 |
self.inner(py).borrow_mut().delete_path( |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
66 |
HgPath::new(path.extract::<PyBytes>(py)?.data(py)), |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
67 |
) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
68 |
.and(Ok(py.None())) |
50571
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
69 |
.map_err(|e| |
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
70 |
PyErr::new::<exc::ValueError, _>( |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
71 |
py, |
43808
1fe2e574616e
rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents:
43482
diff
changeset
|
72 |
e.to_string(), |
50571
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
73 |
) |
6a019a037085
cleanup: simplify code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50569
diff
changeset
|
74 |
) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
75 |
} |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
76 |
def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> { |
44295
281642cd1d04
rust-cpython: rename inner_shared() to inner()
Yuya Nishihara <yuya@tcha.org>
parents:
44278
diff
changeset
|
77 |
let leaked_ref = self.inner(py).leak_immutable(); |
42897
5ccc08d02280
rust-cpython: leverage py_shared_iterator::from_inner() where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
42895
diff
changeset
|
78 |
DirsMultisetKeysIterator::from_inner( |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
79 |
py, |
43285
ffc1fbd7d1f5
rust-cpython: make PyLeakedRef operations relatively safe
Yuya Nishihara <yuya@tcha.org>
parents:
43284
diff
changeset
|
80 |
unsafe { leaked_ref.map(py, |o| o.iter()) }, |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
81 |
) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
82 |
} |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
83 |
|
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
84 |
def __contains__(&self, item: PyObject) -> PyResult<bool> { |
44295
281642cd1d04
rust-cpython: rename inner_shared() to inner()
Yuya Nishihara <yuya@tcha.org>
parents:
44278
diff
changeset
|
85 |
Ok(self.inner(py).borrow().contains(HgPath::new( |
49987
58074252db3c
rust: run `cargo clippy`
Raphaël Gomès <rgomes@octobus.net>
parents:
48043
diff
changeset
|
86 |
item.extract::<PyBytes>(py)?.data(py), |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
87 |
))) |
42544
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
88 |
} |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42427
diff
changeset
|
89 |
}); |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
90 |
|
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
91 |
impl Dirs { |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
92 |
pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> { |
44296
bad4e7b361d2
rust-cpython: switch to upstreamed version of PySharedRefCell
Yuya Nishihara <yuya@tcha.org>
parents:
44295
diff
changeset
|
93 |
Self::create_instance(py, d) |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
94 |
} |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
95 |
|
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
96 |
fn translate_key( |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
97 |
py: Python, |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
98 |
res: &HgPathBuf, |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42897
diff
changeset
|
99 |
) -> PyResult<Option<PyBytes>> { |
44998
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44296
diff
changeset
|
100 |
Ok(Some(PyBytes::new(py, res.as_bytes()))) |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
101 |
} |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
102 |
} |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
103 |
|
42895
ea91a126c803
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator
Yuya Nishihara <yuya@tcha.org>
parents:
42893
diff
changeset
|
104 |
py_shared_iterator!( |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
105 |
DirsMultisetKeysIterator, |
44296
bad4e7b361d2
rust-cpython: switch to upstreamed version of PySharedRefCell
Yuya Nishihara <yuya@tcha.org>
parents:
44295
diff
changeset
|
106 |
UnsafePyLeaked<DirsMultisetIter<'static>>, |
42768
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
107 |
Dirs::translate_key, |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
108 |
Option<PyBytes> |
30320c7bf79f
rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents:
42766
diff
changeset
|
109 |
); |