# HG changeset patch # User Georges Racinet # Date 2019-12-13 18:52:26 # Node ID 2728fcb8127cfb14dd1f84ab94336da6935b1226 # Parent e685fac56693490178cfdc5edd067666dc082f41 rust-index: make it possible to clone the struct referencing the C index If we are to hand over the C index object to other code, we need to be able to create a new python reference to it. Differential Revision: https://phab.mercurial-scm.org/D7656 diff --git a/rust/hg-cpython/src/cindex.rs b/rust/hg-cpython/src/cindex.rs --- a/rust/hg-cpython/src/cindex.rs +++ b/rust/hg-cpython/src/cindex.rs @@ -88,6 +88,15 @@ impl Clone for Index { } } +impl PyClone for Index { + fn clone_ref(&self, py: Python) -> Self { + Index { + index: self.index.clone_ref(py), + capi: self.capi, + } + } +} + impl Graph for Index { /// wrap a call to the C extern parents function fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { diff --git a/rust/hg-cpython/src/revlog.rs b/rust/hg-cpython/src/revlog.rs --- a/rust/hg-cpython/src/revlog.rs +++ b/rust/hg-cpython/src/revlog.rs @@ -7,17 +7,14 @@ use crate::cindex; use cpython::{ - ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, - PythonObject, ToPyObject, + ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, PythonObject, + ToPyObject, }; use hg::Revision; use std::cell::RefCell; /// Return a Struct implementing the Graph trait -pub(crate) fn pyindex_to_graph( - py: Python, - index: PyObject, -) -> PyResult { +pub(crate) fn pyindex_to_graph(py: Python, index: PyObject) -> PyResult { cindex::Index::new(py, index) } @@ -198,6 +195,10 @@ impl MixedIndex { .inner() .call_method(py, name, args, kwargs) } + + pub fn clone_cindex(&self, py: Python) -> cindex::Index { + self.cindex(py).borrow().clone_ref(py) + } } /// Create the module, with __package__ given from parent