annotate rust/hg-cpython/src/utils.rs @ 45610:496537c9c1b4

rust: start plugging the dirstate tree behind a feature gate The previous patch added the `dirstate-tree` feature gate to enable the two dirstate implementations to co-habit while the tree-based one gets better. This patch copies over the code that differs, be it because the algorithm changed or because the borrowing rules are different. Indeed, `DirstateTree` is not observationally equivalent to the std `HashMap` in the APIs we use: it does not have the `Entry` API (yet?) and its iterator returns owned values instead of references. This last point is because the implementation needs to be changed to a more clever and efficient solution. Differential Revision: https://phab.mercurial-scm.org/D9133
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 30 Sep 2020 18:10:29 +0200
parents 26114bd6ec60
children c7fb9b74e753
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44505
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
1 use cpython::exc::ValueError;
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
2 use cpython::{PyBytes, PyDict, PyErr, PyObject, PyResult, PyTuple, Python};
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
3 use hg::revlog::Node;
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
4 use std::convert::TryFrom;
43251
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 #[allow(unused)]
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7 pub fn print_python_trace(py: Python) -> PyResult<PyObject> {
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
8 eprintln!("===============================");
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9 eprintln!("Printing Python stack from Rust");
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
10 eprintln!("===============================");
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
11 let traceback = py.import("traceback")?;
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
12 let sys = py.import("sys")?;
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
13 let kwargs = PyDict::new(py);
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
14 kwargs.set_item(py, "file", sys.get(py, "stderr")?)?;
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
15 traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs))
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
16 }
44505
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
17
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
18 // Necessary evil for the time being, could maybe be moved to
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
19 // a TryFrom in Node itself
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
20 const NODE_BYTES_LENGTH: usize = 20;
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
21 type NodeData = [u8; NODE_BYTES_LENGTH];
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
22
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
23 /// Copy incoming Python bytes given as `PyObject` into `Node`,
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
24 /// doing the necessary checks
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
25 pub fn node_from_py_object<'a>(
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
26 py: Python,
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
27 bytes: &'a PyObject,
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
28 ) -> PyResult<Node> {
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
29 let as_py_bytes: &'a PyBytes = bytes.extract(py)?;
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
30 node_from_py_bytes(py, as_py_bytes)
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
31 }
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
32
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
33 /// Clone incoming Python bytes given as `PyBytes` as a `Node`,
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
34 /// doing the necessary checks.
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44505
diff changeset
35 pub fn node_from_py_bytes(py: Python, bytes: &PyBytes) -> PyResult<Node> {
44505
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
36 <NodeData>::try_from(bytes.data(py))
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
37 .map_err(|_| {
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
38 PyErr::new::<ValueError, _>(
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
39 py,
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
40 format!("{}-byte hash required", NODE_BYTES_LENGTH),
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
41 )
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
42 })
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44505
diff changeset
43 .map(Into::into)
44505
d738b7a18438 rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents: 43251
diff changeset
44 }