rust: blanket implementation of Graph for Graph references stable
authorGeorges Racinet <georges.racinet@octobus.net>
Mon, 22 Apr 2024 19:47:08 +0200
branchstable
changeset 51495 b08c5fbe0e70
parent 51494 ccf5c44092db
child 51496 028dc3f92dbd
rust: blanket implementation of Graph for Graph references The need comes from the fact that `AncestorsIterator` and many Graph-related algorithms take ownership of the `Graph` they work with. This, in turn is due to them needing to accept the `Index` instances that are provided by the Python layers (that neither rhg nor `RHGitaly` use, of course): the fact that nowadays the Python layer holds an object that is itself implemented in Rust does not change the core problem that they cannot be tracked by the borrow checker. Even though it looks like cloning `Changelog` would be cheap, it seems hard to guarantee that on the long run. The object is already too rich for us to be comfortable with it, when using references is the most natural and guaranteed way of proceeding. The added test seems a bit superfleous, but it will act as a reminder that this feature is really useful until something in the Mercurial code base actually uses it.
rust/hg-core/src/ancestors.rs
rust/hg-core/src/revlog/mod.rs
--- a/rust/hg-core/src/ancestors.rs	Mon May 06 15:30:21 2024 +0200
+++ b/rust/hg-core/src/ancestors.rs	Mon Apr 22 19:47:08 2024 +0200
@@ -423,6 +423,18 @@
             ),
             vec![8, 7, 4, 3, 2, 1, 0]
         );
+        // it works as well on references, because &Graph implements Graph
+        // this is needed as of this writing by RHGitaly
+        assert_eq!(
+            list_ancestors(
+                &SampleGraph,
+                vec![11.into(), 13.into()],
+                0.into(),
+                false
+            ),
+            vec![8, 7, 4, 3, 2, 1, 0]
+        );
+
         assert_eq!(
             list_ancestors(
                 SampleGraph,
--- a/rust/hg-core/src/revlog/mod.rs	Mon May 06 15:30:21 2024 +0200
+++ b/rust/hg-core/src/revlog/mod.rs	Mon Apr 22 19:47:08 2024 +0200
@@ -132,6 +132,12 @@
     ParentOutOfRange(Revision),
 }
 
+impl<T: Graph> Graph for &T {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        (*self).parents(rev)
+    }
+}
+
 /// The Mercurial Revlog Index
 ///
 /// This is currently limited to the minimal interface that is needed for