Patchwork D7653: rust-index: add a function to convert PyObject index for hg-core

login
register
mail settings
Submitter phabricator
Date Dec. 13, 2019, 8:13 p.m.
Message ID <differential-rev-PHID-DREV-hzb5dc6vweaslftigow5-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43813/
State Superseded
Headers show

Comments

phabricator - Dec. 13, 2019, 8:13 p.m.
marmoute created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Function in hg-core need something implementing the `Graph` trait. Right now,
  the `hg-cpython` entry points directly turn the PyObject passed as argument
  into a `cindex::Index`. However, if we start having the option to use an Index
  in Rust, we need to dispatch between the different possible PyObject we could
  receive.
  
  So move the "duplicate" call into a unified function. When time come. It will be
  easy to update the logic of all interface when the time come.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7653

AFFECTED FILES
  rust/hg-cpython/src/ancestors.rs
  rust/hg-cpython/src/dagops.rs
  rust/hg-cpython/src/discovery.rs
  rust/hg-cpython/src/lib.rs
  rust/hg-cpython/src/revlog.rs

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel

Patch

diff --git a/rust/hg-cpython/src/revlog.rs b/rust/hg-cpython/src/revlog.rs
new file mode 100644
--- /dev/null
+++ b/rust/hg-cpython/src/revlog.rs
@@ -0,0 +1,14 @@ 
+// revlog.rs
+//
+// Copyright 2019 Georges Racinet <georges.racinet@octobus.net>
+//
+// This software may be used and distributed according to the terms of the
+// GNU General Public License version 2 or any later version.
+
+use crate::cindex;
+use cpython::{PyObject, PyResult, Python};
+
+/// Return a Struct implementing the Graph trait
+pub(crate) fn pyindex_to_graph(py: Python, index: PyObject) -> PyResult<cindex::Index> {
+    cindex::Index::new(py, index)
+}
diff --git a/rust/hg-cpython/src/lib.rs b/rust/hg-cpython/src/lib.rs
--- a/rust/hg-cpython/src/lib.rs
+++ b/rust/hg-cpython/src/lib.rs
@@ -35,6 +35,7 @@ 
 pub mod exceptions;
 pub mod filepatterns;
 pub mod parsers;
+pub mod revlog;
 pub mod utils;
 
 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
diff --git a/rust/hg-cpython/src/discovery.rs b/rust/hg-cpython/src/discovery.rs
--- a/rust/hg-cpython/src/discovery.rs
+++ b/rust/hg-cpython/src/discovery.rs
@@ -12,12 +12,9 @@ 
 //! - [`PartialDiscover`] is the Rust implementation of
 //!   `mercurial.setdiscovery.partialdiscovery`.
 
-use crate::{
-    cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError,
-};
+use crate::{cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError};
 use cpython::{
-    ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python,
-    PythonObject, ToPyObject,
+    ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, PythonObject, ToPyObject,
 };
 use hg::discovery::PartialDiscovery as CorePartialDiscovery;
 use hg::Revision;
@@ -25,6 +22,8 @@ 
 
 use std::cell::RefCell;
 
+use crate::revlog::pyindex_to_graph;
+
 py_class!(pub class PartialDiscovery |py| {
     data inner: RefCell<Box<CorePartialDiscovery<Index>>>;
 
@@ -42,7 +41,7 @@ 
         Self::create_instance(
             py,
             RefCell::new(Box::new(CorePartialDiscovery::new(
-                Index::new(py, index)?,
+                pyindex_to_graph(py, index)?,
                 rev_pyiter_collect(py, &targetheads)?,
                 respectsize,
                 randomize,
diff --git a/rust/hg-cpython/src/dagops.rs b/rust/hg-cpython/src/dagops.rs
--- a/rust/hg-cpython/src/dagops.rs
+++ b/rust/hg-cpython/src/dagops.rs
@@ -9,24 +9,20 @@ 
 //! `hg-core` package.
 //!
 //! From Python, this will be seen as `mercurial.rustext.dagop`
-use crate::{
-    cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError,
-};
+use crate::{conversion::rev_pyiter_collect, exceptions::GraphError};
 use cpython::{PyDict, PyModule, PyObject, PyResult, Python};
 use hg::dagops;
 use hg::Revision;
 use std::collections::HashSet;
 
+use crate::revlog::pyindex_to_graph;
+
 /// Using the the `index`, return heads out of any Python iterable of Revisions
 ///
 /// This is the Rust counterpart for `mercurial.dagop.headrevs`
-pub fn headrevs(
-    py: Python,
-    index: PyObject,
-    revs: PyObject,
-) -> PyResult<HashSet<Revision>> {
+pub fn headrevs(py: Python, index: PyObject, revs: PyObject) -> PyResult<HashSet<Revision>> {
     let mut as_set: HashSet<Revision> = rev_pyiter_collect(py, &revs)?;
-    dagops::retain_heads(&Index::new(py, index)?, &mut as_set)
+    dagops::retain_heads(&pyindex_to_graph(py, index)?, &mut as_set)
         .map_err(|e| GraphError::pynew(py, e))?;
     Ok(as_set)
 }
diff --git a/rust/hg-cpython/src/ancestors.rs b/rust/hg-cpython/src/ancestors.rs
--- a/rust/hg-cpython/src/ancestors.rs
+++ b/rust/hg-cpython/src/ancestors.rs
@@ -34,17 +34,15 @@ 
 //! [`LazyAncestors`]: struct.LazyAncestors.html
 //! [`MissingAncestors`]: struct.MissingAncestors.html
 //! [`AncestorsIterator`]: struct.AncestorsIterator.html
-use crate::{
-    cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError,
-};
+use crate::revlog::pyindex_to_graph;
+use crate::{cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError};
 use cpython::{
-    ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult,
-    Python, PythonObject, ToPyObject,
+    ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult, Python, PythonObject,
+    ToPyObject,
 };
 use hg::Revision;
 use hg::{
-    AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy,
-    MissingAncestors as CoreMissing,
+    AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy, MissingAncestors as CoreMissing,
 };
 use std::cell::RefCell;
 use std::collections::HashSet;
@@ -73,7 +71,7 @@ 
                 inclusive: bool) -> PyResult<AncestorsIterator> {
         let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
         let ait = CoreIterator::new(
-            Index::new(py, index)?,
+            pyindex_to_graph(py, index)?,
             initvec,
             stoprev,
             inclusive,
@@ -113,7 +111,8 @@ 
         let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
 
         let lazy =
-            CoreLazy::new(Index::new(py, index)?, initvec, stoprev, inclusive)
+            CoreLazy::new(pyindex_to_graph(py, index)?,
+                          initvec, stoprev, inclusive)
                 .map_err(|e| GraphError::pynew(py, e))?;
 
         Self::create_instance(py, RefCell::new(Box::new(lazy)))
@@ -126,7 +125,7 @@ 
 
     def __new__(_cls, index: PyObject, bases: PyObject) -> PyResult<MissingAncestors> {
         let bases_vec: Vec<Revision> = rev_pyiter_collect(py, &bases)?;
-        let inner = CoreMissing::new(Index::new(py, index)?, bases_vec);
+        let inner = CoreMissing::new(pyindex_to_graph(py, index)?, bases_vec);
         MissingAncestors::create_instance(py, RefCell::new(Box::new(inner)))
     }