rust/hg-core/src/ancestors.rs
changeset 40300 72b94f946e90
parent 40271 dbc28c91f7ff
child 40811 e13ab4acf555
--- a/rust/hg-core/src/ancestors.rs	Sun Oct 14 01:39:22 2018 -0400
+++ b/rust/hg-core/src/ancestors.rs	Mon Oct 08 19:11:41 2018 +0200
@@ -80,6 +80,26 @@
         self.conditionally_push_rev(parents.1);
         Ok(())
     }
+
+    /// Consumes partially the iterator to tell if the given target
+    /// revision
+    /// 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 {
+        if self.seen.contains(&target) && target != NULL_REVISION {
+            return true;
+        }
+        for rev in self {
+            if rev == target {
+                return true;
+            }
+            if rev < target {
+                return false;
+            }
+        }
+        false
+    }
 }
 
 /// Main implementation.
@@ -203,6 +223,18 @@
         assert_eq!(iter.next(), None)
     }
 
+    #[test]
+    fn test_contains() {
+        let mut lazy =
+            AncestorsIterator::new(Stub, vec![10, 1], 0, true).unwrap();
+        assert!(lazy.contains(1));
+        assert!(!lazy.contains(3));
+
+        let mut lazy =
+            AncestorsIterator::new(Stub, vec![0], 0, false).unwrap();
+        assert!(!lazy.contains(NULL_REVISION));
+    }
+
     /// A corrupted Graph, supporting error handling tests
     struct Corrupted;