##// END OF EJS Templates
rust-index: using `hg::index::Index` in `hg-cpython::dagops`...
rust-index: using `hg::index::Index` in `hg-cpython::dagops` Hooking `headrevs` to the Rust index is straightforward as long as we go the `PySharedRef` way. Direct attempts of obtaining a reference to the inner `hg::index::Index` fail for lifetime reasons: the reference is bound to the GIL, yet the `as_set` local variable is considered to be static (the borrow checker clearly does not realize or care that this set only stores `Revision` values). In `rank()`, the chosen solution is the simplest as far as `hg-cpython` is concerned, but it has the defect of removing an implementation that would be easily adaptable if the core index did implement `RankedGraph` (returning the same error as long as only `REVLOGV1` is supported), but that would introduce a direct dependency of `hg-core` on the ``vcsgraph` crate.

File last commit:

r50525:c7fb9b74 default
r52134:578c049f default
Show More
utils.rs
43 lines | 1.4 KiB | application/rls-services+xml | RustLexer
Georges Racinet
rust-nodemap: add utils to create `Node`s from Python objects...
r44992 use cpython::exc::ValueError;
use cpython::{PyBytes, PyDict, PyErr, PyObject, PyResult, PyTuple, Python};
use hg::revlog::Node;
Raphaël Gomès
rust-utils: introduce a debug util to print the python stack trace...
r43545
#[allow(unused)]
pub fn print_python_trace(py: Python) -> PyResult<PyObject> {
eprintln!("===============================");
eprintln!("Printing Python stack from Rust");
eprintln!("===============================");
let traceback = py.import("traceback")?;
let sys = py.import("sys")?;
let kwargs = PyDict::new(py);
kwargs.set_item(py, "file", sys.get(py, "stderr")?)?;
traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs))
}
Georges Racinet
rust-nodemap: add utils to create `Node`s from Python objects...
r44992
// Necessary evil for the time being, could maybe be moved to
// a TryFrom in Node itself
const NODE_BYTES_LENGTH: usize = 20;
type NodeData = [u8; NODE_BYTES_LENGTH];
/// Copy incoming Python bytes given as `PyObject` into `Node`,
/// doing the necessary checks
pub fn node_from_py_object<'a>(
py: Python,
bytes: &'a PyObject,
) -> PyResult<Node> {
let as_py_bytes: &'a PyBytes = bytes.extract(py)?;
node_from_py_bytes(py, as_py_bytes)
}
/// Clone incoming Python bytes given as `PyBytes` as a `Node`,
/// doing the necessary checks.
Raphaël Gomès
rust: do a clippy pass...
r45500 pub fn node_from_py_bytes(py: Python, bytes: &PyBytes) -> PyResult<Node> {
Georges Racinet
rust-nodemap: add utils to create `Node`s from Python objects...
r44992 <NodeData>::try_from(bytes.data(py))
.map_err(|_| {
PyErr::new::<ValueError, _>(
py,
format!("{}-byte hash required", NODE_BYTES_LENGTH),
)
})
Raphaël Gomès
rust: do a clippy pass...
r45500 .map(Into::into)
Georges Racinet
rust-nodemap: add utils to create `Node`s from Python objects...
r44992 }