rust: look up HgRevlogIndex_GetParents() from symbol table
authorYuya Nishihara <yuya@tcha.org>
Sun, 02 Dec 2018 22:20:38 +0900
changeset 40862 54a60968f0aa
parent 40861 b12700dd261f
child 40863 443eb4bc41af
rust: look up HgRevlogIndex_GetParents() from symbol table And removes the unused index_get_parents_checked() function. I expect the Index struct will be turned into a pyobject type, though I haven't written any PoC-level patches yet.
mercurial/cext/revlog.c
rust/hg-direct-ffi/src/ancestors.rs
--- a/mercurial/cext/revlog.c	Sun Dec 02 22:10:37 2018 +0900
+++ b/mercurial/cext/revlog.c	Sun Dec 02 22:20:38 2018 +0900
@@ -2710,27 +2710,15 @@
 };
 
 /* FFI exposed from Rust code */
-rustlazyancestorsObject *
-rustlazyancestors_init(indexObject *index,
-                       /* to pass index_get_parents() */
-                       int (*)(indexObject *, Py_ssize_t, int *, int),
-                       /* intrevs vector */
-                       Py_ssize_t initrevslen, long *initrevs, long stoprev,
-                       int inclusive);
+rustlazyancestorsObject *rustlazyancestors_init(indexObject *index,
+                                                /* intrevs vector */
+                                                Py_ssize_t initrevslen,
+                                                long *initrevs, long stoprev,
+                                                int inclusive);
 void rustlazyancestors_drop(rustlazyancestorsObject *self);
 int rustlazyancestors_next(rustlazyancestorsObject *self);
 int rustlazyancestors_contains(rustlazyancestorsObject *self, long rev);
 
-static int index_get_parents_checked(indexObject *self, Py_ssize_t rev, int *ps,
-                                     int maxrev)
-{
-	if (rev < 0 || rev >= index_length(self)) {
-		PyErr_SetString(PyExc_ValueError, "rev out of range");
-		return -1;
-	}
-	return index_get_parents(self, rev, ps, maxrev);
-}
-
 /* CPython instance methods */
 static int rustla_init(rustlazyancestorsObject *self, PyObject *args)
 {
@@ -2768,12 +2756,12 @@
 	if (PyErr_Occurred())
 		goto bail;
 
-	self->iter = rustlazyancestors_init(index, index_get_parents, linit,
-	                                    initrevs, stoprev, inclusive);
+	self->iter =
+	    rustlazyancestors_init(index, linit, initrevs, stoprev, inclusive);
 	if (self->iter == NULL) {
 		/* if this is because of GraphError::ParentOutOfRange
-		 * index_get_parents_checked() has already set the proper
-		 * ValueError */
+		 * HgRevlogIndex_GetParents() has already set the proper
+		 * exception */
 		goto bail;
 	}
 
--- a/rust/hg-direct-ffi/src/ancestors.rs	Sun Dec 02 22:10:37 2018 +0900
+++ b/rust/hg-direct-ffi/src/ancestors.rs	Sun Dec 02 22:20:38 2018 +0900
@@ -16,9 +16,14 @@
 use std::slice;
 
 type IndexPtr = *mut c_void;
-type IndexParentsFn =
-    unsafe extern "C" fn(index: IndexPtr, rev: ssize_t, ps: *mut [c_int; 2], max_rev: c_int)
-        -> c_int;
+
+extern "C" {
+    fn HgRevlogIndex_GetParents(
+        op: IndexPtr,
+        rev: c_int,
+        parents: *mut [c_int; 2],
+    ) -> c_int;
+}
 
 /// A Graph backed up by objects and functions from revlog.c
 ///
@@ -27,14 +32,12 @@
 /// - the `index_get_parents()` function (`parents` member)
 pub struct Index {
     index: IndexPtr,
-    parents: IndexParentsFn,
 }
 
 impl Index {
-    pub fn new(index: IndexPtr, parents: IndexParentsFn) -> Self {
+    pub fn new(index: IndexPtr) -> Self {
         Index {
             index: index,
-            parents: parents,
         }
     }
 }
@@ -44,7 +47,7 @@
     fn parents(&self, rev: Revision) -> Result<(Revision, Revision), GraphError> {
         let mut res: [c_int; 2] = [0; 2];
         let code =
-            unsafe { (self.parents)(self.index, rev as ssize_t, &mut res as *mut [c_int; 2], rev) };
+            unsafe { HgRevlogIndex_GetParents(self.index, rev, &mut res as *mut [c_int; 2]) };
         match code {
             0 => Ok((res[0], res[1])),
             _ => Err(GraphError::ParentOutOfRange(rev)),
@@ -59,7 +62,6 @@
 #[no_mangle]
 pub extern "C" fn rustlazyancestors_init(
     index: IndexPtr,
-    parents: IndexParentsFn,
     initrevslen: ssize_t,
     initrevs: *mut c_long,
     stoprev: c_long,
@@ -68,7 +70,7 @@
     assert!(initrevslen >= 0);
     unsafe {
         raw_init(
-            Index::new(index, parents),
+            Index::new(index),
             initrevslen as usize,
             initrevs,
             stoprev,