rust: propagate error of index_get_parents() properly
authorYuya Nishihara <yuya@tcha.org>
Mon, 29 Oct 2018 21:50:53 +0900
changeset 40863 443eb4bc41af
parent 40862 54a60968f0aa
child 40864 09680349cc2d
rust: propagate error of index_get_parents() properly Before, rustla_contains() would return 0 on error, and the exception would be cleared or noticed somewhere else. We need to propagate the error from AncestorsIterator up to the FFI surface.
rust/hg-core/src/ancestors.rs
rust/hg-direct-ffi/src/ancestors.rs
--- a/rust/hg-core/src/ancestors.rs	Sun Dec 02 22:20:38 2018 +0900
+++ b/rust/hg-core/src/ancestors.rs	Mon Oct 29 21:50:53 2018 +0900
@@ -77,19 +77,20 @@
     /// is in the ancestors it emits.
     /// This is meant for iterators actually dedicated to that kind of
     /// purpose
-    pub fn contains(&mut self, target: Revision) -> bool {
+    pub fn contains(&mut self, target: Revision) -> Result<bool, GraphError> {
         if self.seen.contains(&target) && target != NULL_REVISION {
-            return true;
+            return Ok(true);
         }
-        for rev in self {
+        for item in self {
+            let rev = item?;
             if rev == target {
-                return true;
+                return Ok(true);
             }
             if rev < target {
-                return false;
+                return Ok(false);
             }
         }
-        false
+        Ok(false)
     }
 }
 
@@ -117,19 +118,19 @@
 ///   concrete caller we target, so we shouldn't need a finer error treatment
 ///   for the time being.
 impl<G: Graph> Iterator for AncestorsIterator<G> {
-    type Item = Revision;
+    type Item = Result<Revision, GraphError>;
 
-    fn next(&mut self) -> Option<Revision> {
+    fn next(&mut self) -> Option<Self::Item> {
         let current = match self.visit.peek() {
             None => {
                 return None;
             }
             Some(c) => *c,
         };
-        let (p1, p2) = self
-            .graph
-            .parents(current)
-            .unwrap_or((NULL_REVISION, NULL_REVISION));
+        let (p1, p2) = match self.graph.parents(current) {
+            Ok(ps) => ps,
+            Err(e) => return Some(Err(e)),
+        };
         if p1 < self.stoprev || self.seen.contains(&p1) {
             self.visit.pop();
         } else {
@@ -138,7 +139,7 @@
         };
 
         self.conditionally_push_rev(p2);
-        Some(current)
+        Some(Ok(current))
     }
 }
 
--- a/rust/hg-direct-ffi/src/ancestors.rs	Sun Dec 02 22:20:38 2018 +0900
+++ b/rust/hg-direct-ffi/src/ancestors.rs	Mon Oct 29 21:50:53 2018 +0900
@@ -139,7 +139,11 @@
 #[inline]
 fn raw_next<G: Graph>(raw: *mut AncestorsIterator<G>) -> c_long {
     let as_ref = unsafe { &mut *raw };
-    as_ref.next().unwrap_or(NULL_REVISION) as c_long
+    let rev = match as_ref.next() {
+        Some(Ok(rev)) => rev,
+        Some(Err(_)) | None => NULL_REVISION,
+    };
+    rev as c_long
 }
 
 #[no_mangle]
@@ -157,10 +161,10 @@
     target: c_long,
 ) -> c_int {
     let as_ref = unsafe { &mut *raw };
-    if as_ref.contains(target as Revision) {
-        return 1;
+    match as_ref.contains(target as Revision) {
+        Ok(r) => r as c_int,
+        Err(_) => -1,
     }
-    0
 }
 
 #[cfg(test)]